Description
Let's suppose I have a base class Base
with a method foo()
, and a subclass Sub
that raises an Exception
if foo()
is called. I'd like it so that the type checker flags an error on Sub.foo()
, but not Base.foo()
, so I tried this:
from typing_extensions import Never
class Base:
def __init__(self):
pass
def foo(self) -> None:
print("foo")
def goo(self, x: int) -> None;
print("goo ", x)
class Sub(Base):
def foo(self: Never) -> None:
raise Exception("never")
def goo(self, x: Never) -> None:
raise Exception("never")
x = Sub()
x.foo()
x.goo(3)
With pyright 1.1.294 and python 3.10, I get the following:
neversub.py:16:19 - error: Type of parameter "self" must be a supertype of its class "Sub" (reportGeneralTypeIssues)
neversub.py:24:3 - error: Could not bind method "foo" because "Sub" is not assignable to parameter "self"
Type "Sub" cannot be assigned to type "Never" (reportGeneralTypeIssues)
neversub.py:25:7 - error: Argument of type "Literal[3]" cannot be assigned to parameter "x" of type "Never" in function "goo"
Type "Literal[3]" cannot be assigned to type "Never" (reportGeneralTypeIssues)```
The last two errors are exactly what I want. But while the first error makes sense, aside from using a #pyright: ignore
, I'm not sure how this should be declared to avoid having to use #pyright: ignore
. In other words, what is the right way to indicate that a method with zero parameters is not valid to call in a subclass?
I should note that with the method goo()
, since it has a required argument, I can use the Never
declaration to show that this is invalid. What's not clear is how do you indicate that a method with no arguments is invalid without having to use #pyright: ignore
?