type-hinting

How to make mypy like my protocols that work at runtime with runtime_checkable

How to make mypy like my protocols that work at runtime with runtime_checkable Question: I defined a couple of protocols like so: import json from typing import Any, Protocol, TypeVar, runtime_checkable T_co = TypeVar(‘T_co’, covariant=True) T = TypeVar(‘T’) @runtime_checkable class SupportsRead(Protocol[T_co]): def read(self, __length: int = …) -> T_co: … @runtime_checkable class SupportsWrite(Protocol[T_co]): def write(self, …

Total answers: 1

Overload type hint with Literal and Enum not working in PyCharm

Overload type hint with Literal and Enum not working in PyCharm Question: I want to typehint an overload function. For that I use the overload decorator from typing. I want to set multiple possible callees based on a parameter’s value. This parameter is color. I have this code: from typing import Literal, overload from enum …

Total answers: 1

How do I type hint for Enums in Python?

How do I type hint for Enums in Python? Question: I have a python function for which I want to use type hinting. There are two arguments. The first is any Enum class, the second optional arg is an element of that Enum. For example, say I have: class Foo(Enum): ALPHA = 1 BETA = …

Total answers: 1

Type hints for the generic utility in the Django

Type hints for the generic utility in the Django Question: def get_or_create_object(self, model : ?(what will be the type?), data: dict) -> ? (what will be the return type): try: obj, _ = model.objects.get_or_create(**data) except model.MultipleObjectsReturned: obj = model.objects.filter(**data).first() return obj get_or_create_object(ModelName, data) What will be the type hint here – as function will get …

Total answers: 2

type hint for generic attribute inside abstract class

type hint for generic attribute inside abstract class Question: I have a convoluted problem with types that I canĀ“t solve. Let’s say I have some abstract classes and a generic type equivalent to this from abc import ABC from typing import Generic, TypeVar class Action(ABC): pass ActionTypeVar = TypeVar("ActionTypeVar", bound="Action") class Agent(ABC, Generic[ActionTypeVar]): actions: tuple[ActionTypeVar, …

Total answers: 1

Specify return type of a wrapper function that calls an abstract method in Python

Specify return type of a wrapper function that calls an abstract method in Python Question: For this example, consider the simplified scenario where a Solver will return a Solution. We have Solutions: class Solution(ABC): pass class AnalyticalSolution(Solution): pass class NumericalSolution(Solution): def get_mesh_size(self) -> float: return 0.12345 And Solvers: class Solver(ABC): def solve(self, task: int) -> …

Total answers: 1

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