Skip to content

py/repl: Skip private variables when printing tab completion options. #17108

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 19, 2025

Conversation

andrewleech
Copy link
Contributor

Summary

The tab completion on repl is intended to not show any attributes starting with a single _ character, however variables/functions in frozen modules are are currently shown.

This PR introduces a change to py/repl to skip private variables when printing tab completion options.

Originally included in #17011

Testing

A unit test has added which covers this, currently based on the asyncio module which is generally frozen in.
However looking again now maybe it would be better to add a suitable function / attribute to ./tests/frozen/frozentest.py and test against that, assuming it should have the same behavior?

Copy link

github-actions bot commented Apr 10, 2025

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:   +41 +0.022% 
   unix x64:   +32 +0.004% standard
      stm32:   +32 +0.008% PYBV10
     mimxrt:   +32 +0.009% TEENSY40
        rp2:   +16 +0.002% RPI_PICO_W
       samd:   +24 +0.009% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:   +20 +0.004% VIRT_RV32

@dpgeorge
Copy link
Member

It's actually not easy to make a test for this that is robust because the bug relies on a certain ordering of qstrs in the frozen set of qstrs (it needs qstrs beginning with underscore to appear between the first and last items that match the tab completion).

So maybe it's not worth adding a test for? Does the test you added fail prior to the fix in this PR?

@dpgeorge dpgeorge added py-core Relates to py/ directory in source board-definition New or updated board definition files. Combine with a port- label. and removed board-definition New or updated board definition files. Combine with a port- label. labels Apr 10, 2025
@andrewleech
Copy link
Contributor Author

I'll test without the fix again; asyncio did include an _attr in the tab completion when I tested it manually with standard Unix build beforehand though.

This test also highlighted an issue in the original version of this commit in that it was also hiding __foo__ style attributes too.

@andrewleech andrewleech force-pushed the fix-repl-autocomplete branch 2 times, most recently from 9dceed4 to ee5694f Compare April 11, 2025 12:51
@andrewleech andrewleech force-pushed the fix-repl-autocomplete branch from ee5694f to 4f060bb Compare May 21, 2025 21:14
@andrewleech
Copy link
Contributor Author

Curiosity - is the code size change different if it's all in one if statement?

@andrewleech andrewleech force-pushed the fix-repl-autocomplete branch 2 times, most recently from ee23d3c to bfbb700 Compare June 5, 2025 12:15
@dpgeorge
Copy link
Member

CI needs fixing on this.

@andrewleech andrewleech force-pushed the fix-repl-autocomplete branch from bfbb700 to e824afd Compare June 17, 2025 21:22
Copy link

codecov bot commented Jun 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.56%. Comparing base (c16a4db) to head (09541b7).
Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #17108   +/-   ##
=======================================
  Coverage   98.56%   98.56%           
=======================================
  Files         169      169           
  Lines       21948    21950    +2     
=======================================
+ Hits        21633    21636    +3     
+ Misses        315      314    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@andrewleech
Copy link
Contributor Author

@dpgeorge I've got a working test now I think should be reliable.

Added I minor change to the repl test support in run-tests.py to allow using ctrl-keys in these tests, can you think of any negative side effects of this?

@dpgeorge
Copy link
Member

Added I minor change to the repl test support in run-tests.py to allow using ctrl-keys in these tests, can you think of any negative side effects of this?

I see it's only used for "special" tests, which limits its scope (that's good!). So probably OK.

But, why does it need to run code using paste mode? It seems like it should work just constructing that class normally?

@andrewleech
Copy link
Contributor Author

But, why does it need to run code using paste mode? It seems like it should work just constructing that class normally?

When the class code it "typed" into repl via pty the auto-indent throws it off. I could flatten the class in the .py file, but then would still need backspace to de-dent in between each function.

@dpgeorge
Copy link
Member

then would still need backspace to de-dent in between each function

The existing test repl_autocomplete.py has backspace in it as a raw byte. You could follow that.

I'm not against adding an escape sequence parser, but if it's easy to work around that, might be better not to add such complexity?

@dpgeorge
Copy link
Member

I guess we don't have any tests for paste mode, so maybe worth adding that anyway, using escape sequences?

@andrewleech andrewleech force-pushed the fix-repl-autocomplete branch from e5aeb99 to 0153e24 Compare June 18, 2025 03:00
@andrewleech
Copy link
Contributor Author

I've updated / simplified the escape code parser a little in the latest push as suggested earlier.

I guess we don't have any tests for paste mode, so maybe worth adding that anyway, using escape sequences?

I had thought that, I went looking and didn't find any.
I didn't realise backspace and similar codes could just be added (with a hex editor etc) would you prefer that used for this / paste test, rather than the regex based parser?
Would you prefer a separate explicit paste test as well?

Taking a step back... the test I've added here seems to pass even without the fix in both ./run-tests.py cmdline/repl_autocomplete_underscore.py and ./run-tests.py --via-mpy cmdline/repl_autocomplete_underscore.py - is it only when frozen into the build it fails? Assuming that's the case, I guess this test needs to be partly integrated into

freeze_mpy("$(MPY_DIR)/tests/frozen")
instead.

@dpgeorge
Copy link
Member

I didn't realise backspace and similar codes could just be added (with a hex editor etc) would you prefer that used for this / paste test, rather than the regex based parser?

No, I think what you added here is an improvement, much easier to see what's going on in the test with the escape sequences.

Would you prefer a separate explicit paste test as well?

Yes, that would be nice, but not necessary. Only if you have time.

Taking a step back... the test I've added here seems to pass even without the fix in both ./run-tests.py cmdline/repl_autocomplete_underscore.py and ./run-tests.py --via-mpy cmdline/repl_autocomplete_underscore.py - is it only when frozen into the build it fails?

No, it's tricky. It really depends on what qstrs are frozen and the order they appear in. And the test behaviour may change if/when more scripts are frozen. So I think we should just leave this PR as-is.

@andrewleech andrewleech force-pushed the fix-repl-autocomplete branch from 0153e24 to 73b3bd1 Compare June 18, 2025 12:04
@dpgeorge
Copy link
Member

CI is failing again...

@andrewleech andrewleech force-pushed the fix-repl-autocomplete branch from 73b3bd1 to f3362c9 Compare June 19, 2025 04:18
@andrewleech
Copy link
Contributor Author

CI is failing again...

Fixed again; needed dos2unix on the exp files!.
I've added a repl paste test as well.

@dpgeorge
Copy link
Member

Thanks for fixing, it looks good now.

And thanks for the paste test, that's also looking good!

Note: doing tab completion on import <tab> will no longer print modules beginning with an underscore, eg _thread. I guess that's unavoidable, and actually correct behaviour.

pi-anl added 3 commits June 19, 2025 17:23
This allows having {\xDD} in tests, which will be expanded to the given
hex character.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Any '_' variables/functions in frozen modules are currently printed, when
they shouldn't be.  That's due to underscore names possibly existing
between the start and end qstrs which are used to print the auto-complete
matches.  The underscore names should be skipped when iterating between the
two boundary qstrs.

The underscore attributes are removed from the extra coverage exp file
because tab completing "import <tab>" no longer lists modules beginning
with an underscore.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
@dpgeorge dpgeorge force-pushed the fix-repl-autocomplete branch from f3362c9 to 09541b7 Compare June 19, 2025 07:24
@dpgeorge dpgeorge merged commit 09541b7 into micropython:master Jun 19, 2025
68 checks passed
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.

3 participants