gh-150459: Fix SyntaxError message for from x lazy import y#150877
gh-150459: Fix SyntaxError message for from x lazy import y#150877sobolevn wants to merge 4 commits into
SyntaxError message for from x lazy import y#150877Conversation
|
I think we can go further and detect this insignificant whitespace between the |
|
@gpshead I am not sure that this is really possible without introducing breaking changes. # lazy.py
x = 1and # example.py
from . lazy import x # not-pep8 compatible, but working code
print(x)If we change anything - we will break this code :( >>> from . lazy import x
<python-input-0>:2: ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__
Traceback (most recent call last):
File "<python-input-0>", line 2, in <module>
from . lazy import x
ImportError: attempted relative import with no known parent package
Did you mean to use "lazy from . import"? |
is doable matching basically how the decimal literal special case works (thanks claude!). The bulk of the code winds up as in |
|
This is may be a bit hacky though. In the world of the parser there are no more strings: just tokens. Although it's possible to reconstruct the "spacing" via the position information of the tokens this is a bit inelegant (at least with our parser theorist googles on) and I think is too much code for the small edge case that's this. |
|
@gpshead @pablogsal I've implemented a much simplier warning code for this feature. Thanks a lot to @gpshead for the draft. This is now ready to be reviewed. I would really like to get this into 3.15 as well, so it would be in sync with PEP-810 release. @hugovk can we do that, please? :) |
|
Question: this seem unexpected that this warning happens twice in this case What can be the cause of |
|
test_peg_generator fails on the CI "Tests / Ubuntu / build and test (ubuntu-24.04-arm)":
You should export the symbol for test_peg_generator: use |
There's a problem with another branch of
from ... import.Because
from .lazy importandfrom . lazy importis the same thing from the parser's point of view.Things I've tried:
What do I suggest? Maybe we can add an exception note for
from .lazy import ywhenImportErroris raised with a similar message?Update: Thanks a lot to @gpshead for the
SyntaxWarningidea and draft implementation. See #150877 (comment)