File tree Expand file tree Collapse file tree 2 files changed +22
-1
lines changed
Expand file tree Collapse file tree 2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -2917,7 +2917,7 @@ match m[k]:
29172917
29182918match 0:
29192919 case 0 as i:
2920- reveal_type(i) # N: Revealed type is "Literal[0]"
2920+ reveal_type(i) # N: Revealed type is "Literal[0]? "
29212921 case int(i):
29222922 i # E: Statement is unreachable
29232923 case other:
Original file line number Diff line number Diff line change @@ -49,6 +49,27 @@ from typing import cast
4949a = 1
5050b = cast(object, 1)
5151
52+ [case testLiteralRedundantCast]
53+ # https://github.com/python/mypy/issues/19055
54+ # flags: --warn-redundant-cast
55+
56+ from typing import Literal, cast
57+
58+ # "a" is an AnyOf[str, Literal["a"]], denoted Literal['a']?
59+ # See: https://github.com/python/typing/issues/566
60+
61+ # This cast is not redundant because Literal["a"]? is not identical to Literal["a"]
62+ LiteralOnly = Literal["a"]
63+ reveal_type("a") # N: Revealed type is "Literal['a']?"
64+ cast(LiteralOnly, "a")
65+
66+ # This cast is redundant because the type is already Literal["a"]
67+ already_literal: Literal["a"] = "a"
68+ reveal_type(already_literal) # N: Revealed type is "Literal['a']"
69+ cast(LiteralOnly, already_literal) # E: Redundant cast to "Literal['a']"
70+
71+ LiteralUnion = Literal["a", "b"]
72+ cast(LiteralUnion, "a")
5273
5374[case testCastFromUnionOfAnyOk]
5475# flags: --warn-redundant-casts
You can’t perform that action at this time.
0 commit comments