All Questions
93 questions
0
votes
0
answers
66
views
Using python 3.12 generics and mypy
I want to achieve something like this:
from collections import deque
from typing import Iterable
import numpy as np
def mean[T](a: Iterable[T]) -> T:
s = sum(a)
return s * (1.0/len(a))
...
0
votes
1
answer
100
views
Type alias for type of typevar and mypy
I have a type var that represents a child of specific interface
BuildableChild = TypeVar("BuildableChild", bound=Buildable)
The content of Buildable does not really matters, let's assume it ...
0
votes
0
answers
51
views
How to type a generic proxy class [duplicate]
How do I tell mypy that my proxy class has the same attributes as the proxied class?
import typing as t
from dataclasses import dataclass
class Proxy(t.Generic[C]):
def __init__(self, obj: C):
...
0
votes
1
answer
57
views
Creating a constrained TypeVar from Union
Running mypy on the following code yields no issues.
from typing import TypeVar
S = TypeVar("S", int, float, complex)
def func(x: list[S], m: S) -> list[S]:
return [val * m for val ...
1
vote
4
answers
205
views
How to constrain a generic type with an alias derived from a union type in Python
quick summary:
I want the function combine to take two arguments with the same type. It is the generic type T.
And T is one element certain union type possibleTypeForCombine = int | str. I want to ...
0
votes
2
answers
90
views
How to efficiently use generics in Python to type-hint a function with two arguments of the same type (either int or str)?
I have a function that takes two arguments of the same type, which could be either int or str. My initial implementation without generics looks like this:
python
def combine1(a: int | str, b: int | ...
0
votes
0
answers
28
views
Can I refer to one TypeVar in the bound of another TypeVar?
I have a generic Base class that instantiates a class called Factory. The Factory class is provided to Base by Base's subclasses, and then Base passes the instance to certain subclass methods.
I want ...
-1
votes
1
answer
113
views
How do I safely type a function that accepts a generic container class?
from __future__ import annotations
import logging
from datetime import datetime, UTC
from typing import Any, Generic, Self, Protocol, TypeVar
from pydantic import AwareDatetime, BaseModel
logger = ...
1
vote
2
answers
202
views
Type casting of Generics
I'm using mypy and ran into unexpected behaviour. Mypy incorrectly infer type on an expected type
from typing import Generic, TypeVar, Callable, reveal_type
S1 = TypeVar('S1')
F1 = TypeVar('F1')
I = ...
2
votes
2
answers
2k
views
Generic type-hinting for kwargs
I'm trying to wrap the signal class of blinker with one that enforces typing so that the arguments to send and connect get type-checked for each specific signal.
eg if I have a signal user_update ...
0
votes
1
answer
56
views
Type hint for a factory classmethod of an abstract base class in Python 3
I have two abstract classes, AbstractA and AbstractB. AbstractB is generic and its type parameter is bound to AbstractA. AbstractB further has a factory classmethod that returns an instance of one of ...
0
votes
1
answer
56
views
How to distinguish between Base Class and Derived Class in generics using Typing and Mypy
Consider the following code:
from typing import TypeVar
import dataclasses
@dataclasses.dataclass
class A:
pass
@dataclasses.dataclass
class B(A):
pass
T = TypeVar("T", A, B)
def ...
2
votes
1
answer
212
views
How do I check that parameters of a variadic callable are all of a certain subclass?
This might be a tough one.
Suppose I have a type
JSON = Union[Mapping[str, "JSON"], Sequence["JSON"], str, int, float, bool, None]
And I have a function
def memoize[**P, T: JSON](...
0
votes
0
answers
94
views
Union of protocols with invariant generic type causes problems
I have a protocol MyExporter using a generic type T:
T = TypeVar("T", bound=BaseSample)
class MyExporter(Protocol[T]):
def get_sample(self) -> T:
...
def process_sample(...
0
votes
0
answers
134
views
How to deal with nested protocols and generics for Python stubs
I'm trying to define a Protocol for a Stream object. Stream should be generic over two parameters: inputs and outputs. Stream defines methods that return a new stream with the new inputs being the ...