Skip to content
Advertisement

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

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

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 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

Advertisement