Conformance: test @override checks on __init__ and __new__ by ashishpatel26 · Pull Request #2297 · python/typing
Closes #2222.
The @override section of the spec states that a method decorated with
@override "should [be treated] as a type error unless that method is
overriding a method or attribute in some ancestor class, and the type of the
overriding method is assignable to the type of the overridden method." There is
no exemption for __init__/__new__, so although constructors are normally
not checked against their parents, an explicit @override should still trigger
the assignability check.
This adds cases to tests/classes_override.py:
ChildC1:@override__init__/__new__that are assignable to the
parent — no error.ChildC2:@override__init__/__new__with an incompatible parameter
type — error (# E[init],# E[new]).ChildC3: incompatible__init__/__new__without@override— allowed,
to document the normal constructor exemption.
Scoring
Ran the conformance tool against the locked checker versions:
- pyrefly flags the incompatible constructor overrides → conformant.
- mypy, pyright, pycroscope, ty, zuban do not currently apply the
assignability check to__init__/__new__even with@override→ scored
Partialwith a note.
I marked the incompatible cases as mandatory errors (# E) since the spec text
is unconditional. If maintainers feel constructor override-checking should be
left optional, these could be relaxed to # E? and the five Partial scores
returned to Pass — happy to adjust.