Skip to content
Advertisement

Tag: typing

Generic ParamSpec on Python 3.9

The following is correct in Python 3.10, but not 3.9: Running the above triggers a TypeError: What must I do to get this piece of code to run in Python 3.9? I get it that ParamSpec is a 3.10 feature. But typing-extensions is supposed to make the code backward-compatible, right? I’m guessing that I must change the syntax of Foo[[int,

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,

How do I assign the types for python-modules?

Here is an example with pygame (Types do not get inferred): Types do get inferred: Well, this library throws an error when importing single modules and using pygame.init(). Is there another way to use the first example and type the modules afterwards? Answer from pygame import * is the best thing to use in this situation. In your situation, init()

What for we call for the typing import List from the Python Standard Library?

I am solving some questions from the Leetcode: https://leetcode.com/problems/remove-duplicates-from-sorted-array/ I find the answers: Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length The answer for this question can be, too: I can’t find the answer to the question of why in the code should be applied typing. On the

Python: What is the typing signature for print?

What is the typing signature for print or similar function which takes a variable number of arguments of any type, when defined as a Callable parameter? Concretely, what is the declaration of output_function here? Update: clarified as a Callable parameter. Answer From PEP 484 Arbitrary argument lists can as well be type annotated, so that the definition: is acceptable and

Optional[Type[Foo]] raises TypeError in Python 3.5.2

This code: will raise TypeError on 3.5.2: whereas it runs fine on 3.6. Same problem if I spell out Optional as Union[None, Type[Foo]]. Is there any workaround for 3.5.2, while still accurately annotating the return type? Answer This is a bug in Python 3.5.2. Optional[cls] is a wrapper for Union[cls, type(None)], which uses __subclasses__() to establish whether one class is

Advertisement