Why can’t Ambroses support type notes?

Asked

Viewed 60 times

5

We know that Python, as of version 3.6, supports annotating types in functions and variables, according to the PEP 526 -- Syntax for Variable Annotations and PEP 3107 -- Function Annotations.

def int2float(x: int) -> float:
    return float(x)

a: int = 2
b: float = int2float(a)

However, even after the implementation of these Peps Amblas do not have type annotation support.

int2float = lambda x: int: float(x)

I believe that in this case would break the syntax and generate redundancy because it would not be possible to define whether the lambda is just lambda x: int, returning to class int, or if this is the type annotation. However, I believe that adding parentheses, which are currently not supported, would solve the problem:

int2float = lambda (x: int) -> float: float(x)

Apparently they decided that this would be unfeasible or that it would not make sense (except for the fact that the language is dynamic typing).

Why did you choose not to add type annotations in lambda? They didn’t see the need or it doesn’t make sense for a lambda to have type annotations?

1 answer

2

Well, according to PEP 3107 there really is no type annotations support for lambda functions and the reason for this is described in PEP itself (my free translation):

The syntax of a lambda function could have been changed to support annotations, being needed to add parentheses around the parameter. However decided do not make such changes because:

  • It would be an incompatible change
  • Lambdas are "castrated" anyway.
  • A lambda can always be ported to a function itself.

By "castrated," I imagine they said they were less complex functions than a normal function.

The justification Guido gave by email was that in order for the lambda to accept the type annotations it would take extra parentheses around the annotations so that the interpreter would not be confused with the : of the annotations and the : that comes before the function itself.

However, it ends the email with

Fortunately this is at least one of those Things that we can fix with a Purely syntactical source code Transformation...

which may mean that at some future time these functions may also gain note. (:

Currently, if you want to make the type annotations, either you turn your lambda function into a common function or you can use the module Callable to make the note, as described in that matter

  • Why would a change be incompatible? What do you mean "Both are neutral"? A function would also not be given that the language has dynamic typing? Even so they added the annotations to the functions. As for the lambda can be ported to a function, why would this serve as justification?

  • the change would be incompatible pq would have to change the syntax of lambda. imagine that you have a function lambda x: x+ 1, if you were to enter the type annotation, the parser would be confused because of the : that would separate the type annotations from the function itself

  • Even leaving parentheses (and type annotations) optional?

  • I edited the answer because I hit enter before sending hahaha sorry. , anyway, the messages are neutral because they are valid for any type. the same function I mentioned above works for int and float, so they decided that forcing this typing would not make so much sense

  • finally, this idea of neutrality of the lambda function justifies the third point cited by them. if you really want to leave the annotations, which are optional, you can turn into a function that the parser will not get confused. I don’t know if I was clear enough, but that’s what I got from PEP and the email I quoted from Guido there

  • What’s still confusing is this part about neutrality; the functions are also neutral against input types and yet they chose to add type annotations to them. Why would I justify to one and not to another?

  • To be honest, I think it was a translation error on my part. in the original post of PEP, called the function "neutered" and I read "neutral" (including I will edit the answer to not get anything wrong). neutered means "castrated", or "less powerful" than a normal function, because it is a hidden function. Maybe that’s why they thought it would be an unnecessary effort to keep the notes now. I can’t tell you in detail what justified the decision, but it seems to me that they just weren’t into it anyway

  • @Andersoncarloswoss I complemented my reply with excerpts from Guido’s email. see if it is somehow clearer for you. (: I don’t know if I can help much more than that hahaha

  • 1

    It is worth remembering that in the change to Python 3 Guido’s plan was to completely extinguish the Ambdas - were some protests that kept it (fortunately). Personally, I think that in order to be able to note the type of return would be more useful than the input valroes - in a truly anonymous lambda (without being assigned to a variable), the input values in general are "close" in the code, or in the documentation of the place to which it is being passed as parameter.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.