On 16 Apr 2021, at 19:38, Jelle Zijlstra wrote:

El vie, 16 abr 2021 a las 10:01, Walter Dörwald (<walter@livinglogic.de>)

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


<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.