basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

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

Open DetachHead opened this issue 1 year ago • 3 comments

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"

DetachHead avatar Jan 08 '24 22:01 DetachHead

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

KotlinIsland avatar Jun 22 '24 13:06 KotlinIsland

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?

DetachHead avatar Jun 23 '24 00:06 DetachHead

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

playground

KotlinIsland avatar Jun 23 '24 06:06 KotlinIsland