Thanks. So from my reading of the PEP, type[C] is (in effect) a union of C and all its subclasses? If we think of it like that, then consider the following:
❯ bat -p .\tyex.py
class A:
def fn(self, i1: int) -> int:
return i1 + 3
class B:
def fn(self, s1: str, s2: str) -> str:
return s1 + s2
an_instance: A | B = A()
print(an_instance.fn(1))
❯ py .\tyex.py
4
❯ mypy .\tyex.py
tyex.py:10: error: Missing positional argument "s2" in call to "fn" of "B" [call-arg]
tyex.py:10: error: Argument 1 to "fn" of "B" has incompatible type "int"; expected "str" [arg-type]
Found 2 errors in 1 file (checked 1 source file)
That’s pretty bizarre behaviour. It’s very clear that an_instance has actual type A, and the method call is correctly typed. And both of classes A and B are entirely correct in themselves. It’s only calling fn, which is defined differently in the two classes, on a variable declared as a union type containing both of those classes, that could be considered invalid, and even then the type checker (in this instance, at least) should be perfectly capable of inferring the correct runtime type and validating that the usage is correct. But regardless of all this, it’s not hard to reason about the typing of this code.
I’m willing to believe that simply defining type[C] as “a union of C and all its subclasses” is a little simplistic in practice (in the sense that I assume there are nuances beyond simply “it’s easier to type” where type[C] is more useful in practice than the corresponding union) but given that’s basically what the PEP says, I find it difficult to see why __init__ doesn’t correspond precisely to fn in the above. And yes, that includes the fact that it’s incorrect to call obj.fn if all you know is that obj is of type A | B (i.e., it’s invalid to call cls(...) if all you know is that cls is of type type[A]).
Going back to the fundamental question, the following is invalid code:
class A:
def __init__(self, i1: int, i2: int):
self.i1 = i1
self.i2 = i2
class B(A):
def __init__(self, i1: int):
self.i1 = i1
self.i2 = 12
def dodgy(obj: A):
assume_its_an_a = type(obj)(1, 2)
dodgy(B())
It would be nice if type checkers could catch the error and report it, but the error is in the call in dodgy, not in the definitions of A and B, and that’s the important point here.
It would also be nice if the author of A had a way to say "subclasses must preserve the calling signature of __init__, if that’s a requirement they want to impose on subclasses (maybe so they can write code like dodgy without it actually being dodgy
). But it has to be opt-in, as A’s author might not have any intention of writing such code (in which case, why constrain subclasses unnecessarily?)
While this is broadly true (and is basically what the LSP says) the critical point is how we define “things you can do”. Python’s introspection capabilities let you get at things that are far wider than any sensible interpretation of “things you can do with an instance of a type”. And IMO, one of those capabilities is the ability to use type() to get an object that is the type of the instance.
If someone wants to explicitly define what “things you can do” (in the context of Python) fall under this rule, then that would be fine. But without such a precise definition, the LSP has to be just a design guideline, and not a hard and fast rule.