KotlinIsland / basedmypy

Based Python static type checker with baseline, sane default settings and based typing features
Other
138 stars 4 forks source link

value with `Any` in union type does not narrow when assigned to variable with non-`Any` type #608

Open DetachHead opened 8 months ago

DetachHead commented 8 months ago

this does not happen in upstreamed mypy

Gist to reproduce

# mypy: allow-any-explicit=true

from typing import Any, cast

def any() -> Any: ...

foo: str = any() or "" # type:ignore[no-any-expr]

reveal_type(foo) # error: Expression type contains "Any" (has type "Any | str")

bar: str = any()

reveal_type(bar) # no error, revealed type is "str"
KotlinIsland commented 3 months ago

I'm not sure what you are expecting sorry. if you split the assgnments then basedmypy behaves the same as mypy

DetachHead commented 3 months ago

if assigning an Any to a variable that takes a str causes the variable to remain typed as a str, why does assigning an Any | str to a variable that takes a str change its type to Any | str?

KotlinIsland commented 3 months ago

it's an upstream defect that has been exposed via initial assignment.

playground