El vie, 16 abr 2021 a las 10:01, Walter Dörwald (<walter@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. 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.