8000 py: Implement PEP 526, syntax for variable annotations by dpgeorge · Pull Request #6126 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

py: Implement PEP 526, syntax for variable annotations #6126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 16, 2020

Conversation

dpgeorge
Copy link
Member
@dpgeorge dpgeorge commented Jun 8, 2020

This addition to the grammar was introduced in Python 3.6 (see issue #2415). It allows annotating the type of a varilable, like:

x:int = 123
s:str

The implementation here is quite simple and just ignores the annotation (the int and str bits above). The reason to implement this is to allow Python 3.6+ code that uses this feature to compile under MicroPython without change.

In the future viper could use this syntax as a way to give types to variables (which is currently done in a bit of an ad-hoc way, eg x = int(123)). And this syntax could potentially be used in the inline assembler to define labels in an way that's easier to read.

8000
@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jun 8, 2020
@dpgeorge
Copy link
Member Author
dpgeorge commented Jun 8, 2020

See https://www.python.org/dev/peps/pep-0526/ for the PEP.

Code size change for this PR is:

   bare-arm:   +44 +0.067% 
minimal x86:   +80 +0.055% 
   unix x64:   +40 +0.008% 
unix nanbox:   +48 +0.011% 
      stm32:   +44 +0.011% PYBV10
     cc3200:   +56 +0.031% 
    esp8266:   +68 +0.010% GENERIC
      esp32:   +80 +0.006% GENERIC[incl -16(data)]
        nrf:   +52 +0.036% pca10040
       samd:   +60 +0.059% ADAFRUIT_ITSYBITSY_M4_EXPRESS

@tve
Copy link
Contributor
tve commented Jun 9, 2020

What prompted this implementation? I guess it's "nice to have" but last I looked there are python 3.5 features that are not yet implemented?

@dpgeorge
Copy link
Member Author

What prompted this implementation?

It was on my to-do list for a long time... at some point I was trying to run code that used this syntax and it would not run.

This is not high-priority but is one of the things that eventually needs to be done to get full Python 3.6 syntax compatibility.

@kevinkk525
Copy link
Contributor
kevinkk525 commented Jun 10, 2020

I have been waiting for this feature for a long time. Thanks!

@tve with bigger projects I found it very useful to use variable annotation. It makes writing code easier and more robust (at least in my case). Also easier to get back into the code after some time.
Until now, I had to run a script "strip_hints" over all project files before compiling/copying them because upy didn't support variable annotations.
So for me this was a feature that can't wait until all requested python 3.5 features have been built in, because this is the way people could/should write programs now (with python >3.6). Especially because a fairly small code change was needed to support it.

dpgeorge added 3 commits June 16, 2020 23:18
This addition to the grammar was introduced in Python 3.6.  It allows
annotating the type of a varilable, like:

    x: int = 123
    s: str

The implementation in this commit is quite simple and just ignores the
annotation (the int and str bits above).  The reason to implement this is
to allow Python 3.6+ code that uses this feature to compile under
MicroPython without change, and for users to use type checkers.

In the future viper could use this syntax as a way to give types to
variables, which is currently done in a bit of an ad-hoc way, eg
x = int(123).  And this syntax could potentially be used in the inline
assembler to define labels in an way that's easier to read.
@dpgeorge dpgeorge merged commit a51eef4 into micropython:master Jun 16, 2020
@dpgeorge dpgeorge deleted the py-pep-526 branch June 16, 2020 13:38
@dpgeorge dpgeorge mentioned this pull request Jun 16, 2020
53 tasks
tannewt pushed a commit to tannewt/circuitpython that referenced this pull request Mar 8, 2022
…etection

Espressif: Fix i2c pullup detection
@beyonlo
Copy link
beyonlo commented Aug 2, 2022

@kevinkk525 Do you have any example to share using this syntax for variable annotations on MicroPython to have a robust code?

I would like to check, for example, when the method receive a wrong argument type, it return a error for that, or some like that. Is this the main power of this feature?

Thank you.

@peterhinch
Copy link
Contributor

Annotations are not checked by the compiler, they are for information only. There may be static analysis tools that can do this.

@mattytrentini
Copy link
Contributor

@beyonlo mypy is a commonly used tool for this. Consider the following:

# th.py
def take_int(x: int):
    print(x)

take_int(10)
take_int("abcd")

(Micro)Python runs this without error, not checking types:

❯ python th.py
10
abcd

But running mypy quickly identifies the issue:

❯ mypy .
th.py:5: error: Argument 1 to "take_int" has incompatible type "str"; expected "int"
Found 1 error in 1 file (checked 1 source file)

@beyonlo
Copy link
beyonlo commented Aug 3, 2022

@mattytrentini

Understood. But in this case I need to have all methods/functions available to be called by mypy using something like as a test.py - that will be pass argument to check all methods, right? However some methods can't be called directly because are internally, I mean, just some methods can to call another methods. Do you have a tip to how to solve that?

Thank you.

@ZanderBrown
Copy link

It's static analysis — it's not running your code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants
0