[Python-Dev] PEP 484 wishes
Alex Grönholm
alex.gronholm at nextday.fi
Mon May 18 09:14:09 CEST 2015
18.05.2015, 02:50, Guido van Rossum kirjoitti:
> On Sun, May 17, 2015 at 3:07 PM, Alex Grönholm
> <alex.gronholm at nextday.fi <mailto:alex.gronholm at nextday.fi>> wrote:
>
> Looking at PEP 484, I came up with two use cases that I felt were
> not catered for:
>
> 1. Specifying that a parameter should be a subclass of another
> (example: Type[dict] would match dict or OrderedDict; plain
> "Type" would equal "type" from builtins)
>
>
> I don't understand. What is "Type"? Can you work this out in a full
> example? This code is already okay:
>
> def foo(a: dict):
> ...
>
> foo(OrderedDict())
This code is passing an /instance/ of OrderedDict. But how can I specify
that foo() accepts a /subclass/ of dict, and not an instance thereof?
A full example:
def foo(a: Type[dict]):
...
foo(dict) # ok
foo(OrderedDict) # ok
foo({'x': 1}) # error
>
> 1. Specifying that a callable should take at least the specified
> arguments but would not be limited to them: Callable[[str,
> int, ...], Any]
>
> Case #2 works already (Callable[[str, int], Any] if the
> unspecified arguments are optional, but not if they're mandatory.
> Any thoughts?
>
> For #2 we explicitly debated this and found that there aren't use
> cases known that are strong enough to need additional flexibility in
> the args of a callable. (How is the code calling the callable going to
> know what arguments are safe to pass?) If there really is a need we
> can address in a future revision.
Consider a framework where a request handler always takes a Request
object as its first argument, but the rest of the arguments could be
anything. If you want to only allow registration of such callables, you
could do this:
def calculate_sum(request: Request, *values):
return sum(values)
def register_request_handler(handler: Callable[[Request, ...], Any]):
...
> --
> --Guido van Rossum (python.org/~guido <http://python.org/%7Eguido>)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150518/81c295c1/attachment.html>
More information about the Python-Dev
mailing list