Skip to content

Annotate 3.14+ ipaddress version / max_prefixlen as Literal - #16301

Open
ABruihler wants to merge 4 commits into
python:mainfrom
ABruihler:ipaddress-version-literal
Open

Annotate 3.14+ ipaddress version / max_prefixlen as Literal#16301
ABruihler wants to merge 4 commits into
python:mainfrom
ABruihler:ipaddress-version-literal

Conversation

@ABruihler

@ABruihler ABruihler commented Aug 26, 2026

Copy link
Copy Markdown

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).

ABruihler and others added 2 commits August 26, 2026 15:44
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>
@github-actions

This comment has been minimized.

ABruihler and others added 2 commits August 26, 2026 16:00
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>
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@ABruihler
ABruihler marked this pull request as ready for review August 26, 2026 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant