Mypy has documentation for this under "advanced uses of self types".
https://mypy.readthedocs.io/en/stable/more_types.html#advanced-uses-of-self-...
Den tis 22 nov. 2022 16:41Eric Traut
I don't think the ability to annotate the `self` parameter is documented, but its behavior falls out naturally from the ability to annotate any other function parameter. The only thing that makes `self` special is that the corresponding argument is typically supplied through a binding operation (where an object instance is bound to the parameter as part of a method access) rather than passed directly via a call expression argument.
Once you declare `self` as type `HasX`, the type checker will enforce that type. So it's correct for a type checker to emit an error in your `double_x_plus_z` example above.
It's not safe to assume that `self` also has the type `Self` because an instance method can be invoked on the class directly, and the caller can pass an argument for `self`. For example, `Base.double_x_plus_z(has_x_instance)`.
It sounds like what you want is an intersection type — a way to declare that `self` is both type `HasX` and `Self`. There is some work underway to determine whether it's feasible to add intersection types to the type system.
-- Eric Traut Contributor to Pyright & Pylance Microsoft _______________________________________________ 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: git@antonagestam.se