typing

Type one of several classes

Type one of several classes Question: Let’s say I have the following two classes: class Literal: pass class Expr: pass class MyClass: def __init__(self, Type:OneOf(Literal, Expr)): pass How would I make the type one of the Expr or Literal class? The full example of what I’m trying to do is as follows: from enum import …

Total answers: 2

How to infer the type of last element passed to `*args` parameter in python?

How to infer the type of last element passed to `*args` parameter in python? Question: I want to write a pipeline function which takes an abritrary number of functions and forwards the input of the first to the second and so on up until the last. The output of pipeline is a Callable. The input …

Total answers: 2

How to use Dict to specify type-hint in Python

How to use Dict to specify type-hint in Python Question: I have a function which will return a dictionary like: from typing import Dict def foo() -> Dict[]: params = { "side": "Buy", "symbol": "BTCUSDT", "order_type": "Market", "time_in_force": "PostOnly", "reduce_only": False, "close_on_trigger": False, } return { "method": "POST", "api": "private/linear/order/create", "body": params } What should …

Total answers: 2

Optional typing for more than a single type

Optional typing for more than a single type Question: Since the documentation only uses Optional with a single type (Optional[X]), I was wondering if Union is required if I have an argument that accepts either a string, a list, or None. def func( arg: Optional[str, list] ) def func( arg: Optional[Union[str, list]] ) Asked By: …

Total answers: 1

How can I use ParamSpec with method decorators?

How can I use ParamSpec with method decorators? Question: I was following the example from PEP 0612 (last one in the Motivation section) to create a decorator that can add default parameters to a function. The problem is, the example provided only works for functions but not methods, because Concate doesn’t allow inserting self anywhere …

Total answers: 1

vector-like input – numpy – typing

vector-like input – numpy – typing Question: I have a problem with python typing of the function with vector-like (1-D) input, which will be looped. The input should be one of list, tuple, numpy.ndarray or similar. What is important I will be checking the length for the input. Thus the typing.Sequence should be perfectly suited. …

Total answers: 2

If the Python runtime ignores type hints, why do I get a TypeError?

If the Python runtime ignores type hints, why do I get a TypeError? Question: I already know that: The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc. but why does this code check the types for me? def …

Total answers: 1

Python typing: how to define a function or method?

Python typing: how to define a function or method? Question: I want to annotate my function using type hints. I am running into a problem where I am using a function as input parameters (or more specifically, a class-method), and I do not know how I should define the type hint. The code is as …

Total answers: 1