PEP 3107 - Function Annotations

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

PEP 3107 - Function Annotations

Post by kevinkk525 » Wed Dec 19, 2018 1:52 pm

Just like CPython3.5 micropython supports function annotations:

Code: Select all

>>> def test(a: str)->bool:
...     print(a)
...     return True
In my opinion this is great during the development of libraries and I'm just looking for confirmation that using this feature does not have any downsides like increase RAM usage, firmware size, speed etc.
I could not find any information about it.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: PEP 3107 - Function Annotations

Post by pythoncoder » Wed Dec 19, 2018 4:58 pm

It is used - indeed required - for the Viper code emitter. I haven't tried it with normal Python code. You can view the resultant bytecode with

Code: Select all

micropython -v -v my_file.py
so you could compare bytecode with and without annotations. I would expect them to be the same, but it would be good to know.
Peter Hinch
Index to my micropython libraries.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: PEP 3107 - Function Annotations

Post by kevinkk525 » Thu Dec 20, 2018 2:05 pm

Guess that's a good way for firmware size but not for speed and RAM (RAM can be tested on the microcontroller of course and speed too).

However I can't find the executable "micropython". Did you mean using mpy-cross to compile it to bytecode .mpy and compare the filesize of these?
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: PEP 3107 - Function Annotations

Post by HermannSW » Thu Dec 20, 2018 9:47 pm

That "micropython" is the executable built in Micropython Unix port:
https://github.com/micropython/micropyt ... ports/unix

Code: Select all

$ ./micropython -v
MicroPython v1.9.4-683-gd94aa57 on 2018-11-13; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> 
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: PEP 3107 - Function Annotations

Post by pythoncoder » Sat Dec 22, 2018 6:49 am

kevinkk525 wrote:
Thu Dec 20, 2018 2:05 pm
Guess that's a good way for firmware size but not for speed and RAM (RAM can be tested on the microcontroller of course and speed too).

However I can't find the executable "micropython". Did you mean using mpy-cross to compile it to bytecode .mpy and compare the filesize of these?
My point was to check whether the bytecode was identical with or without annotations.

As @HermannSW says, it's the Unix build of MicroPython.

From a quick test with this script the bytecode is identical with and without annotations (implying identical resource usage). But it would be good to see a comment from someone who understands the compiler.

Code: Select all

def foo(a: int, b: int, c: int) -> int:
    return a*b*c

print(foo(4,5,6))
Peter Hinch
Index to my micropython libraries.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: PEP 3107 - Function Annotations

Post by kevinkk525 » Sat Dec 22, 2018 9:05 am

Thanks for testing this. I had to set my PC up again after Windows failed me so I just finished setting up the building environment and got the unix port running but did not test it yet.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: PEP 3107 - Function Annotations

Post by pythoncoder » Sat Dec 22, 2018 11:00 am

Well, my test was hardly exhaustive ;)

But reading the PEP, the only effect of function annotations is to populate the __annotations__ function attribute. Since this can be done at compile time I wouldn't expect it to affect the runtime except for the RAM used by the __annotations__ dict. But further testing indicates that MicroPython doesn't instantiate __annotations__ so my guess is that, on MP, it's cost-free.

Code: Select all

>>> def foo(x: int = 5):
...     print(x)
... 
>>> foo()
5
>>> dir(foo)
['__class__', '__name__']
>>> 
Peter Hinch
Index to my micropython libraries.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: PEP 3107 - Function Annotations

Post by kevinkk525 » Sat Dec 22, 2018 11:02 am

Sounds great! Thanks a lot.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply