Skip to content
Advertisement

Tag: python-typing

python multiprocessing.Array typing

When creating an array, I want to put it inside a dataclass, but I cannot find the type of the returned object. arr = multiprocessing.RawArray(“i”, 2) If I do: but multiprocessing.sharedctypes.c_long_Array_2 does not exists. How can I use type hints, e.g arr: the_type with multiprocessing Array? UPDATE Pycharm example when using typing ctypes.c_long * 2, there’s still a value attribute

How to annotate that a function produces a dataclass?

Say you want to wrap the dataclass decorator like so: How should my_dataclass and/or something_else be annotated to indicate that the return type is a dataclass? See the following example on how the builtin @dataclass works but a custom @my_dataclass does not: Answer There is no feasible way to do this prior to PEP 681. A dataclass does not describe

Polymorphism and Type Hints in Python

Consider the following case: Let’s say I’m calling get_base_instance in a context where I kow it will return a Sub instance – maybe based on what args I’m passing. Now I want to pass the returned instance to do_something_with_sub: The problem is that my IDE complains about passing a Base instance to a method that only accepts a Sub instance.

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

Can the * (unpacking) operator be typed in Python? Or any other variadic args function such that all variadic types are in the result type?

Working with type stubs, I’m wondering if it’s possible to express a type in Python that allows you to type this correctly for any number of arguments: At first glance, I came with: But this of course will only type correctly the first T. Is the only possible way to write the overrides for all arities? This is not complete

How to overload a function with default parameters in Python

So I have the following (simplified) code which I don’t know how to get to typecheck. If I don’t add the overloads mypy complains that I might be indexing into a tuple with a str index. The as_tuple parameter defaults to false, so mypy should be able to infer that I’m using the first overload when not providing the second

Advertisement