-
I was going to open an issue for this, but I realize that I might just be wrong about what to expect. It seems that Pyright is unable to infer the constraints from a generic type parameter when it is referred to outside of the class, unless you specify a concrete type for it. from abc import ABC, abstractmethod
class Currency(ABC):
@abstractmethod
def to_int(self) -> int:
return 0
class Account[BaseCurrency: Currency]:
def __init__(self, balance: BaseCurrency):
self.balance = balance
def get_balance(self) -> int:
return self.balance.to_int() # Works as expected
def get_balance(account: Account) -> int:
return account.balance.to_int() # to_int is "Unknown" to pyright This only works if you do |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Pyright is working correctly here. The This behavior is all prescribed by the Python typing spec, and pyright is conforming to the spec. |
Beta Was this translation helpful? Give feedback.
Pyright is working correctly here. The
Account
class is generic and accepts one type argument. In the functionget_balance
, you have annotated theaccount
parameter with the typeAccount
. Since you have omitted the type argument, type checkers will assumeAny
(or in the case of pyright,Unknown
, which is an implicitAny
). That means the instance variablebalance
has the typeUnknown
.This behavior is all prescribed by the Python typing spec, and pyright is conforming to the spec.