Skip to content

Commit cb6a089

Browse files
added test for #19605
1 parent 110641f commit cb6a089

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

test-data/unit/check-literal.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3135,3 +3135,26 @@ reveal_type(CONST) # N: Revealed type is "Literal['const']?"
31353135
reveal_type(join(Foo.A, CONST)) # N: Revealed type is "builtins.str"
31363136
reveal_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")

0 commit comments

Comments
 (0)