Skip to content

Add StrEnum compatibility layer for Python 3.10#4491

Merged
cidrblock merged 12 commits intoansible:mainfrom
cidrblock:chore_compat
Jul 31, 2025
Merged

Add StrEnum compatibility layer for Python 3.10#4491
cidrblock merged 12 commits intoansible:mainfrom
cidrblock:chore_compat

Conversation

@cidrblock
Copy link
Copy Markdown

This PR adds a compatibility module that vendors Python 3.11's enum.StrEnum for use on Python 3.10.

The implementation is an exact copy of Python 3.11's StrEnum source code with no modifications. This provides identical behavior across Python versions and eliminates the need for version-specific logic.

This compatibility layer is needed for upcoming work that will utilize StrEnum functionality while maintaining support for Python 3.10.

Changes:

  • src/molecule/compatibility.py: Vendored Python 3.11 StrEnum implementation
  • tests/unit/test_compatibility.py: Comprehensive test suite covering string behavior, formatting, auto() functionality, and cross-version compatibility
  • All tests pass on Python 3.10 and 3.13

The module includes appropriate noqa comments to handle linter warnings for the vendored code.

- Add src/molecule/compatibility.py with StrEnum compatibility for Python 3.10+
- Use built-in enum.StrEnum on Python 3.11+ when available
- Provide custom _StrEnum implementation for Python 3.10 with list() and from_str() methods
- Add comprehensive test suite in tests/unit/test_compatibility.py
- Tests pass on both Python 3.10 (all 10 tests) and Python 3.13 (7 tests, 3 skipped)
- Proper version detection and graceful fallback behavior
- 95% code coverage on Python 3.10, validates both implementations
- Add _generate_next_value_ to support auto() producing lowercase member names
- Override __str__ and __format__ to use str.__str__ and str.__format__ for compatibility
- Add comprehensive tests for auto() behavior and string formatting
- Maintain backward compatibility while matching Python 3.11 StrEnum behavior
- Keep custom list() and from_str() methods as useful extensions

This brings our Python 3.10 implementation much closer to the official
Python 3.11 enum.StrEnum behavior while preserving useful enhancements.
Replace complex compatibility layer with direct copy of Python 3.11s StrEnum:
- 15 lines vs 67 lines (78% reduction)
- Identical behavior across all Python versions
- Zero compatibility issues
- Simplified maintenance and testing
- No version detection or conditional imports needed

This vendored approach is cleaner and matches exactly what users will
get when they upgrade to Python 3.11+, ensuring perfect compatibility.
- Use exact copy of Python 3.11 enum.StrEnum source code
- Add file-level noqa comments to disable linter warnings
- No type annotations or custom modifications
- 100% identical behavior to Python 3.11 built-in
- Clean 15-line implementation with zero compatibility issues
- Tests pass on both Python 3.10 and 3.13
- Exact copy of Python 3.11 enum.StrEnum implementation
- All 11 tests pass on Python 3.10 and 3.13
- File-level noqa comments for ruff, pylint, mypy
- Functional vendored implementation despite remaining pydoclint warnings
- Ready for production use
- Rename TestCompatEnum to CompatEnum to avoid pytest collection warnings
- Rename TestAutoEnum to AutoEnum
- Add pydoclint disable for compatibility module
- All 11 tests pass cleanly without pytest warnings
- File-level noqa comments for test files to silence remaining linter errors
- Clean up compatibility module and tests
- Keep essential noqa comments for ruff, mypy, pylint
- Vendored StrEnum implementation ready for use
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a compatibility layer that vendors Python 3.11's enum.StrEnum for use on Python 3.10, enabling consistent string enumeration behavior across Python versions while maintaining backward compatibility.

  • Implements an exact copy of Python 3.11's StrEnum functionality for Python 3.10 compatibility
  • Provides comprehensive test coverage for string behavior, formatting, auto() functionality, and cross-version compatibility
  • Updates documentation baseline to account for linter warnings in vendored code

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/molecule/compatibility.py Vendored Python 3.11 StrEnum implementation with noqa comments for linter warnings
tests/unit/test_compatibility.py Comprehensive test suite covering StrEnum string behavior, formatting, and compatibility
.config/pydoclint-baseline.txt Updated baseline to suppress documentation linter warnings for vendored code and test enums

- Address reviewer feedback by using proper enum.auto() instead of calling _generate_next_value_ directly
- Fix test to properly validate StrEnum __new__ behavior with type conversion
- All 10 tests pass on both Python 3.10 and 3.13
- Keep vendored StrEnum implementation unchanged to maintain exact Python 3.11 compatibility
@cidrblock cidrblock closed this Jul 31, 2025
@cidrblock cidrblock reopened this Jul 31, 2025
@cidrblock cidrblock merged commit ae840fa into ansible:main Jul 31, 2025
51 of 96 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants