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,
Tag: typing
How to annotate a type that is a combination of multiple duck-types?
Let’s say I want to annotate the parameter to a function, and it should satisfy both typing.Sized and typing.Hashable (any other 2+ types could apply, I just picked these two for the sake of example). How would I annotate this? Given that one can combine types as an “or” using Sized | Hashable, I would expect something like: Sized &
How can I change the hinted return type of a function depending on the value of a parameter
I have a function where it usually returns an object that it searches for and performs some other actions. It raises an exception if it fails to find a match. Frequently, I don’t care if it finds a match or not, but not frequently enough that I’d consider removing the exception entirely. As such, the compromise I’ve made is to
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
How can you test that a python typing Protocol is a subclass of another Protocol?
The obvious solution to the problem is to use issubclass, but this raises TypeError (using Python 3.6.7), e.g. Answer For more on the topic of python Protocols, see https://mypy.readthedocs.io/en/latest/protocols.html#using-isinstance-with-protocols In Python 3.6.7, one way to solve this is to use the @runtime_checkable decorator:
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