Is there a way to type hint a function that takes as argument its return type? I naively tried to do this: but it doesn’t work. Answer Have a look at Generics, especially TypeVar. You can do something like this:
Tag: type-hinting
Pylance: “ClassVar” is not allowed in this context?
With the following function: I’m getting this error from Pylance in Visual Studio Code: “ClassVar” is not allowed in this context I’m using Python3.9 and Pylance v2021.10.0. In this example the Email class has a “make_stub” function, which has a _not_overriden attribute set to True. When I inspect a module for subclasses of that class I can use this to
Inheritable custom class constructor in python
How can I implement a custom constructor (class method) that is inheritable in python? The following minimized example might give an idea: mypy does not like the constructor I call when returning from from_float. I don’t know how to refer to class (Parent or Child) from the class method. Answer Pass the bound argument to TypeVar to specify that the
Return type based on type argument
I have this code, which type checks fine on its own: But if I try to use it: I get Incompatible return value type (got “Tuple[BaseObject, …]”, expected “Tuple[DerivedObject, …]”)mypy(error) I can fix this with But what I would really like to do is specify that df2objects returns a Tuple of object_type, like this: or this: Of course, neither of
Type hint for return value in subclass
I am writing a CustomEnum class in which I want to add some helper methods, that would then be available by the classes subclassing my CustomEnum. One of the methods is to return a random enum value, and this is where I am stuck. The function works as expected, but on the type-hinting side, I cannot figure out a way
How to add Python type hints to runtime type
How would you add type hints to this function? Answer Hmm it seems you can do it like this:
Define Python type hints in for-loop inside class body
I have the following code in Python: The type hints are used upon instantiation of MyDesign to dynamically add instances of the various Modules. I chose this syntax because the class MyDesign is really just a “template” defining what Modules it’s composed of, but the instantiation of modules needs some arguments only available when MyDesign is being instantiated. I would
How can request.param be annotated in indirect parametrization?
In the Indirect parametrization example I want to type hint request.param indicating a specific type, a str for example. The problem is since the argument to fixt must be the request fixture there seems to be no way to indicate what type the parameters passed through the “optional param attribute” should be (quoting the documentation). What are the alternatives? Perhaps
How do you type hint an additional attribute on an object?
I have a custom test runner that uses inspect to find all the functions starting with with test_. At one point I add an attribute to the function object. This is done so the test runner has access to the docstring information later. I’d really like to create a type hint for the list of functions so that my IDE
How to type hint a function’s optional return parameter?
How do I type hint an optional output parameter: — edit This seem to work: but that is so ugly and seems to overwhelm the fact that typically it will only return a simple float. Is that the correct way to do this? See answer below for correct way. — edit 2 This question was flagged as duplicate of that