Annotate 3.14+ ipaddress version / max_prefixlen as Literal - #16301
Annotate 3.14+ ipaddress version / max_prefixlen as Literal#16301ABruihler wants to merge 4 commits into
version / max_prefixlen as Literal#16301Conversation
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) <noreply@anthropic.com>
This comment has been minimized.
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) <noreply@anthropic.com>
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) <noreply@anthropic.com>
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
|
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:
(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. |
On 3.14+,
_BaseV4/_BaseV6declareversionandmax_prefixlenas bareFinals, so they inferas
Literal[4]?— an inferred literal that falls back toint— rather than a declaredLiteral[4]..versiontherefore no longer narrows anIPv4Network | IPv6Networkunion, which it does on 3.13where these are
Literal-returning properties, and that narrowing is the only type-safe way to reachsubnet_of/supernet_of/address_exclude, since they takeother: Self.Annotate the literal types explicitly, with
# noqa: PYI064because the bareFinalthat rulesuggests is precisely the form that loses the declared literal (pre-commit.ci autofixed it away in
0752a11, reverted in 44bc4e1). Happy to drop
Finaland use plainLiteral[4]instead if youwould 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.