Does this apply to return values as well? I suspect this is where the pain point is.

On Wed, 2021-06-02 at 15:05 +0000, Eric Traut wrote:
I find that these annotations are typically unnecessary, at least when using pyright or mypy. I'm not sure about other Python type checkers.

When pyright sees an unannotated `self` or `cls` parameter, it infers its type to be an internally-synthesized TypeVar that is bound to the parent class. If that method then returns `self` or `cls` or `cls()`, it infers the proper return type. It appears that mypy does something similar.

Because of this behavior, I rarely need to annotate `self` or `cls` explicitly in my code. It's still required in overloads or in stub files because the return type cannot be inferred without the function's implementation.

 -Eric

--

Eric Traut
Contributor to Pyright & Pylance
Microsoft Corp
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: pbryan@anode.ca