-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature - Partial Type Specifications For Callable #8263
Comments
The recommended way to do weird stuff with callable types is to use callback protocols: https://mypy.readthedocs.io/en/latest/protocols.html#callback-protocols, which we usually prefer to the experimental extended callables. That said this doesn't support what you are trying to do here, I think. The best current approach involves writing a lot of overloads. Would https://www.python.org/dev/peps/pep-0612/ support your use cases? |
@msullivan unfortunately there is no way to achieve the desired effect using overloads as there's no way I could enumerate all the possibilities, nor does PEP-612 appear to cover this since it doesn't actually apply any restrictions on the callable being passed into the generic function. This feature request might actually dovetail nicely with PEP-612 though if |
I'm not sure I fully understand your use case here for all of these features |
@msullivan honestly the main one I'm after is the ability describe a function whose first N parameters are well defined, but where the remainder do not matter. The other features described here simply fall out nicely from the syntax. A specific use case for this new syntax might be a def handle_my_request(r: Request, param_1: int, param_2: str) -> str: ... Now imagine that we want to develop a Unfortunately there is no way to accomplish this. As a result I'm proposing that RequestHandler = Callable[[Request, ...], Any] # <-- request the new syntax
def route(uri: str) -> Callable[[RequestHandler], Route]:
def decorator(function: RequestHandler) -> Route:
...
return decorator
@route("/{param_1}/{param_2}")
def handle_my_request(r: Request, param_1: int, param_2: str) -> str: ... |
One approach for supporting use cases would be an extension to PEP 612, such as making it possible to concatenate argument lists and ParameterSpecifications. In any case, it seems that supporting this would require an extension to the PEP 484 syntax, and these should be discussed at https://github.com/python/typing and/or typing-sig@. If some approach attracts support there, you can create another issue here (or we can reopen this issue). |
Feature Request
It would be awesome if it were possible to create a partial type spec for
Callable
. For example, I might want to be able to specify that the first argument of a function must be an integer, but that any other parameters are allowed. This is useful if parameters are being curried to a function as in the decoratorcast_first_arg
decorator below where the only requirement offunc
is that it's first argument is anint
:At the moment, the only alternative to a "partial callable type spec" is to use
VarArg
andKwArg
, however this is problematic since, in the case ofcast_first_arg
, the function doesn't actually need to accept variadic positional or keyword arguments.Proposed Syntax
To provide a partial argument specification one should use
...
before, between, or after any extended callable types. Using this syntax the type spec forcast_first_arg
might look like this:Ellipsis After
When
...
follows some number of argument specifications should indicate that any other function parameters not explicitly indicated therein is allowed.Ellipsis Before
Similarly if
...
is placed before some number of argument specifications then only the last argument will have been specifically defined:Ellipsis Between
Lastly
...
placed between argument specifications should indicate that any parameters between the two explicitly defined parameters are allowed:Multiple Ellipsis
This could add complications and edge cases to the feature implementation so perhaps this could be left for a later iteration of partial callable type specifications, however this syntax enables you to describe an even wider variety of functions:
The text was updated successfully, but these errors were encountered: