-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
pep 570 positional only parameters. #8480
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
base: master
Are you sure you want to change the base?
pep 570 positional only parameters. #8480
Conversation
Thanks for the contribution. I think this is an important addition for MicroPython because it allows a nice optimisation, where the names of parameters don't need to be stored alongside the function definition. But I don't think that's done here? In particular this part of the code in const uint8_t *arg_names = code_state->ip;
arg_names = mp_decode_uint_skip(arg_names); |
You are correct, we did not think of making this optimization. We will add it. Changing to draft while we make the changes. |
692494a
to
9adf2e4
Compare
py/bc: Add pos-only to encode/decode bytecode. py/compile: Raise exception when slash in invalid position. py/grammar: Allow slash in argslist_item. py/objfun: Change according to py/bc. py/persistentcode: Add pos-only to bytecode_prelude. py/profile: Add pos-only to prelude extract. py/scope: Add pos-only to scope_t. tools/mpy-tool: Reflect on changes to prelude.
Ported over CPython tests to test positional-only args. Expected output of the run given.
9adf2e4
to
68cb619
Compare
68cb619
to
3efbea4
Compare
@dpgeorge marked as ready for review, I changed the appropriate emit functions (bc&native) to skip storing arg_names for positional only parameters. |
Regarding the prelude signature encoding, Did you consider any other way to store the new number needed by this PR (number of positional-only args)? One way to test the impact of the change here is to compile (using |
Removing the pos only parameter count from the pos_args counter.
Yes I guessed so while I read through the git history of the
We began implementing this with this pattern: If your question is if we considered placing it elsewhere in the byte/native code than the answer is no. Not sure where to look.
I am not sure if the best approach is to do an before and after analysis, generating mpy files with the code before restricts the usage of pos only parameters and we would probably see increased size. I am however unsure what the best approach is, maybe this is the only way. At last, I added a new commit which could make the byte/native code smaller when positional parameters are used. I have yet to squash it into the implementation commit. |
I think this scheme is actually a good idea (although maybe I ran some tests and removing 1 bit from S in the trailing bytes makes very little difference to encoding size. Compiling all code in I did some other tests with this PR, modifying some of uasyncio to use the new |
Some other points:
|
Okay, so my plan is to:
|
Now that v1.19 is released with mpy format v6, I don't want to change the bytecode format for at least a couple of releases. So it would be good to split this PR into two separate pieces:
|
…lizzard_s3 Added Unexpected Maker BlizzardS3 board for Espressif port.
Any progress on this? |
@smurfix Sorry but I havent spent any time with this. I would suggest forking the branch and rebase to continue with this, and open a PR from that new fork/branch. |
In CPython 3.8 the proposed positional only parameter indicator (slash) was introduced.
The proposal: https://peps.python.org/pep-0570/
The python bug report tracking the pep 570 development: https://bugs.python.org/issue36540
In short the pep 570 introduces a slash in the function definition where all parameters to the left of the slash are considered positional only. Therefore, can not be called with keyword.
Example:
Tests were duplicated from the introduction to this syntax from CPython with slight modifications.
This was worked on with the assistance of @norandomtechie