pyright icon indicating copy to clipboard operation
pyright copied to clipboard

Bound generic property types not inferred

Open vMarcelino opened this issue 6 years ago • 2 comments

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"]

vMarcelino avatar May 11 '20 13:05 vMarcelino

Thanks for the bug report!

This will be fixed in the next version of pyright.

erictraut avatar May 12 '20 06:05 erictraut

This is now addressed in Pyright 1.1.37, which I just published.

erictraut avatar May 16 '20 16:05 erictraut