Skip to content

Make any callable compatible with (*args: Any, **kwargs: Any)? #5876

Description

@JukkaL

Perhaps a callable that accepts arbitrary arguments by having (*args: Any, **kwargs: Any) could be treated similarly to Callable[..., X] (with explicit ...), i.e. a callable with arbitrary arguments would be compatible with it.

The reason for this is that there's no easy way to annotate a function so that the type would be Callable[..., X], even though this seems to be a somewhat common need. Users sometimes assume that using (*args: Any, **kwargs: Any) will work for this purpose and are confused when it doesn't. python/typing#264 (comment) is a recent example.

Example:

fromtypingimportAnyclassA:
# A method that accepts unspecified arguments and returns intdeff(self, *args: Any, **kwargs: Any) ->int: passclassB(A):
deff(self, x: int) ->int: pass# Should be okay

We could perhaps extend this to handle decorators as well:

fromtypingimportAny, Callable, TypeVarT=TypeVar('T', bound=Callable[..., Any])
defdeco(f: T) ->T:
defwrapper(*args: Any, **kwargs: Any) ->Any:
print('called')
returnf(*args, **kwargs)
returnwrapper# Maybe this should be okay

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions