2,789 questions
-1
votes
2
answers
45
views
reveal_type in python as of python 3.10 in runtime [closed]
When using mypy playground, this works fine:
def f():
x = 123
reveal_type(x)
However, in runtime it generates the error
NameError: name 'reveal_type' is not defined
I've seen many ...
1
vote
1
answer
50
views
python type annotation of SpecialForms with pydantic and mypy
python 3.13
pydantic 2.10.5
mypy 1.16.0
how to properly annotate return of typing._SpecialForm, specifically typing.Annotated?
I'm using mypy as type checker.
from functools import partial
from ...
5
votes
2
answers
137
views
Containment test against literal list (`y in ["foo", "bar"]`) does not narrow value type
The following MWE causes mypy to error.
from typing import Literal
def expects_literal(x: Literal["foo", "bar"]) -> None:
print(f"{x=}")
def fails_mypy_check(y: ...
-1
votes
1
answer
63
views
How to type-annotate write when subclassing io.RawIOBase, getting "Liskov substitution principle" violation
I want to type-annotate the write in a class that subclasses io.RawIOBase.
I'm struggling to get anything other than Any to type check, which is frustrating, because I should be able to use a much ...
0
votes
1
answer
67
views
Pyright false positive when implementing a protocol [duplicate]
This MRE illustrates my problem:
from dataclasses import dataclass
from typing import Protocol
class Child(Protocol):
val: float
class Parent(Protocol):
sub: Child
@dataclass
class Child1(...
0
votes
1
answer
44
views
mypy type narrowing of a field does not update
I have a test that boils down to:
from enum import Enum
class E(Enum):
A = 1
B = 2
class Test:
def __init__(self) -> None:
self.var = E.A
def update(self, var: E) -> ...
2
votes
1
answer
46
views
mypy linter error (valid-type) with pydantic's Annotated pattern in a generic
Why would the following give a linter error in the second case but not the first:
# OK:
type MyAnnotatedType = Annotated[int | None, Field(strict=True)]
# Error: Invalid type alias: expression is not ...
-2
votes
1
answer
106
views
How do I make mypy treat my class as not being a subtype of object?
I'm trying to write a class in Python where comparisons like MyClass(5) == 5 are considered type errors by mypy, even though in Python all user-defined classes inherit from object.
My goal is to:
...
1
vote
2
answers
80
views
How to get mypy to recognise file `-stubs` package
Here's my folder structure:
.
├── foo
│ ├── __init__.py
│ └── classic.py
├── foo-stubs
│ ├── __init__.pyi
│ └── classic.pyi
├── pyproject.toml
└── t.py
Contents are:
$ cat foo/__init__.py
...
0
votes
0
answers
87
views
Issue while typing a recursive function with list unpacking
I'm trying to correctly type the following function in Python 3.10
from typing import overload
@overload
def joinpath(*path_pieces: str) -> str: ...
@overload
def joinpath(*path_pieces: list[str] |...
1
vote
2
answers
104
views
How to make mypy ignore pytest.approx in a code outside of test functions
I want to use pytest.approx(...) inside immutable dataclasses (frozen=True) in my unittest, so I can use a single assert to check possibly quite complex structures.
This works fine when I use these ...
0
votes
0
answers
77
views
Mypy issue with generics and overload
I'm trying to add types to my code, and I'm running into errors. Below is minimal version of code. I don't understand why B[str] | B[bytes] is incompatible with A[T]. The two last errors are also ...
3
votes
2
answers
141
views
How to type a generic callable in python
I am trying to do something like this:
from collections.abc import Callable, Coroutine
from typing import Any, Generic, TypeVar
CRT = TypeVar("CRT", bound=Any)
class Command(Generic[CRT]): ...
3
votes
1
answer
94
views
How to mark a class as abstract in python (no abstract methods and in a mypy compatible, reusable way)?
I'm trying to make it impossible to instantiate a class directly, without it having any unimplemented abstract methods.
Based on other solutions online, a class should have something along the lines ...
0
votes
1
answer
89
views
Typing sqlalchemy where clauses
Following this doc:
https://docs.sqlalchemy.org/en/20/orm/extensions/mypy.html
I tried to type-check my test.py file:
from sqlalchemy import Column, Integer, String, select
from sqlalchemy.orm import ...