Gate the StrEnum fallback on sys.version_info instead of try/except - #769
Open
5point1 wants to merge 1 commit into
Open
Gate the StrEnum fallback on sys.version_info instead of try/except#7695point1 wants to merge 1 commit into
5point1 wants to merge 1 commit into
Conversation
Static type checkers prune sys.version_info branches (the idiom used throughout typeshed) but analyze both branches of a try/except ImportError, so the fallback class made every enum member's type ambiguous downstream. With ty, any enum member passed to a parameter annotated with its enum type was reported as invalid-argument-type. Runtime behavior is unchanged: enum.StrEnum exists on 3.11+, the fallback keeps covering 3.8-3.10. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
|
Thought: since < 3.11 stops being supported in October, should this package maybe follow that? |
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.
Changes included in this PR
Compat chore: replace the
try/except ImportErrorStrEnum fallback with asys.version_info >= (3, 11)gate in the threeenums.pymodules (v16, v201, v21).Current behavior
Static type checkers can't tell which branch of a
try/except ImportErrorwins, so theStrEnumbase — and with it the type of every enum member — is ambiguous to them. Checking a downstream codebase with ty reportsinvalid-argument-typeevery time an enum member is passed to a parameter annotated with its own enum type (e.g.RegistrationStatus.acceptedagainst astatus: RegistrationStatusfield).Minimal repro (ty 0.0.61)
The same snippet with
if sys.version_info >= (3, 11):passes cleanly.New behavior
None at runtime:
enum.StrEnumis still used on 3.11+, and the fallback still covers 3.8–3.10.sys.version_infogates are the idiom type checkers special-case and prune (it's what typeshed uses throughout), so enum members type correctly again. On a downstream CSMS project, ty went from 43 to 24 diagnostics with this patch — all 19 removed were these false positives.Impact
No breaking change; the import logic is identical at runtime. The
# pragma: no covermoves onto theelse:clause (ocpp/v16/enums.pystill reports 100% coverage locally), and the CI matrix already exercises both branches (3.8–3.10 take the fallback, 3.11–3.13 the stdlib import).Checklist
poetry install+ themake testscommands