pyright
pyright copied to clipboard
Bound generic property types not inferred
The correct type is not inferred when using a bounded generic type in a property note: may be related to #383
To Reproduce
from typing import TypeVar, Generic
T = TypeVar('T', bound=int)
class Foo(Generic[T]):
def __init__(self, bar: T):
self._bar = bar
@property
def bar(self) -> T:
return self._bar
def bar_method(self) -> T:
return self._bar
class NewInt(int):
def new_thing(self):
pass
class FooNewInt(Foo[NewInt]):
def fizz(self) -> None:
reveal_type(self.bar) # Type of "self.bar" is "TypeVar["T"]"
reveal_type(self.bar_method()) # Type of "self.bar_method()" is "NewInt"
self.bar.new_thing() # <-- error
self.bar_method().new_thing() # ok
# error message:
# Cannot access member "new_thing" for type "int"
# Member "new_thing" is unknown
Expected behavior
FooNewInt(NewInt()).bar should be of type NewInt instead of TypeVar["T"]
Thanks for the bug report!
This will be fixed in the next version of pyright.
This is now addressed in Pyright 1.1.37, which I just published.