Skip to content

Y051: Flag redundant negative integer literals - #561

Merged
srittau merged 1 commit into
PyCQA:mainfrom
TohruSu:fix-y051-negative-int-literals
Nov 14, 2026
Merged

srittau merged 1 commit into
PyCQA:mainfrom
TohruSu:fix-y051-negative-int-literals

Conversation

@TohruSu

@TohruSu TohruSu commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

In Literal[-1], the -1 is parsed as a unary minus applied to a constant rather than as an ast.Constant, so the Y051 check skipped it. int | Literal[-1] and Union[Literal[-5], int] were not reported at all, and for int | Literal[-1, 5] the error pointed at Literal[5] instead of the first redundant member.

This makes the Y051 check treat a negated integer constant as an int literal. I added these cases to tests/union_duplicates.pyi, together with str | Literal[-1], which is still not flagged, and a changelog entry.

The new test cases fail without the change, and the full test suite passes with it on Python 3.11 on Windows; flake8, mypy (--python-version 3.10 and 3.15), black and isort are clean.

@srittau srittau left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@github-actions

Copy link
Copy Markdown

This change has no effect on typeshed. 🤖🎉

@srittau
srittau merged commit ca3933c into PyCQA:main Nov 14, 2026
23 checks passed
@TohruSu
TohruSu deleted the fix-y051-negative-int-literals branch November 14, 2026 23:32
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.

2 participants