4,331 questions
4
votes
2
answers
79
views
Compile list with more than one argument
Why is the following line valid Python code?
code = compile('l = list(1, 2, 3, 4, 5)', '', 'exec')
The documentation says
Using the type constructor: list() or list(iterable)
I can even write the ...
-3
votes
0
answers
120
views
Python Generics and inheritance
I have created this abstract base class:
import json
from dataclasses import dataclass, InitVar, field
from cryptography.hazmat.primitives.asymmetric.types import PrivateKeyTypes, PublicKeyTypes
...
1
vote
1
answer
101
views
Deprecate only a specific overload
Python type-hints allow me to define several @overload for a single function, so that only a single version of the function exists at runtime, but multiple versions exist from the point of view of the ...
4
votes
2
answers
91
views
PyCharm Code Inspection warns about tkinter constants
Using PyCharm (and Python), my code runs fine, but PyCharm gives me warnings that I'd like to avoid.
In this example code:
import tkinter as tk
root = tk.Tk()
tk.Label(text="dummy").pack(...
2
votes
3
answers
140
views
How to typecheck x.startswith() when x has type str | bytes?
I have the following code:
import json
from typing_extensions import Any
import lz4.block
bytebuf = bytes | bytearray
MAGIC_HEADER = b"mozLz40\0"
def jsonlz4_loads(buf: str | bytebuf) ->...
-2
votes
2
answers
215
views
Do function annotations make Python code faster? [duplicate]
I was wondering if notations mattered in Python.
These are both the same functions, same input and output:
# example 1
def add(x, y):
return x + y
# example 2
def add(x: int, y: int) -> int:
...
3
votes
1
answer
177
views
How do I write a type hint to indicate that a variable is a specific subtype of the return type of a method
I am working with a method from an external library that has return type A. In practice, however, the method always returns instances of a subtype of A, since A acts only as a base class. There are ...
5
votes
2
answers
158
views
Python type hints with optional dependencies
I am writing some Python type stubs for a package, and I have run into a function that can take either a list or, if numpy is available, a numpy.ndarray. If numpy is present and the function is given ...
0
votes
0
answers
155
views
Type-hint variable/argument that itself is a type but should allow Unions [duplicate]
I am trying to build a descriptor class and want to provide neat type hints to users. The idea is a bit like this:
class MyModule(Module):
a = Connector(float)
b = Connector(Union[float, str, ...
3
votes
2
answers
85
views
Getting mypy to recognize tuple of certain length
Mypy triggers the following error:
Incompatible return value type (got "tuple[int | None, ...]", expected "tuple[int | None, int | None, int | None, int | None, int | None, int | None, ...
2
votes
4
answers
122
views
Proper python type-hinting for functions that return a list of objects of unknown classes
Let's say I am writing a catalogue of museum items. Let's say I have a data hierarchy like this (all examples below are pseudo code and not taken from any real project):
file baseclasses.py
class Item:...
0
votes
0
answers
111
views
How to implement a Protocol as a class instance variable?
I have a class that has a member variable that I want to allow to be any container that implements a few functions, which I'm enforcing with the Protocol below:
class RandomAccessContainer(Protocol):
...
4
votes
1
answer
136
views
Is there a Python equivalent of TypeScript's Omit?
In TypeScript I have Omit, constructing a new type from an existing type by removing keys:
interface Todo {
title: string;
description: string;
completed: boolean;
createdAt: number;
}
type ...
2
votes
1
answer
126
views
`NameError` from `inspect.signature()` or `obj.__annotations__` for types present only in `TYPE_CHECKING` block
I'm using inspect.signature() to get a function signature as a string for generating some documentation by hand, and I'm getting a NameError.
I have this minimal Python script to reproduce my problem:
...
7
votes
1
answer
170
views
How to annotate a function that returns a dataclass field so that type checkers treat it correctly?
I'm currently working on a library that uses dataclasses.dataclass classes for data structures. I'm utilizing the metadata argument of the dataclasses.field() method to inject custom library ...