Skip to content
Advertisement

Tag: mypy

Annotate dataclass class variable with type value

We have a number of dataclasses representing various results with common ancestor Result. Each result then provides its data using its own subclass of ResultData. But we have trouble to annotate the case properly. We came up with following solution: but it stopped working lately with mypy error ClassVar cannot contain type variables [misc]. It is also against PEP 526,

mypy error on same line regardless of code content

I’m getting a mypy syntax error on line 36 in every single one of my files. If the file is shorter than 36 lines it just highlights the last line. The error is invariably the same: invalid syntax mypy(error) Apparently this issue may have happened before, but without a clear solution. Is this a known bug? How can I fix

Mypy: incompatible type error during set update

Mypy returns an error if the set is updated with new tuple using add() code.py error body As far as I know, it is common practice to add new tuplets to the set. The add() method can add a tuple object as an element in the set Why does mypy think it’s not allowed? Answer adgroups_by_campaign_id is marked as Dict[CampaignId,

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

Abstract base class using type of subclass in abstractmethods

I want to create an abstract base class with an abstract method that returns an instance of any subclass that implements the interface. Is there a more pythonic way to achieve this for mypy other than what I have created below? In my code example, I make the Animal class generic. Subclasses can inherit from Animal and specify the generic

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