On 16 Apr 2021, at 19:38, Jelle Zijlstra wrote:
El vie, 16 abr 2021 a las 10:01, Walter Dörwald
(<wal...@livinglogic.de>)
escribió:
On 16 Apr 2021, at 16:59, Guido van Rossum wrote:
If you look deeper, the real complaints are all about the backwards
incompatibility when it comes to locally-scoped types in annotations.
I.e.
def test():
class C: ...
def func(arg: C): ...
return func
typing.get_type_hints(test()) # raises NameError: name 'C' is not
defined
Can't this be solved by wrapping the annotation in a lambda, i.e.
def test():
... class C: ...
... def func(arg: lambda: C): ...
... return func
...
test().__annotations__['arg']()
<class '__main__.test.<locals>.C'>
So typing.get_type_hints() would simply call an annotation if the
annotation was callable and replace it with the result of the call.
That sort of thing can work, but just like string annotations it's not
good
for usability.
Yes, but it's close to what PEP 649 does. The PEP even calls it
"implicit lambda expressions".
Users using annotations will have to remember that in some
contexts they need to wrap their annotation in a lambda, and unless
they
have a good understanding of how type annotations work under the hood,
it
will feel like a set of arbitrary rules. That's what I like about PEP
649:
code like this would (hopefully!) just work without needing users to
remember to use any special syntax.
Yes, that's what I like about PEP 649 too. It just works (in most
cases), and for scoping it works like an explicit lambda expression,
which is nothing new to learn.
If Python had taken the decision to evaluate default values for
arguments not once at definition time, but on every call, I don't think
that that would have been implemented via restringifying the AST for the
default value.
But then again, the difference between default values and type
annotations is that Python *does* use the default values. In most cases
however Python does not use the type annotations, only the type checker
does. The problem is where Python code *does* want to use the type
annotation. For this case PEP 649 is the more transparent approach.
Servus,
Walter
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/IMDY2NLPM36CHO6JFKDBE54NDWF3OPZO/
Code of Conduct: http://python.org/psf/codeofconduct/