-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
[Bug] f-string with hasattr causes an import error #1954
Comments
I assume this works with Sounds like a bug with the Python parser we use for import detection in Pybricks Code. |
Will try tonight. [EDIT] Why not now.... @dlech
|
So in the end a user syntax error. But a nasty way of showing it. |
Thanks for checking. So more a bug in MicroPython for not catching the syntax error. |
Running import mission_run_1
missions = [("1", irrelevant_name, "first"), ("2", "stop", "stop"),]
print(f"hasattr f-string {hasattr(missions[0][1], "run")}") Shows:
I do not know the phases but I would expect the syntax being found before the import being done. Will look if I can get the "source" more simple to show that the syntax error is not catched. Bert |
I see. The code works fine in MicroPython if there are no imports since MicroPython seems to allow
Pybricks Code uses static analysis tool find imports and silently fails on syntax errors assuming that any syntax error it detects would also be a syntax error in MicroPython. But since this is not a syntax error in MicroPython, the program compiles and runs but the proper imports were not included because the import analyzer detected the syntax error. |
minimal test case: import a
print(f"{"x"}") |
Simplified to: print(f"{"run"}")
print("{"run"}") The first line should report a syntax error, but in these versions the second line does. Tested on micropython v1.22.1+ds-1build2 on 2024-04-01 |
Added cpython issue 127428 Unexpected behavior in f-strings with nested quotes |
JelleZijlstra pointed to PEP 701 – Syntactic formalization of f-strings [EDIT]
So leave this until pybricks uses micropython 1.22? |
I don't see how updating the MicroPython version will make a difference. The problem is that the Python parser for finding imports in Pybricks Code hasn't been updated to support the Python 3.12 syntax. MicroPython isn't involved in this part of the code at all. |
I stand corrected 😄 |
Describe the bug
Using
hasattr
in an f-string causes an import error (There is no import error).The same data in a
.format
does not produce that error.To reproduce
Steps to reproduce the behavior:
ImportError: no module named 'mission_run_1'
ImportError
, but the correctNameError
Expected behavior
The same correct behavior for the f-string and the formatted one.
And most of all: No incorrect flagging the import as error.
Took me quiet some time to assure that the import statement was correct 😄
Screenshots
There is a saying that a picture is worth a 1000 words. Screenshots really help to identify and solve problems.
None sofar.
Extra info
The text was updated successfully, but these errors were encountered: