Modernize NetLib: Python 3.10+, type hints, Pydantic, and Poetry - #24
Merged
Conversation
This major update modernizes the netlib codebase to align with current Python best practices: ## Core Changes - Upgrade minimum Python version to 3.10+ - Remove all Python 2.x support and legacy code - Add full type hints throughout the codebase (PEP 484) - Implement Pydantic v2 for robust input/output validation ## Package Management - Migrate from setuptools to Poetry - Remove setup.py and requirements.txt - Add pyproject.toml with complete project configuration - Include py.typed marker for PEP 561 compliance ## Testing & Quality Assurance - Replace unittest with pytest framework - Add comprehensive test suite with 7 test modules - Implement pytest-cov for coverage reporting - Add pytest-mock for better mocking support ## Linting & Type Checking - Add ruff for fast, modern linting and formatting - Configure mypy for strict type checking - Add pylint for additional code quality checks - Create .ruff.toml configuration file ## CI/CD - Remove Travis CI (.travis.yml) - Implement GitHub Actions workflow - Add multi-version testing (Python 3.10, 3.11, 3.12) - Include automated linting, type checking, and testing - Add codecov integration for coverage reporting ## Code Improvements - Add Pydantic models for connection configurations - Modernize class syntax (remove explicit object inheritance) - Use modern string formatting (f-strings) - Add comprehensive docstrings - Improve error handling and type safety ## Documentation - Complete README.md rewrite with modern badges - Add detailed API reference - Include migration guide from v0.1.x - Add development setup instructions - Document all new features and tools Version bumped to 0.2.0 to reflect breaking changes. https://claude.ai/code/session_019D1TUrg1xPGse1HsS8pgqQ
## Security Enhancement - SecretStr for Passwords - Update all password fields to use Pydantic SecretStr type - Models updated: - ConnectionConfig.password now SecretStr - CredentialsData.password and .enable now SecretStr - Classes updated to handle SecretStr: - SSH class stores password as _password (SecretStr) - Telnet class stores password as _password (SecretStr) - Both classes use .get_secret_value() when using passwords - set_enable() methods accept str | SecretStr for flexibility - KeyRing.get_creds() returns plain strings (for backward compatibility) - Internal validation ensures passwords aren't empty - Benefits: - Passwords not exposed in logs or repr() - Better security for credential handling - IDE autocomplete won't leak password values ## Dynamic Version Management - __version__ now reads from pyproject.toml - Uses importlib.metadata when package is installed - Falls back to reading pyproject.toml during development - Added tomli dependency for Python <3.11 (Python 3.11+ uses built-in tomllib) - Graceful fallback to "0.2.0-dev" if neither method works ## Test Updates - Updated all tests to use .get_secret_value() for password assertions - Tests now properly validate SecretStr behavior - Maintained backward compatibility in test patterns https://claude.ai/code/session_019D1TUrg1xPGse1HsS8pgqQ
## Code Quality Improvements - Run ruff format to apply consistent code formatting - Fix all ruff linting issues automatically - Remove unused imports (typing.Any, pytest, MagicMock) - Fix import sorting across all modules - Sort __all__ exports alphabetically ## Manual Fixes - Remove unnecessary variable assignment before return in SSH.set_enable() - Add noqa comment for intentional function-level imports in test_imports.py - Update pyproject.toml per-file-ignores configuration ## Results - All ruff format checks pass ✓ - All ruff linting checks pass ✓ - Code follows consistent style guidelines - Improved code maintainability and readability https://claude.ai/code/session_019D1TUrg1xPGse1HsS8pgqQ
## Type Checking Fixes (mypy) - Add SecretStr() wrapper when creating Pydantic models - Import SecretStr in user_keyring.py - Convert passwords to SecretStr in SSH/Telnet constructors - Add explicit type annotations for recv() calls to satisfy mypy - Replace deprecated --strict-concatenate with --extra-checks - Fix unused type: ignore comments in __init__.py ## Code Quality Fixes - Fix sendall() to accept bytes instead of strings (encode strings) - Remove unnecessary UTF-8 encoding argument (ruff UP012) - Update pylint configuration to disable false positives: - no-member (Pydantic SecretStr false positives) - deprecated-module (telnetlib is needed despite deprecation) - broad-exception-caught (acceptable for version fallback) - possibly-used-before-assignment (false positive with while loops) - too-many-positional-arguments - redefined-builtin (__name__ is intentional) ## Test Fixes - Update SSH tests to expect bytes in sendall() assertions - Fix test_get_creds_creates_new_when_none_exist mock side_effect (added extra values for nested get_creds() call) - All 54 tests now passing ✓ ## Python 3.13 Support - Add Python 3.13 to pyproject.toml classifiers - Add Python 3.13 to GitHub Actions test matrix - Note: telnetlib deprecated warning expected (removal in 3.13) ## Verification Results ✓ ruff format --check: All files properly formatted ✓ ruff check: All checks passed ✓ mypy netlib: Success, no issues found ✓ pylint netlib: 10.00/10 rating ✓ pytest: 54 tests passed, 82.52% coverage https://claude.ai/code/session_019D1TUrg1xPGse1HsS8pgqQ
Python 3.13 removed the telnetlib module from the standard library, which is a core dependency for the Telnet class in this package. Until telnetlib is replaced with an alternative implementation, Python 3.13 cannot be supported. Changes: - Remove Python 3.13 from pyproject.toml classifiers - Remove Python 3.13 from GitHub Actions test matrix - Maximum supported Python version is now 3.12 https://claude.ai/code/session_019D1TUrg1xPGse1HsS8pgqQ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR modernizes the NetLib project by upgrading to Python 3.10+, adding comprehensive type hints, implementing Pydantic validation, migrating to Poetry for dependency management, and setting up a modern CI/CD pipeline with GitHub Actions.
Key Changes
Core Library Updates
|, f-strings, etc.)netlib/models.pywith validation models for:Dependency Management
setup.pyandrequirements.txtwithpyproject.tomlCode Quality & Linting
.ruff.tomlwith comprehensive linting rules (100 char line length, import sorting, naming conventions, etc.)pyproject.tomlCI/CD Pipeline
.github/workflows/ci.ymlwith:.travis.yml(outdated Python 2.7, 3.5, 3.6 support)Documentation
Project Structure
netlib/__init__.pyto export main classes (SSH, Telnet, KeyRing)py.typedmarker file for type checking supportBackward Compatibility
buffer="8192") are automatically converted to proper typesNotable Implementation Details
int | str | float)Nonestates for connectionsVersion
Bumped from v0.1.0 to v0.2.0 to reflect the significant modernization and breaking changes (Python 3.10+ requirement).
https://claude.ai/code/session_019D1TUrg1xPGse1HsS8pgqQ