Adapt and clarify tests and docs around explicitly annotated receiver parameters
The conformance tests currently include a test in protocols_generic.py which wants type checkers to accept assignability of ConcreteHasParent to the HasParent protocol:
T = TypeVar("T")
class HasParent(Protocol):
def get_parent(self: T) -> T:
...
class ConcreteHasParent:
def get_parent(self) -> Self:
return self
However, the get_parent method of the protocol seems to be more general than what ConcreteHasParent implements. The T type variable is unbounded, while the implicit Self annotation on the unannotated self received of ConcreteHasParent.get_parent should be bounded by ConcreteHasParent. The protocol should theoretically support calls like the following, through type[HasParent], whereas ConcreteHasParent would not:
def f(h: type[HasParent]):
h.get_parent("whatever")
The spec includes a paragraph that states:
Note that some type checkers may apply restrictions on [the use of an explicitly annotated receiver parameter], such as requiring an appropriate upper bound for the type variable used
On the other hand, no type checker seems to do so (pyright assumes an implicit upper bound, but that's not supported by that statement in the spec). Given that, I think it would be best to just define an upper bound for the type variable here.
I also fixed that problem (using an unbounded type variable for as an annotation on a receiver parameter) elsewhere in the tests and the documentation, often by using Self instead, if it seemed like the test was about something unrelated.