-
Consider the following: from typing import assert_type, Callable
def func1(x: object):
if x is str:
assert_type(x, type[str])
def func2(x: Callable[..., object]):
if x is str:
assert_type(x, type[str]) Analysing this file, Pyright 1.1.380 CLI produces the following output:
Pyright narrows the type in the first function, but not in the second function. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I notice also that both from typing import assert_type, Callable
from typing_extensions import TypeGuard
def is_str_cls(x: object) -> TypeGuard[type[str]]:
...
def func2(x: Callable[..., object]):
if is_str_cls(x):
assert_type(x, type[str]) |
Beta Was this translation helpful? Give feedback.
-
Pyright's narrowing logic for the For what it's worth, mypy doesn't handle type narrowing for either of the |
Beta Was this translation helpful? Give feedback.
Pyright's narrowing logic for the
x is y
type guard pattern doesn't currently handle the case wherey
is a class object whose constructor matches the callable type ofx
. I don't consider this a bug, but it is a missing feature. I think this is a reasonable enhancement request if you'd like to file one.For what it's worth, mypy doesn't handle type narrowing for either of the
x is str
code samples above.