GeezerVison is showing github.com — Annotate 3.14+ ipaddress `version` / `max_prefixlen` as `Literal` by ABruihler · Pull Request #16301 · python/typeshed
Conversation
ABruihler
commented
Aug 26, 2026
•
edited by srittau
Loading
Uh oh!
There was an error while loading. Please reload this page.
On 3.14+, _BaseV4 / _BaseV6 declare version and max_prefixlen as bare Finals, so they infer
as Literal[4]? — an inferred literal that falls back to int — rather than a declared Literal[4].
.version therefore no longer narrows an IPv4Network | IPv6Network union, which it does on 3.13
where these are Literal-returning properties, and that narrowing is the only type-safe way to reach
subnet_of / supernet_of / address_exclude, since they take other: Self.
Annotate the literal types explicitly, with # noqa: PYI064 because the bare Final that rule
suggests is precisely the form that loses the declared literal (pre-commit.ci autofixed it away in
0752a11, reverted in 44bc4e1). Happy to drop Final and use plain Literal[4] instead if you
would rather not carry the noqas, or to take this to ruff as a rule bug first.
Agent used: Claude Code (Claude Opus 5).
2026-08-31 (@srittau): Deferred pending discussion in python/typing#2351.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Declared as bare `Final`, they infer as `int`, so `.version` no longer
narrows an `IPv4Network | IPv6Network` union the way the pre-3.14
`Literal`-returning properties did.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This comment has been minimized.
This reverts commit 0752a11. PYI064's autofix rewrites
`Final[Literal[4]] = 4` to a bare `Final`, which is exactly the form
that loses the declared literal type, so the fix undoes itself.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The rule's suggested bare `Final` is the form that loses the declared
literal type, so it cannot apply here.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
: Final = X is the pattern we generally use in typeshed. The typing spec says:
The typechecker should apply its usual type inference mechanisms to determine the type of ID (here, likely, int).
This text has been copied verbatim from PEP 591 This was released around the same the PEP 586 (Literals) was released, so this might simply be a case where two typing PEPs overlapped. This is supported by the following quote from the spec, a bit later:
Type checkers should treat uses of a final name that was initialized with a literal as if it was replaced by the literal.
(Also coming from PEP 591.)
The conformance tests also ensure that.
I've opened python/typing#2351, so let's wait for the feedback there.
|
ABruihler reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This branch has not been deployed
No deployments
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|