type-hinting

How to alias a type used in a class before it is declared?

How to alias a type used in a class before it is declared? Question: I have code similar to this example import typing class C(): def __init__(self, callback: typing.Callable[[C], int]): self._callback = callback def getCallback() -> typing.Callable[[C], int]: return self._callback class C2(): def __init__(self, cInstance: C): self._cInstance = cInstance def f() -> typing.NoReturn: self._cInstance.getCallback()(self.cInstance) and …

Total answers: 2

Type hints support for subclass of dict

Type hints support for subclass of dict Question: How can I implement a subclass of dict so it supports type hints like vanilla dict? I want to create a custom dict that does some extra work when modifying item, I implement it by subclassing the built-in dict class. After doing some googling I learned that …

Total answers: 1

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