python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.27k stars 2.79k forks source link

(🐞) no error when can't able to infer type argument and becomes `Never` #17412

Open KotlinIsland opened 3 months ago

KotlinIsland commented 3 months ago
# Some stub
from typing import TypeVar

T = TypeVar("T")

def something(x: T = ...) -> T: 
    """x, the parameter for everything (formally Twitter)"""

# some other file
f: object
f = something()
reveal_type(f)  # Never
if f:
    print("hi")  # unreachable
print("hi")  # unreachable

We have an interesting array of bugs on display here, but most importantly I think, is that the call to something doesn't report that T can't be inferred.

Seeing as f is object here, I would expect T to be inferred as object