|
|
|
|
|
by hermitdev
877 days ago
|
|
In practice, this would be solved with `typing.overload`[0]. Using you example: from typing import overload
@overload
def fooify(x: int) -> int:
...
@overload
def fooify(x: list[int]) -> list[int]:
...
def fooify(x: list[int] | int) -> list[int] | int:
if isinstance(x, list):
return [fooify(_x) for _x in x]
return x * 2
[0] https://docs.python.org/3/library/typing.html#overload |
|
Meanwhile it's not even possible to express such things in other static type systems. So I'm not exactly an unhappy customer, but it does put certain things tantalizingly close, but still out of reach without a ton of clunky boilerplate and LoC explosion.