Repository navigation
fix(Y062): report duplicates inside nested Literals - #556
Open
Cherith1222 wants to merge 2 commits into
Open
Cherith1222 wants to merge 2 commits into
Cherith1222 wants to merge 2 commits into
Conversation
Literal[1, Literal[2, 1]] is silent. Two identical nested Literals are reported as a duplicate subscript rather than a duplicate value.
PEP 586 flattens a nested Literal, so duplicate detection has to unwrap those slices. The outer slice reports each duplicated value once.
|
This change has no effect on typeshed. 🤖🎉 |
srittau
requested changes
Nov 9, 2026
| | <a id="Y060" href="#Y060">Y060</a> | Redundant inheritance from `Generic[]`. For example, `class Foo(Iterable[_T], Generic[_T]): ...` can be written more simply as `class Foo(Iterable[_T]): ...`.<br><br>To avoid false-positive errors, and to avoid complexity in the implementation, this check is deliberately conservative: it only flags classes where all subscripted bases have identical code inside their subscript slices. | Style | ||
| | <a id="Y061" href="#Y061">Y061</a> | Do not use `None` inside a `Literal[]` slice. For example, use `Literal["foo"] \| None` instead of `Literal["foo", None]`. While both are legal according to [PEP 586](https://peps.python.org/pep-0586/), the former is preferred for stylistic consistency. Note that this warning is not emitted if Y062 is emitted for the same `Literal[]` slice. For example, `Literal[None, None, True, True]` only causes Y062 to be emitted. | Style | ||
| | <a id="Y062" href="#Y062">Y062</a> | `Literal[]` slices shouldn't contain duplicates, e.g. `Literal[True, True]` is not allowed. | Redundant code | ||
| | <a id="Y062" href="#Y062">Y062</a> | `Literal[]` slices shouldn't contain duplicates, e.g. `Literal[True, True]` is not allowed. Nested `Literal` slices are flattened first, so `Literal[1, Literal[2, 1]]` is checked as `Literal[1, 2, 1]`. | Redundant code |
Member
There was a problem hiding this comment.
Let's keep this text user-focused. Maybe just "Literal[] slices shouldn't contain duplicates, e.g. Literal[True, True] or Literal[True, Literal[True]] is not allowed."
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.
Why
Y062 compares the direct members of one
Literalslice. A nestedLiteralcounts as a single member, soLiteral[1, Literal[2, 1]]does not report the repeated1. The check now unwraps nestedLiteralslices before it looks for duplicates. See #485.What changed
Literalvalues, includingtyping.Literal.Literalnested inside another type, such asAnnotated, still checks itself.Scope
This covers duplicate values inside nested
Literalslices. It does not add a new error for a nestedLiteralthat has no duplicate, such asLiteral[1, Literal[2, 3]].Tradeoffs
A nested
Literalwith no repeated value could also be rewritten as one flat slice. This change only reports duplicates, which is what Y062 already does for a flat slice.Blast Radius
Stub files that repeat a value inside a nested
Literalnow get Y062. Slices that were already flat stay the same.Verification
Literal[1, Literal[2, 1]], and it reportedLiteral[Literal[1], Literal[1]]as a duplicateLiteral[1].1.Literal[1, Literal[2, 3]]stays quiet.uv run --python 3.13 --group=dev pytest -qreported 37 passed.flake8_pyiandtests/test_pyi_files.pyproduced no findings.Drafted with Cursor.