4,314 questions
-1
votes
1
answer
57
views
Incorrect type hint for name parameter in binary_var_list [closed]
The binary_var_list method in the docplex.mp.model API has an incorrect type annotation for the name parameter.
Current signature:
def binary_var_list(self, keys, lb=None, ub=None, name: type[str] = ...
1
vote
1
answer
64
views
Sqlalchemy mapped_column typing issues with pylance
I am currently defining ORMs and DTOs in my fastapi application, and using SQLAlchemy 2.0 for this job.
Many sources, including the official docs, specify that the way to use mapped types with ORMs is ...
1
vote
0
answers
67
views
How to provide type-safe factory functions for dynamically registered classes in Python?
TL;DR
Suppose a Python library defines an interface meant to be implemented by third-party code. How could this library provide a factory function that creates instances of those implementations, with ...
-4
votes
0
answers
90
views
Pydantic model that can take "Any" type?
Say I have a Pydantic model that can take an Any type. If the user sends in JSON on their end, what will Pydantic do here? Will it fail, or will request be of some known type?
Ultimately, I know ...
0
votes
0
answers
47
views
How to make Pydantic Generic model type-safe with subclassed data and avoid mypy errors?
I have the following abstract Data class and some concrete subclasses:
import abc
from typing import TypeVar, Generic, Union
from pydantic import BaseModel
T = TypeVar('T')
class Data(BaseModel, abc....
1
vote
1
answer
81
views
Python 3.12+ generic syntax for <T extends Foo>
Python 3.12 introduced new syntax sugar for generics. What's the new way of writing an upper-bounded generic like this:
def foo[T extends Bar](baz: T) -> T:
...
Before new syntax features I ...
1
vote
1
answer
122
views
Why does a class attribute named `type` make `type[Foo]` fail?
When I define a class attribute named type, a type[Foo] annotation inside the same class causes mypy to report that the type name is a variable and therefore “not valid as a type”.
class Foo:
type:...
1
vote
1
answer
107
views
Python Google style doc string for generic class Type Parameter
I want to annotate a type parameter for a generic dataclass of mine with a Google style docstring to both support generating documentation and mouse hovering within VS Code (and other editors/IDEs). ...
0
votes
1
answer
91
views
Combining Pydantic Models and File Uploads
I'm trying to type hint my fastAPI to take both a BaseModel pydantic class for various arguments and some seperate files. I also want to add a description for all of the inputs on http://127.0.0.1:...
1
vote
2
answers
108
views
python typing distinctions between inline created parameters and variables
Preamble
I'm using polars's write_excel method which has a parameter column_formats which wants a ColumnFormatDict that is defined here and below
ColumnFormatDict: TypeAlias = Mapping[
# dict of ...
4
votes
0
answers
100
views
Narrowing dict to TypedDict
I want to narrow an unambiguously defined dict[...] type in a superclass to a specific TypedDict in an inheriting class but I cannot figure out a way to specify a dict-based supertype that the ...
1
vote
1
answer
141
views
Type hint a decorator to enforce matching signature as the decorated function
How can I implement DecoratorFactory such that it type-checks as follows:
def accepts_foo(foo: int): ...
def accepts_bar(bar: int): ...
decorator_foo = DecoratorFactory(foo=1)
decorator_foo(...
1
vote
2
answers
116
views
Variadic tuple type parameter
I am trying to annotate a function which receives a tuple of types, and returns a value of one of the provided types.
For example:
@overload
def func[T1](types: tuple[type[T1]]) -> T1: ...
@...
0
votes
0
answers
127
views
telnetlib3, StreamReader/Writer vs. TelnetReader/Writer
I'm trying to type hint my telnetlib3 client.
However, I have some utility code which should not depend on telnet. It should be able to deal with asyncio StreamReader/Writer in common.
Now, if I pass ...
0
votes
0
answers
115
views
structlog enforce Wrapped logger type with mypy
I wanted to override the structlog logger for the whole application, by doing this:
import enum
from collections.abc import Iterable
import structlog
from structlog.typing import Processor
from ...
0
votes
0
answers
72
views
Type hinting for dynamically modified variable type through __setattr__
I am using pydantic with placeholders interpolation in field values:
from typing import Any
from pydantic import BaseModel
class Model(BaseModel):
_placeholders: dict[str, Any]
a: int = "{...
-1
votes
1
answer
57
views
Merging dict with TypedDict casts dict[str,object]?
My goal is to handle multiple functions that are chained and create outputs in dictionaries that are summarized and handed over in a chain. To assert types and that certain keys are present, I am ...
0
votes
1
answer
108
views
Inheriting function type hints from child class
Is there a way to make a generic class A[T], which has a function that inherits the type for the same function in T?
I want to make something like these 2 classes:
class A[T]:
...
def a(self):
...
4
votes
2
answers
126
views
Unexpected behavior of mypy with TypeVar, Generic, and decorators
I'm currently in trouble to understand TypeVar and Generic in Python.
Here's the setting of my MWE (Minimal Working Example) :
I defined an abstract class, which has a __call__ method:
This method ...
6
votes
2
answers
218
views
Type hint two function parameters to constrain them both to the same type and reject types like numpy.ndarray which do not support boolean comparison
I would like to annotate both parameters of a function – supposed to perform simple comparison between them – in order to indicate that their types should be the same and should support simple ...
1
vote
0
answers
49
views
Static typing for an `Enum` that contains compiled regex patterns
I am in a scenario (that could be compared to writing an AST) where I compile multiple regex patterns that I want to reuse later, and for the sake of simplicity and readability I want to store them in ...
1
vote
1
answer
119
views
Is it possible to set the type of a field based on the value of another field?
I want to set and instantiate the right type based on the value of another field and get typing and autocomplete on it.
I want this to happen automatically when the class is instantiated:
...
3
votes
3
answers
214
views
Type hint empty dict
I need to type-hint that a value is either some TypedDict or a completely empty dict. The TypedDict itself already exists and is non-trivial, and it's an all-or-nothing situation – so modifying the ...
0
votes
1
answer
86
views
How to use type hints in a dict with TypeVar and a function that depends on that TypeVar? [duplicate]
I have a relative simple app in which I use a Message Bus to pass messages.
I prefer a bit more stricter type checking: rather than passing arbitrary text strings as topic, I pass message objects, and ...
2
votes
0
answers
68
views
How to transform callable argument types?
Assume I have a callable type with some arbitrary set of argument types:
Callable[[T1, T2, T3, <etc>, Tn], str]
Is there a way (presumably using TypeVarTuple or ParamSpec) to statically ...