Inheriting Overloaded Signatures #1254
-
PreambleI have learned from that I've also learned there has been considerable discussion (mostly indirectly; initially stemmed from callback protocols) on the topic of trying to develop a way for users to declare that subclasses inherit the methods signatures of their parents:
I am trying to absorb all the information here. Although I thought I knew type hinting well, I'm realizing there is still so much I that don't know. I'd rather not open up another Issue unnecessarily and complicate matters further, but I am still confused and have some questions. Questions
BenefitsTo reiterate what has already been stated, this would be nice to have for the following reasons:
ResearchFrom what I have gathered, the following approaches have been suggested: Sourcefrom typing import Any, Callable, TypeVar
Ts = TypeVar('Ts')
# Implementation
def copy_type(f: Ts) -> Callable[[Any], Ts]:
return lambda x: x
# Example Usage
@copy_type(open)
def backup_open(file: Any, *args: Any, **kwargs: Any) -> Any: ... Pitfalls:
Thoughts?Perhaps a The |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I consider overloads an advanced feature. Very few Python developers, even among those who regularly use type annotations in their code, use (or even know about) Overriding an overloaded method is even more uncommon. I can remember needing to do this only twice in as many years. So I don't think that overridden overloaded functions is a common problem. The decision of whether to inherit type declarations (of attributes or methods) from a base class is not currently standardized, and behaviors vary from one type checker to the next. Some of this is rooted in the somewhat different philosophies of the type checkers. For example, mypy doesn't type check any functions that lack type annotations. Pyright always type checks all functions based on whatever available type information is present. If pyright encounters a method whose input parameters are not annotated, it will use types from a base class method of the same name, but only if the number and names of the parameters matches. Pylance (the Python language based on pyright) also supports completions when defining a new method within a derived class. If you start typing a method definition, it will offer to fill in the remainder of the signature (including type annotations) based on the base class method definition. It does not offer to complete overloads, however. To my knowledge, no pylance user has ever asked for completion of overloads, which further underscores my assertion above that this is a rare occurrence. |
Beta Was this translation helpful? Give feedback.
I consider overloads an advanced feature. Very few Python developers, even among those who regularly use type annotations in their code, use (or even know about)
@overload
. Overloads are most useful for annotating legacy libraries that have polymorphic functions and methods. Developers who are annotating their own code or developing new libraries with static typing in mind tend to use functional polymorphism less frequently because typing is much more top of mind when they are writing the code. So I don't know that this is a big issue.Overriding an overloaded method is even more uncommon. I can remember needing to do this only twice in as many years. So I don't think that overridden over…