You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please forgive me if this issue is already solved or if it is part of #1317, but I'm not sure this is already covered with the existing issues. I'm trying to find a solution for the following use case:
from collections import Callable
from typing import Any, Tuple
def decorator(func: 'Callable[..., None]') -> 'Callable[..., None]':
def wrapper(*args: Tuple[Any], **kwargs: Tuple[Any]) -> None:
new_args = [arg[0] for arg in args]
new_kwargs = {key: kwargs[key][0] for key in kwargs}
func(*new_args, **new_kwargs)
return wrapper
@decorator
def f1(a: str) -> None:
print('f1')
print(a)
@decorator
def f2(a: str, b: int) -> None:
print('f2')
print(a)
print(b)
@decorator
def f3(a: str, b: int = 3) -> None:
print('f3')
print(a)
print(b)
# This works as expected
f1(('abc',)) # Good 1
f1(a=('keyword_abc',)) # Good 2
f2(('def',), (3,)) # Good 3
f3(('ghi',)) # Good 4
# I would like these to generate an error
f1('abc') # Bad 1
f1(a='abc') # Bad 2
f2('def', 3) # Bad 3
f3('ghi') # Bad 4
I have a decorator, which changes functions to accept all arguments inside a Tuple. The current solution works and doesn't give any mypy errors, because the type of the decorated functions is
'def (*Any, **Any)'
and basically looses all typing information.
However, after the decorator is applied, the four last lines should give an error, since the decorated function only accepts tuples now.
Is or will there be a way type decorator so that these cases can be solved? I tried different solutions with generics and overloads, but didn't manage to improve upon Callable[..., None].
Or, in other words, I would like to add more typing information to decorator to allow mypy to convert decorated functions from Callable[[T1, T2, T3], None] to Callable[[Tuple[T1], Tuple[T2], Tuple[T3]], None]
The text was updated successfully, but these errors were encountered:
which correctly handles Good 1 and Bad 1, but only works for functions with one argument (like f1) and also fails when calling the decorated function with keyword arguments (Good 2)
Yeah, this is an unfortunate shortcoming that is covered in #1317 and maybe others (python/typing#193).
We're hoping to improve on this, though the timeframe is somewhat unclear. Currently the best solution is to write a bunch of overloads (for 0-6 arguments, for example), though that won't work for arbitrarily named keyword arguments.
Please forgive me if this issue is already solved or if it is part of #1317, but I'm not sure this is already covered with the existing issues. I'm trying to find a solution for the following use case:
I have a decorator, which changes functions to accept all arguments inside a
Tuple
. The current solution works and doesn't give any mypy errors, because the type of the decorated functions isand basically looses all typing information.
However, after the decorator is applied, the four last lines should give an error, since the decorated function only accepts tuples now.
Is or will there be a way type decorator so that these cases can be solved? I tried different solutions with generics and overloads, but didn't manage to improve upon
Callable[..., None]
.Or, in other words, I would like to add more typing information to
decorator
to allowmypy
to convert decorated functions fromCallable[[T1, T2, T3], None]
toCallable[[Tuple[T1], Tuple[T2], Tuple[T3]], None]
The text was updated successfully, but these errors were encountered: