File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -3135,3 +3135,26 @@ reveal_type(CONST) # N: Revealed type is "Literal['const']?"
31353135reveal_type(join(Foo.A, CONST)) # N: Revealed type is "builtins.str"
31363136reveal_type(join(CONST, Foo.A)) # N: Revealed type is "builtins.str"
31373137[builtins fixtures/tuple.pyi]
3138+
3139+
3140+ [case testLiteralRedundantCast]
3141+ # https://github.com/python/mypy/issues/19055
3142+ # flags: --warn-redundant-cast
3143+
3144+ from typing import Literal, cast
3145+
3146+ # "a" is an AnyOf[str, Literal["a"]], denoted Literal['a']?
3147+ # See: https://github.com/python/typing/issues/566
3148+
3149+ # This cast is not redundant because Literal["a"]? is not identical to Literal["a"]
3150+ LiteralOnly = Literal["a"]
3151+ reveal_type("a") # N: Revealed type is "Literal['a']?"
3152+ cast(LiteralOnly, "a")
3153+
3154+ # This cast is redundant because the type is already Literal["a"]
3155+ already_literal: Literal["a"] = "a"
3156+ reveal_type(already_literal) # N: Revealed type is "Literal['a']"
3157+ cast(LiteralOnly, already_literal) # E: Redundant cast to "Literal['a']"
3158+
3159+ LiteralUnion = Literal["a", "b"]
3160+ cast(LiteralUnion, "a")
You can’t perform that action at this time.
0 commit comments