How to type hint functions that modify leaf type of nested structures. #1411
Unanswered
randolf-scholz
asked this question in
Q&A
Replies: 1 comment 3 replies
-
A recursive type alias should do the trick. The following works fine in pyright and mypy. from typing import TypeAlias, TypeVar
T = TypeVar("T")
Nested: TypeAlias = T | list["Nested[T]"] | tuple["Nested[T]", ...] | dict[str, "Nested[T]"]
def foo(x: Nested[int]) -> Nested[float]:
... |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Basically I have a function that acts on arbitrarily nested data, for simplicity consider the following which casts all
int
tofloat
in a nested data structure.How can I type hint
foo
such that the following assertions holdBeta Was this translation helpful? Give feedback.
All reactions