Skip to content

Conversation

eric-downes
Copy link

PyContracts Python 3.8+ Compatibility Fixes

This PR adds compatibility with Python 3.8+ by fixing several issues.
The changes are minimal and focused on maintaining compatibility.

  1. inspect.ArgSpec Removal
    In Python 3.8+, inspect.ArgSpec was removed. Fixed by:
  • Adding a custom implementation using collections.namedtuple
  • Maintaining the same API and interface expected by the existing codebase
  • File modified: src/contracts/backported.py
  1. NumPy Deprecated Types
    Several NumPy types used in PyContracts were deprecated:
  • Replaced np.int,np.float with np.int64 etc
  • Replaced np.complex with np.complex128
  • File modified: src/contracts/library/array_ops.py
  1. Collections ABC Module Changes
    Abstract base classes moved from collections to collections.abc in Python 3.3+:
  • Added conditional import based on Python version
  • Used collections.abc for Container, Iterable, etc. in Python 3.3+
  • Replaced collections.Callable with the built-in callable function
  • File modified: src/contracts/library/miscellaneous_aliases.py
  1. Invalid Escape Sequences in Regular Expressions
    Fixed SyntaxWarnings related to invalid escape sequences in regular expressions:
  • Replaced \s with r'\s'; raw strings FTW!
  • File modified: src/contracts/docstring_parsing.py

Testing. All functionality tested with Python 3.8+:

  • Basic contract checking
  • Type description
  • Docstring parsing
  • String utilities
  • Exception wrapping

@eric-downes
Copy link
Author

I believe this addresses #72

@eric-downes
Copy link
Author

  1. Created a compatibility layer in py_compatibility.py that handles:
    • String type differences between Python 2 and 3
    • Collections module changes in Python 3.12+
    • Exception handling differences
    • StringIO compatibility
  2. Updated string handling:
    - Replaced all occurrences of six.string_types with our string_types
    - Fixed string formatting and comparison
  3. Fixed collections imports for Python 3.12 compatibility:
    - Updated imports in seq.py, map.py, and sets.py
    - Centralized the imports in the compatibility layer
  4. Exception handling:
    - Created helpers for properly wrapping exceptions
    - Updated key methods to catch and wrap exceptions consistently
    - Fixed the Contract.check method to handle all exceptions
  5. Fixed old-style class checking for Python 2 compatibility
  6. Provided documentation:
    - Created a detailed changelog
    - Created an update guide for other projects
    - Provided a test script to verify the fixes

@eric-downes
Copy link
Author

I believe this resolves the following issues
#101 #72 #82

@Ajadaz
Copy link

Ajadaz commented Apr 10, 2025

There seems to be an error in setup.py. Tried installing through pip and got

  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [7 lines of output]
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "C:\Users\me\AppData\Local\Temp\pip-req-build-98z_4ogz\setup.py", line 71
          classifiers=[
          ^^^^^^^^^^^^^
      SyntaxError: keyword argument repeated: classifiers
      [end of output]

ran this on Python 3.11.2

@eric-downes
Copy link
Author

eric-downes commented Apr 10, 2025 via email

eric-downes and others added 9 commits April 12, 2025 00:34
- Converted yield-style tests to parameterized tests
- Updated requirements to remove nose dependency
- Added migration guide with patterns and examples
- Created migration tracking tools
- Fixed Python 3.12 compatibility issues in tests
- Updated documentation to mention pytest
- Add lessons learned from the migration process
- Include information about test coverage management
- Add section on migration tracking and tooling
- Add guidance for handling false positives in migration tracking
- Provide example code for migration tools
- Add comprehensive test suite for py_compatibility.py
- Add tests for utils.py, enabling.py and inspection.py
- Improve test coverage from 88% to 90%
- Update documentation with Python 3.12+ compatibility notes
- Add additional insights to nose-to-pytest migration guide
- Clean up migration artifacts

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants