python-typing

Infer return type annotation from other function's annotation

Infer return type annotation from other function's annotation Question: I have a function with a complex return type annotation: from typing import (Union, List) # The -> Union[…] is actually longer than this example: def parse(what: str) -> Union[None, int, bool, float, complex, List[int]]: # do stuff and return the object as needed def parse_from_something(which: …

Total answers: 3

MyPy linting conflicting typing with typing.Self

MyPy linting conflicting typing with typing.Self Question: During linting and rewriting of an immutable class, operations do not pass the mypy checker. The class implements operations such as__add__, however, every function that returns a new instance of the same class triggers a typing error Incompatible return value type (got "Point", expected "Self") [return-value]mypy(error) Methods I’ve …

Total answers: 1

Typing to mark return values of current class type or any of its subclasses

Typing to mark return values of current class type or any of its subclasses Question: I want to make sure that the from_dict in the following method works well in its subclasses as well. Currently, its typing does not work (mypy error "Incompatible return value type"). I think because the subclass is returning an instance …

Total answers: 1

how to type python API responses similar to TS `as`?

how to type python API responses similar to TS `as`? Question: I’m using a lib that types its response as -> (Unknown | Response | Any) If i know/expect the response to be a Response and that is has an id field, how can I cast that in my code? Typescript provides an as <type> …

Total answers: 1

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

adding __getitem__ accessor to Python class method

adding __getitem__ accessor to Python class method Question: I’m attempting to add an item getter (__getitem__, to provide the [] syntax) to a class method so that I can use some unique-ish syntax to provide types to functions outside the normal parentheses, like the following. The syntax on the last line (of this first snippet) …

Total answers: 5

Type hint generic function with arguments of passed function

Type hint generic function with arguments of passed function Question: I would like to write type hints that allow type checkers to recognize if a wrong argument is passed to a function I call "meta" in the example code below: I have got the following functions: a function meta that takes a function and arguments …

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

reveal_type gives me a type, but it does not exist

reveal_type gives me a type, but it does not exist Question: So, I have a code that typechecks, but when I try to run it, it complains about the type not existing import pyrsistent a = pyrsistent.pset([1,2,3]) #reveal_type(a) #mypy file.py, gives type pyrsistent.typing.PSet[buildins.int] #b : pyrsistent.typing.PSet[int] = a #when running: AttributeError: module ‘pyrsistent’ has no …

Total answers: 1

Type hinting decorator with bounded arguments

Type hinting decorator with bounded arguments Question: An example of what I’d like to do is as follows: @dataclass class Arg1: x = field() @dataclass class Arg2: x = field() @dataclass class Obj: y = field() T = TypeVar("T") R = TypeVar("R", bound=(Arg1 | Arg2)) C = TypeVar("C", bound=Callable[[Self, R], T]) @staticmethod def deco(f: C) …

Total answers: 2