python-typing Questions

3

I wonder how to make this getter more type-safe: VALUES = { '1': 'One', '2': 'Two', '3': 'Three' } def get(key : str) -> str: return VALUES[key] Instead of the type str I would love to ha...
Mezcaline asked 10/8, 2021 at 21:17

2

Solved

When speaking about Generics, Python gives the following example: from collections.abc import Sequence from typing import TypeVar T = TypeVar('T') # Declare type variable def first(l: Sequence[T]...
Divisionism asked 2/4, 2021 at 3:6

6

I would like to instantiate a typing Union of two classes derived from pydantic.BaseModel directly. However I get a TypeError: Cannot instantiate typing.Union. All examples I have seen declare Unio...
Zitella asked 7/1, 2020 at 17:27

6

Solved

I'm having a hard time understanding from the documentation exactly what typing.Annotated is good for and an even harder time finding explanations/examples outside the documentation. Or does it &qu...
Eldwon asked 17/4, 2022 at 1:23

2

Solved

My code looks something like this, which runs fine BDW without any errors from typing import Literal def verify(word: str) -> Literal['Hello XY']: a = 'Hello ' + word return a a = verify('XY...
Burl asked 4/10, 2021 at 11:16

3

Solved

So, I was writing an event emitter class using Python. Code currently looks like this: from typing import Callable, Generic, ParamSpec P = ParamSpec('P') class Event(Generic[P]): def __init__(se...
Euler asked 20/4, 2022 at 17:45

2

Based on the documentation for Python Typing, these are the ones that deal with Tuples... Tuple[()] - empty tuple Tuple[int, int] - tuple of two int objects Tuple[int, ...] - tuple of an arbitrar...
Disseisin asked 21/1, 2018 at 18:36

1

a variable annotated with Type[C] may accept values that are classes themselves How do you do this with TypeVarTuples (PEP 646) from typing import TypeVar, TypeVarTuple T = TypeVar("T") ...
Groundsill asked 1/5, 2023 at 23:55

7

Solved

This doesn't seem to work: from typing import NewType MyStr = NewType("MyStr", str) x = MyStr("Hello World") isinstance(x, MyStr) I don't even get False, but TypeError: isins...
Trolley asked 4/11, 2021 at 18:23

3

Solved

Say you want to wrap the dataclass decorator like so: from dataclasses import dataclass def something_else(klass): return klass def my_dataclass(klass): return something_else(dataclass(klass)) ...
Mutation asked 23/3, 2022 at 18:31

3

How can I specify one type for all of these callables: a(str) b(str, str) c(str, str, str) d(str, str, str, str) I found that I can specify Callable[..., None] in a general way, but how to specify...
Zita asked 26/8, 2019 at 13:28

2

Solved

I have the following in test.py: def f(x: int) -> float: pass if __name__=="__main__": f(4) When I run mypy --strict test.py, I get no errors. I expected mypy would be able to infe...
Thorathoracic asked 22/1, 2022 at 18:18

2

My type annotations I currently have look similar to the following, and I want to use an typing alias to not repeat myself so much: A class has class variables, which can be either: some specified...
Discriminate asked 26/5, 2019 at 21:14

2

Solved

Python's PEP 544 introduces typing.Protocol for structural subtyping, a.k.a. "static duck typing". In this PEP's section on Merging and extending protocols, it is stated that The general...
Wainscot asked 7/7, 2020 at 4:49

3

Solved

How can I automatically create the boilerplate code of pyi files? I want to create a pyi file for type hinting as described in pep484 which contains all method names. I don't want magic. I want to ...
Flywheel asked 24/2, 2016 at 12:42

2

Solved

I'm trying to use mypy in my projects, but many of the instance attributes I use are only initialized after __init__, and not inside it. However, I do want to keep the good practice of declaring al...
Bough asked 30/3, 2020 at 6:28

7

Solved

Assume you have a Python class that inherits from Generic[T]. Is there any way to get a hold of the actual type passed in from within the class/instance? For example, from typing import TypeVar, Ty...
Necropsy asked 29/8, 2019 at 8:32

3

Solved

Function Annotations: PEP-3107 Background: I am PyCharm user w/CPython 3.4x on Linux. I find it helps to annotate function parameters and return types. The IDE can better hint when I use these meth...
Hygienic asked 16/11, 2014 at 5:37

3

Solved

I am trying to import ArrayLike doing from numpy.typing import ArrayLike, and I get the error mentioned in the title: ModuleNotFoundError: No module named 'numpy.typing' I know I could simply write...
Cullen asked 16/4, 2021 at 1:10

3

Solved

I want to add typing to a Python function that takes in a type as an argument (actually, a subtype of a specific class), and return an instance of that type. Think a factory that takes in the speci...
Charron asked 25/5, 2021 at 9:41

5

Solved

I'm unsure of the Python convention for type hinting instance variables - I've been doing them within the __init__ constructor arguments like seen here: class LoggedVar(Generic[T]): def __init__(s...

3

Consider a function that performs type promotion, e.g. a simple multiplication of two numbers that can both be either int or float: def mul(a: int | float, b: int | float): # return type? return a...
Outcurve asked 28/11, 2023 at 11:50

3

Solved

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 ...
Salon asked 17/12, 2020 at 3:13

1

Python 3.12 has the type statement. How do I document the type alias properly? I tried: type Number = int | float """Represents a scalar number that is either an integer or float&quo...
Iconology asked 3/12, 2023 at 21:45

1

Say we have a function, for instance reduce, that takes in a callback, for instance combiner. If I have an example callback function, exampleReducer, I would like to avoid having to write the type ...
Bothwell asked 7/10, 2022 at 11:57

© 2022 - 2024 — McMap. All rights reserved.