4,321 questions
2
votes
1
answer
58
views
`NameError` from `inspect.signature()` or `obj.__annotations__` for types present only in `TYPE_CHECKING` block
I'm using inspect.signature() to get a function signature as a string for generating some documentation by hand, and I'm getting a NameError.
I have this minimal Python script to reproduce my problem:
...
7
votes
1
answer
123
views
How to annotate a function that returns a dataclass field so that type checkers treat it correctly?
I'm currently working on a library that uses dataclasses.dataclass classes for data structures. I'm utilizing the metadata argument of the dataclasses.field() method to inject custom library ...
1
vote
1
answer
118
views
How do type aliases (typing.Tuple, typing.Set, typing.List, etc.) work as an instance but also a class?
How do the type alias classes (pseudoclasses?) such as typing.Tuple work?
They behave like instance objects since their constructors take arguments
But they also behave like class objects: they ...
2
votes
2
answers
70
views
Why does the PyCharm type checker give a warning about operators in the ComponentData class when they are indexed?
I am working on a Pyomo model with indexed variables, expressions, and constraints, and I am getting some warnings regarding the ComponentData class. The model still runs smoothly, and the results are ...
1
vote
0
answers
92
views
How to type dict_keys/items/values in Python? [duplicate]
I have a protocol Store (as described here) that looks like:
from typing import Protocol, TypeVar, Any
type _KeyT[T] = type[T]
T = TypeVar('T', bound='SomeClass')
class Store(Protocol):
def ...
5
votes
0
answers
104
views
Can someone help explain Python's mypy issue with Incompatible return value type due to covariance when returning a sub type of a union
Apologies if there is a straightforward answer already that I'm too smooth brained to see. I am working with Python 3.13+ and Mypy 1.19.1. I've read mypy's docs, read several similar questions/answers ...
1
vote
3
answers
140
views
typing.overload based on presence of keyword arguments
Given:
from typing import overload, no_type_check
from collections.abc import Mapping
@overload
def f[K, V](arg: Mapping[K, V], /) -> Mapping[K, V]:
...
@overload
def f[K, V, V2](arg: Mapping[...
3
votes
2
answers
204
views
How to generate __slots__ based on annotations in Python 3.8-3.14
I would like to make __slots__-based classes, without having to repeat the attribute names that I've already listed in type annotations.
Before Python 3.14, I could do this:
class C:
foo: int
...
3
votes
1
answer
151
views
How do I resolve this circular import?
Implementing a virtual file system I encounter a circular import problem.
common.py:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .directory import Directory
from .archive import ...
2
votes
1
answer
87
views
Map variadic generics to their class equivalent
I am trying to use variadic generics to express the following pattern: given a variadic list of classes, return a tuple of instances whose types correspond positionally to the input classes.
...
1
vote
0
answers
109
views
basedpyright gives 'Type of "connect" is partially unknown' on PyQt6 connect
When I use a signal's connect method, basedpyright gives a warning. I use the signal as is in the documentation: some_object.someSignal.connect(some_callable). It works, the only problem is the ...
4
votes
1
answer
110
views
How to type hint class tuple itself not instance without pylance complaining
I'm trying to hint that a Pydantic BaseModel field needs to be the class tuple or one of its subclasses, so I've typed the field as type[tuple]:
from pydantic import BaseModel
class Task1(BaseModel):
...
3
votes
1
answer
98
views
How to narrow type of literals using generics?
In order to narrow type into literal types, I usually do the following:
from typing import Literal, TypeIs, get_args, reveal_type
type OneTwoThree = Literal[1, 2, 3]
type FourFiveSix = Literal[4, 5, ...
2
votes
1
answer
108
views
Why is list[list[str]] not equal to list[HashableList[Hashable]]
I struggle with typechecks using matplotlib.pyplot.subplot_mosaic.
I have create the following fuction, which generates the mosaic pattern and the per_subplot_kw:
def create_mosaic(num_rows):
def ...
0
votes
1
answer
106
views
How to type libraries using ahk? [duplicate]
How to type libraries using ahk? I thought about doing it like this:
class AHKMouseController:
def __init__(
self,
ahk: AHK
):
self._ahk = ahk
But mypy complains:
...