Skip to content

py/modmicropython: Expose repl_autocomplete as python function. #17011

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andrewleech
Copy link
Contributor

Summary

The mp_repl_autocomplete() function in py/repl.c provides the tab complete functionality for the standard repl.
It would be highly beneficial to also add tab completion to aiorepl which requires exposing this function to python.

This PR also includes a minor change to py/repl: Skip private variables when printing tab completion options.
Any '_' variables/functions in frozen modules are currently printed which is fixed by this.

Testing

While the aiorepl integration hasn't been written yet, this PR is only exposing existing functionality and includes basic unit tests.

Copy link

Code size report:


@andrewleech andrewleech force-pushed the expose_mp_repl_autocomplete branch from e1aa321 to 5dd9831 Compare March 26, 2025 01:13
@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Mar 27, 2025
@mattytrentini
Copy link
Contributor

Adding autocomplete for aiorepl is very desirable, and this is looking promising! Here are some of the tests I ran:

>>>import micropython
>>>micropython.repl_autocomplete("impo")  # Should complete 'import'
'rt '
>>> class Foo:
...     def _bar():
...         pass
...     def alpha():
...         pass
>>> micropython.repl_autocomplete("f = Fo") # Should complete Foo
'o'
>>>f = Foo()
>>> micropython.repl_autocomplete("f.") # Should complete alpha (ignoring _bar)
'alpha'

Note that this is correct and consistent with MicroPython's built-in tab completion - but differs in some cases to CPython, at least at v3.12. CPython will also supply parentheses, ie it would return 'o()' to complete f = Fo and 'alpha(' for f.. That's not a problem with this PR, but should be resolved in mp_repl_autocomplete.

I also tested a module with private members and they were also correctly filtered out.

Looks good!

@Josverl
Copy link
Contributor

Josverl commented Apr 5, 2025

but differs in some cases to CPython, at least at v3.12.

Perhaps that should just be documented as such. I'd be fine with consistent behavior with the MicroPython repl.

py/repl.c Outdated
@@ -218,7 +218,7 @@ static void print_completions(const mp_print_t *print,
for (qstr q = q_first; q <= q_last; ++q) {
size_t d_len;
const char *d_str = (const char *)qstr_data(q, &d_len);
if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0) {
if (s_len <= d_len && strncmp(s_start, d_str, s_len) == 0 && d_str[0] != '_') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please break this out into a separate PR. It'll need changes to the tests as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split out to #17108 with a unit test (which might need some more updating to ensure the test is consistent / works across ports etc?)

@andrewleech
Copy link
Contributor Author

Ok I'm really not sure why the unit tests are still failing on some builds; unix standard in particular. The test passes for me locally with the same build.
Are local variable names somehow handled differently in CI that they're not visible to the auto complete function?
I only just found/ looked at the cmdline repl autocomplete unit test, I should rewrite the test for this module to use test cases more similar to that.

Used to add tab completion to aiorepl.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
@andrewleech andrewleech force-pushed the expose_mp_repl_autocomplete branch from bdb98b0 to 6709f5e Compare May 6, 2025 21:33
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.

5 participants