4,332 questions
1
vote
1
answer
88
views
Define a type as a union of constants
I have a Python function that accepts a socket.AddressFamily argument.
import socket
def fn(family: socket.AddressFamily) -> None:
...
This works fine. However, intersphinx doesn’t know this ...
0
votes
1
answer
93
views
How can I type-hint a constrained method on a generic class?
I have a Python method that only works on a constrained subsets of a generic class. As as constructed example, I might want Spam.floatify in the following code to only be available for instances of ...
0
votes
0
answers
139
views
Python 3.14 annotationlib throws NameError on local factory function defaults
I am testing the new Python 3.14 annotationlib module to build a dynamic validation tool. I want to inspect annotations lazily without causing import circular dependencies.
The problem is that if my ...
-3
votes
0
answers
55
views
Python bitwise OR (?) operator | used in function argument type hints [duplicate]
I'm trying to understand the methods defined in gdsfactory, a project for laying out components for integrated circuit wafers. Each component is defined by the class Component, in which you put ...
3
votes
1
answer
161
views
"name 'T' is not defined" when evaluating annotations on inherited methods using Python 3.12+ type parameter syntax
I am upgrading a codebase to Python 3.12 and migrating our generic classes to use the new PEP 695 syntax, e.g.:
class Box[T]:
...
instead of:
T = TypeVar('T')
class Box(Generic[T]):
...
Most ...
Advice
2
votes
1
replies
70
views
Typing a restricted wrapper of isinstance
I have a function comma_separated_list_ex currently implemented as such
def comma_separated_list_ex[T: Lexeme](self, typ: type[T] | tuple[type[T], ...], fin: type[Lexeme]) -> tuple[
list[T],...
2
votes
2
answers
309
views
How to type a decorator as a callable on Python
I want to type a decorator, that is a callable that returns a callable.
I want to type this on a way that validate at the moment of decoration that the args and returns type are correct, but also ...
3
votes
2
answers
133
views
FastAPI response model for env-dependent variable return type
_Item = TypeVar("_Item", BaseItem)
@app.get(path="items")
def get_items() -> list[_Item]:
return _get_items()
Pylance complains:
TypeVar "_Item" appears only ...
Advice
6
votes
3
replies
169
views
Type narrowing a generic sequence argument in python
Afternoon,
Is it possible in python, given a function that takes a generic sequence of objects, to type it so that the list must be of only one type and not a union of multiple children?
for code like ...
3
votes
0
answers
141
views
Static verification of type compatibility across Python + YAML + JSON Schema pipeline stage boundaries [closed]
Is there an existing tool or pattern that statically verifies type compatibility across pipeline stage boundaries — where stages are defined in Python, configured in YAML, and validated with JSON ...
1
vote
2
answers
114
views
Type annotation that depends on the number of args
I have a function that extract info on any kind of variable, and returns it, so it can be embedded in a code without the need to rewrite another line. It is defined as such:
from typing import TypeVar
...
Best practices
0
votes
2
replies
94
views
Use signature of one function as typing of another function
If I write a wrapper around a function, how can I type *args and **kwargs properly?
In this tiny example (just a tiny self contained minimal code, not meant to relate to what exists in real life)
# ...
Advice
3
votes
8
replies
256
views
Make UserString behave like str
TLDR; is there a way to make UserString be accepted as str, or to define a class that inherits from str and then intercept access to self in the str (to modify before returning it)?
We want to apply ...
Best practices
0
votes
4
replies
99
views
Dataclass from dynamically created class
I am trying to create a dataclass from a dynamically created class. I am doing something like
[1] A = dataclass(type("ClsA", (), {"a":1}))
However, the attribute a is not ...
4
votes
1
answer
192
views
Why can I use a non-runtime_checkable protocol with non-method members in isinstance?
I have the following code
from typing import Protocol, runtime_checkable
@runtime_checkable
class A(Protocol):
def f(self): ...
class B:
def f(self): ...
if __name__ == '__main__':
...