2,813 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
3
votes
1
answer
89
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, ...
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
127
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 ...
Advice
1
vote
4
replies
127
views
mypy complains about a missing optional module
I have a module (let's name it optional_module) that I want to be imported optionally, as it can be either present or absent. Now I do it this simple way:
try:
import optional_module
except ...
3
votes
1
answer
136
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[...
Best practices
0
votes
3
replies
76
views
Are there any advantages to having mypy check only your entrypoints in your Python program versus every single file?
I'm working with a large, existing Python codebase. Recently someone noticed that one of the files in the testsuite wasn't being type checked by mypy and so suggested to run python -m mypy . rather ...
1
vote
2
answers
217
views
Is there a way to install only stubs (types) and nothing more of a pypi package?
Objective
I'm using mypy to type check my code.
Locally, this works fine by running mypy --install-types once on setup. It installs e.g. scipy-stubs, because the stubs are in an extra package. It ...
-4
votes
1
answer
180
views
How to use type argument as return type?
Is it possible to tell the type checker what the return type is by supplying an input argument, something like rtype here:
from __future__ import annotations
from typing import TypeVar
T = ...
0
votes
1
answer
83
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
0
answers
132
views
How to check source and stub files with mypy?
The Mypy docs state:
If a directory contains both a .py and a .pyi file for the same module, the .pyi file takes precedence. This way you can easily add annotations for a module even if you don’t ...
1
vote
0
answers
136
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:...
2
votes
0
answers
87
views
Why does the Mediator work with QUERY, but Handler throws an incompatible method override error?
I’m trying to implement a system where I use a Mediator class to execute queries and return results based on the type of QUERY passed. I also want to use a Handler class with a handle method that ...
0
votes
0
answers
74
views
How do I make a Qt5 queued connection in a way that mypy will accept?
Using Python and Qt5, I can make a normal signal connection like this:
def clicked() -> None:
print("clicked")
btn = QtWidgets.QPushButton("Button", window)
btn....
1
vote
1
answer
112
views
Python static code analysis complains about kw argument 'font' in Event.widget.configure
I have a callback
from tkinter import font, ttk
class Foo(ttk.Frame):
def set_font_cb(self, event: tk.Event) -> None:
event.widget.configure(font=font.Font(...))
And this creates in ...
0
votes
0
answers
127
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 ...