basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

narrow literal types

Open KotlinIsland opened this issue 1 year ago • 1 comments

a = "is this a joke"
reveal_type(a)  # str

b: str
assert b == "is this a joke"
reveal_type(b)  # str

KotlinIsland avatar Jul 23 '24 05:07 KotlinIsland

it should also narrow in cases like this:

from typing import Literal

value: str

assert value == "asdf" # not sure about this because __eq__ can be overridden
assert value is "asdf" # but this one should definitely work

foo: Literal["asdf"] = value # error: Incompatible types in assignment (expression has type "str", variable has type "Literal['asdf']") 

DetachHead avatar Jul 23 '24 05:07 DetachHead