-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Conversation
See https://www.python.org/dev/peps/pep-0526/ for the PEP. Code size change for this PR is:
|
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? |
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. |
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. |
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.
…etection Espressif: Fix i2c pullup detection
@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. |
Annotations are not checked by the compiler, they are for information only. There may be static analysis tools that can do this. |
@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) |
Understood. But in this case I need to have all methods/functions available to be called by Thank you. |
It's static analysis — it's not running your code |
This addition to the grammar was introduced in Python 3.6 (see issue #2415). It allows annotating the type of a varilable, like:
The implementation here is quite simple and just ignores the annotation (the
int
andstr
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.