Add StrEnum compatibility layer for Python 3.10#4491
Merged
cidrblock merged 12 commits intoansible:mainfrom Jul 31, 2025
Merged
Add StrEnum compatibility layer for Python 3.10#4491cidrblock merged 12 commits intoansible:mainfrom
cidrblock merged 12 commits intoansible:mainfrom
Conversation
- 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
Contributor
There was a problem hiding this comment.
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
alisonlhart
approved these changes
Jul 30, 2025
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.
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:
The module includes appropriate noqa comments to handle linter warnings for the vendored code.