Skip to main content

All Questions

Tagged with
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)) ...
mirekh_68's user avatar
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 ...
asai95's user avatar
  • 3
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): ...
volferine's user avatar
  • 372
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 ...
Hernan's user avatar
  • 6,053
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 ...
Pierre-olivier Gendraud's user avatar
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 | ...
Pierre-olivier Gendraud's user avatar
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 ...
David Loehr's user avatar
-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 = ...
takeshi2010's user avatar
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 = ...
Anka's user avatar
  • 13
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 ...
Michoel's user avatar
  • 874
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 ...
MaartenB's user avatar
  • 475
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 ...
newandlost's user avatar
  • 1,020
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](...
AAZ's user avatar
  • 23
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(...
wstr's user avatar
  • 940
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 ...
DIN14970's user avatar
  • 351

15 30 50 per page
1
2 3 4 5
7