Closed tpajenkamp-dspace closed 1 week ago
Pyright is working as intended here.
This is a limitation in how pyright implements value-constrained type variables. See this documentation for details.
I recommend avoiding value-constrained type variables. They are not well specified in the typing spec and have many limitations. They are not found in any other programming language — for good reason.
The recommended approach for your use case is to use an overload.
from typing import overload
@overload
def string_fun(arg: str) -> str: ...
@overload
def string_fun(arg: None) -> None: ...
def string_fun(arg: str | None) -> str | None:
if arg is None:
return None
return f"s: {arg}"
Describe the bug Pyright reports an error for functions like the one below which uses a
TypeVar
for both an argument and its return type whereNone
is one of the constraints. The type of the type variable is not properly narrowed (or whatever the correct term is in this case) by thearg is None
conditional. If I useisinstance(arg, types.NoneType)
instead it works, but this form does not seem idiomatic to me.Code
VS Code extension or command-line pyright 1.1.388
& 'C:\Python312\python.exe' -m pyright .