4,315 questions
3
votes
1
answer
104
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 ...
1
vote
1
answer
71
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
61
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
99
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
87
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, ...
3
votes
1
answer
80
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
98
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:
...
Best practices
2
votes
2
replies
126
views
how to enforce not-None for Optional arguments to Pydantic models
I am working with an application that uses Pydantic models extensively. Many attributes on these models are set to Optional but with a default value to ensure that they are not None. The intent is ...
1
vote
1
answer
89
views
basedpyright cannot resolve imports from tests directory even though unittest run fine
I'm using basedpyright for static type checking, and it fails to resolve imports from a test utility module located under the tests directory.
However, unittest executes the tests without any issues. ...
3
votes
2
answers
122
views
VS Code selects incorrect overload when a function parameter has a default value
I'm overloading a method so the return type differs depending on the value of a given bool parameter. That same parameter has a default value (False in my case).
Here's a simplistic example function ...
Best practices
0
votes
1
replies
41
views
How to type a generic SimulationEvent and engine in Python without “TypeVar bound type cannot be generic” errors?
I'm building a simulation engine in Python and want to use generics so that events are strongly typed to a simulation state. I have something like this:
from __future__ import annotations
from abc ...
3
votes
1
answer
94
views
Propagate correctly unions across containers with pyright
Pyright type inference seems unable to convert unions of homogeneous sequences, e.g. from Union[tuple[A, ...], tuple[B, ...]] to Union[list[A], list[B]].
Suppose a function which takes as input an ...
0
votes
1
answer
97
views
Subclass of Generic fails with AttributeError: object has no attribute '__parameters__' - using Generic when superclass does forward __init_subclass__
I have a setup like the following
from typing import Generic, TypeVar
T = TypeVar("T")
class ThirdParty:
def __init_subclass__(cls):
... # does not call super()
class Mine(...
3
votes
1
answer
135
views
Negative narrowing of union type with TypeIs of TypeVar not working as expected in new mypy version
I recently upgraded mypy from 1.17.0 to 1.18.2
The following code was successfully validated in the old mypy version (1.17.0), but fails in the new one (1.18.2):
_T = TypeVar('_T')
class Foo(Generic[...
2
votes
2
answers
142
views
Enforce __init__ method's arguments are superset of base class
We use pyre for linting and have been updating some old polymorphic code to be typed. The __init__ method has quite a few arguments and was using **kwargs to pass them through the various layers with ...