type-hinting

Type annotations for full class hierarchy in Python

Type annotations for full class hierarchy in Python Question: Suppose we have the following code: class Base: a: int class Derived(Base): b: int print(Derived.__annotations__) Running this script in all recent versions of python will print {‘b’: <class ‘int’>} (that is, the class members we explicitly defined in Derived. In python 3.10, using inspect.get_annotations(Derived) will result …

Total answers: 1

How do I read extended annotations from Annotated?

How do I read extended annotations from Annotated? Question: PEP 593 added extended annotations via Annotated. But neither the PEP nor the documentation for Annotated describes how we’re supposed to access the underlying annotations. How are we supposed to read the extra annotations stored in the Annotated object? from typing import Annotated class Foo: bar: …

Total answers: 2

Python define return type based on property type

Python define return type based on property type Question: I have three classes, one base class and two child classes with the following structure: class BaseLayout: def add_element(self: "BaseLayout", element: str) -> None: … class LayoutA(BaseLayout): def add_element(self: "BaseLayout", element: str, name: str) -> None: … class LayoutB(BaseLayout): def add_element(self: "BaseLayout", element: str, number: int) …

Total answers: 1

Python typing dict of dicts

Python typing dict of dicts Question: How do I correctly type hint this python structure: people={ "john" : {"likes": "apples", "dislikes": "organges"}, "aisha": {"likes": "kittens", "dislikes": "ravens"} } EDIT: There can be any keys specified – e.g. "mary","joseph", "carl"… I understand that the value-dict can be typed as such class _Preferences(TypedDict): likes: str dislikes: str …

Total answers: 2

Python method return hint for method that can either return a tuple or single value

Python method return hint for method that can either return a tuple or single value Question: Inside my class BatterSimulation I have a static method that either returns a Tuple[pd.Dataframe, np.array] or returns just the dataframe depending on if I’m using the method internally in the class or not. @staticmethod def transform_schedule(schedule: List[dict], time_step: int, …

Total answers: 1

How can I ensure type hints still work for my class that inherits UserList from collections?

How can I ensure type hints still work for my class that inherits UserList from collections? Question: I have a class that adds something on top of standard list behavior. To avoid re-implementing everything, I used UserList: from collections import UserList class MyList(UserList): …stuff… But now, when I do something like: my_list = MyList([1,2,3,4]) My …

Total answers: 1

How to annotate parent classmethod so that child methods return an instance of themselves?

How to annotate parent classmethod so that child methods return an instance of themselves? Question: I have a parent class BaseBlob that has several child classes LimitedProofId, ProofId, TxId. The parent class implement a deserialize class method that should return an instance of itself. I also have a Delegation class that takes a LimitedProofId. I …

Total answers: 2

Instantiate empty type-hinted list

Instantiate empty type-hinted list Question: I have the following code: from typing import List, NewType MultiList = NewType("MultiList", List[List[int]]) def myfunc(): multi: MultiList = [] # More stuff here The code works fine, it’s just my IDE (PyCharm) doesn’t like the instantiation of multi to an empty list, I get this error: “Expected type ‘MultiList’, …

Total answers: 2