<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<br>
<br>
<div class="moz-cite-prefix">18.05.2015, 02:50, Guido van Rossum
kirjoitti:<br>
</div>
<blockquote
cite="mid:CAP7+vJK8hqBhTXXz7U2cRuOcs4LWDejQd4SE8pRA-prSNhGu1w@mail.gmail.com"
type="cite">
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote">On Sun, May 17, 2015 at 3:07 PM, Alex
Grönholm <span dir="ltr"><<a moz-do-not-send="true"
href="mailto:alex.gronholm@nextday.fi" target="_blank">alex.gronholm@nextday.fi</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"> Looking at PEP 484,
I came up with two use cases that I felt were not
catered for:<br>
<ol>
<li>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)</li>
</ol>
</div>
</blockquote>
<div><br>
</div>
<div>I don't understand. What is "Type"? Can you work this
out in a full example? This code is already okay:<br>
<br>
</div>
<div>def foo(a: dict):<br>
...<br>
<br>
</div>
<div>foo(OrderedDict())<br>
</div>
</div>
</div>
</div>
</blockquote>
This code is passing an <i>instance</i> of OrderedDict. But how can
I specify that foo() accepts a <i>subclass</i> of dict, and not an
instance thereof?<br>
<br>
A full example:<br>
<br>
def foo(a: Type[dict]):<br>
...<br>
<br>
foo(dict) # ok<br>
foo(OrderedDict) # ok<br>
foo({'x': 1}) # error<br>
<blockquote
cite="mid:CAP7+vJK8hqBhTXXz7U2cRuOcs4LWDejQd4SE8pRA-prSNhGu1w@mail.gmail.com"
type="cite">
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote">
<div> </div>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<ol>
<li>Specifying that a callable should take at least
the specified arguments but would not be limited to
them: Callable[[str, int, ...], Any]</li>
</ol>
<p>Case #2 works already (Callable[[str, int], Any] if
the unspecified arguments are optional, but not if
they're mandatory. Any thoughts?<br>
</p>
</div>
</blockquote>
<div>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.<br>
</div>
</div>
</div>
</div>
</blockquote>
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:<br>
<br>
def calculate_sum(request: Request, *values):<br>
return sum(values)<br>
<br>
def register_request_handler(handler: Callable[[Request, ...],
Any]):<br>
... <br>
<blockquote
cite="mid:CAP7+vJK8hqBhTXXz7U2cRuOcs4LWDejQd4SE8pRA-prSNhGu1w@mail.gmail.com"
type="cite">
<div dir="ltr">
<div class="gmail_extra">-- <br>
<div class="gmail_signature">--Guido van Rossum (<a
moz-do-not-send="true" href="http://python.org/%7Eguido"
target="_blank">python.org/~guido</a>)</div>
</div>
</div>
</blockquote>
<br>
</body>
</html>