pyright icon indicating copy to clipboard operation
pyright copied to clipboard

Inherited self-annotated `__init__` overload does not solve a generic subclass's type parameters

Open UnknownPlatypus opened this issue 3 weeks ago • 7 comments

Describe the bug

A self-annotated __init__ overload solves the type parameters of the class that declares it, but not those of a generic subclass that inherits it. The overload is still selected for the subclass, but its type parameters stay unsolved.

The typing spec says a type checker "should bind the class being constructed to this parameter" (https://typing.python.org/en/latest/spec/constructors.html#init-method), which solves them through the subclass's base class expression. ty and pyrefly do this. mypy has the same gap: python/mypy#22395.

Found in django-stubs, where every field subclass repeats its parent's overloads to work around this: https://github.com/typeddjango/django-stubs/pull/3763

Code or Screenshots

from typing import Generic, Literal, overload
from typing_extensions import TypeVar

_S = TypeVar("_S", contravariant=True)
_G = TypeVar("_G", covariant=True)


class Base(Generic[_S, _G]):
    @overload
    def __init__(self, *, null: Literal[False] = False) -> None: ...
    @overload
    def __init__(self: "Base[int | None, int | None]", *, null: Literal[True]) -> None: ...
    def __init__(self, *, null: bool = False) -> None: ...


class Sub(Base[_S, _G]): ...


reveal_type(Base(null=True))
reveal_type(Sub(null=True))

Output:

information: Type of "Base(null=True)" is "Base[int | None, int | None]"
information: Type of "Sub(null=True)" is "Sub"

Expected the second line to be Sub[int | None, int | None].

Adding default=int to both type variables changes the second line to Sub[int, int], which shows the overload is matched but its self annotation never reaches the subclass's type parameters.

VS Code extension or command-line

Command-line, pyright 1.1.414, Python 3.15.2.

UnknownPlatypus avatar Nov 07 '26 23:11 UnknownPlatypus

The typing spec doesn't say how an inherited __init__ with an annotated self specializes a generic subclass. The constructors chapter (which you linked) and its conformance tests only cover the class that declares the __init__. That's why the checkers split: pyright and mypy leave the subclass's type parameters unsolved, while ty and pyrefly solve them by mapping the annotation through the subclass's base class.

That mapping can't reach a type parameter the subclass adds, because an unspecialized constructor call has nothing to determine it from:

class Sub[A, B, C](Base[C, B]): ...

reveal_type(Sub(null=True))
  • pyright: Sub[Unknown, Unknown, Unknown]
  • mypy: Sub[Never, Never, Never]
  • ty: Sub[Unknown, int | None, int | None]
  • pyrefly: Sub[@_, int | None, int | None]

ty and pyrefly solve B and C, but the self annotation on Base.__init__ can't say anything about A, so the call still produces a partially specialized Sub. No argument can supply A either, so it stays unknowable. If a self annotation is supposed to fully specialize the class being constructed, then applying it to subclasses can only guarantee that for the type parameters they forward to Base, and that makes it hard to justify applying it to them at all.

Since the spec leaves this open, I'd recommend raising it on discuss.python.org, so it can be settled for all type checkers rather than in each one's tracker.

crypt0lith avatar Nov 08 '26 07:11 crypt0lith

Thanks for the answer, I'm not super familiar with the typing spec process but yeah I suppose it would be nice to have an added test in https://github.com/python/typing/blob/main/conformance/tests/constructors_call_init.py for this.

Maybe something like that ?

# > This step should be skipped if the class does not define an __init__ method
# > and does not inherit an __init__ method from a base class other than object.


class Class5Sub(Class5[T]): ...


assert_type(Class5Sub(0), Class5Sub[list[int]])
assert_type(Class5Sub(""), Class5Sub[set[str]])
assert_type(Class5Sub(3.0), Class5Sub[float])
assert_type(Class5Sub[int](3), Class5Sub[int])
Class5Sub[int]("")  # E

Should I open an issue in https://github.com/python/typing/issues or a discussion in https://discuss.python.org/c/typing/32 ?

UnknownPlatypus avatar Nov 08 '26 08:11 UnknownPlatypus

Should I open an issue in https://github.com/python/typing/issues or a discussion in https://discuss.python.org/c/typing/32 ?

A discussion. The Typing Council's process has substantive spec changes discussed on discuss.python.org first, then proposed as a spec PR to python/typing and submitted to the council for a decision. The conformance suite tests behavior the spec defines, so a test for this would follow that decision rather than precede it.

The passage you quoted says only that an inherited __init__ is evaluated, not how its self annotation specializes the subclass. Your test also covers only a subclass that forwards its type parameters unchanged; the discussion would need to settle the case where the subclass adds one, as in the example above.

crypt0lith avatar Nov 08 '26 08:11 crypt0lith

Thanks! will do when I find some time

UnknownPlatypus avatar Nov 08 '26 09:11 UnknownPlatypus

Thanks! will do when I find some time

No need to worry about it, I'll open the discussion and link it here. It'll cover how pyright, mypy, ty and pyrefly currently handle this case, and #11991, which implements it in pyright. That PR stays a draft until the Typing Council decides.

crypt0lith avatar Nov 09 '26 15:11 crypt0lith

Ok that discussion is up here. Your typeddjango/django-stubs#3763 is cited as the motivation for it.

crypt0lith avatar Nov 09 '26 19:11 crypt0lith

Thanks for that, this is awesome !

UnknownPlatypus avatar Nov 10 '26 01:11 UnknownPlatypus