Replies: 1 comment
-
Pyright is working as designed here, so I don't consider this a bug. Static type checkers perform "type narrowing" of expressions based on type guards. Each expression is narrowed independently from other expressions. For example, the type guard For more details about type narrowing and type guards, refer to this documentation. The reason that mypy does not (currently) generate an error here is a quirk of mypy. In many circumstances, it uses a "join" operation rather than a "union" operation when evaluating types. In your code sample, it's "joining" the types Pyright uses unions rather than joins, so it evaluates the type in your example to be One issue here with your code is that you're violating the Liskov Substitution Principle (LSP) because the class ItemKindTwo(Item):
def __init__(self, kind: Kind | None = Kind.TWO) -> None:
super().__init__(kind) |
Beta Was this translation helpful? Give feedback.
-
Code sample in pyright playground
Pyright errors and says that
None
cannot be passed toItemKindTwo
. But ifkind is None
, thenitem_cls
must beItem
, notItemKindTwo
.Is it possible for Pyright to understand this? mypy seems to have no problem with it.
Beta Was this translation helpful? Give feedback.
All reactions