Repository navigation
Typo hint message for from-imports? #91058
Description
Activity
Jean-Abou-Samra commented
on Mar 2, 2022 Jean-Abou-SamramannequinMannequinAuthorMore actionsSee for example:
>>> from pygments.regexopt import regexopt Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: cannot import name 'regexopt' from 'pygments.regexopt' (/home/jean/repos/pygments/pygments/regexopt.py) >>> from pygments.regexopt import regex_opt >>> regexopt Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'regexopt' is not defined. Did you mean: 'regex_opt'?
In Python 3.10, there is a nice helpful error message suggesting
how to correct the typo for NameError and AttributeError. Would
it be possible to have the same for ImportError with 'from import'?- added3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancementA feature request or enhancement
on Mar 2, 2022 I evaluated this, but is considerably more complex than the regular import, because one triggers an attribute error where we have the full module but in the second case we don't have the full module ready, so it requires considerable modifications.
I will investigate again if there is a way that doesn't require lots of changes, but is likely that we unfortunately need to reject this improvement :(
Pablo's reply hints that the added complexity would not be worth it. I suggest to consider closing this as rejected, unless Pablo found a less complex way :)
- addedpendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
on Aug 16, 2022 Perhaps it is doable within CPython without too much added code since is something that friendly_traceback can do.
[1]: from pygments.regexopt import regexopt Traceback (most recent call last): Code block [1], line 1 from pygments.regexopt import regexopt ImportError: cannot import name 'regexopt' from 'pygments.regexopt' (C:\Users\Andre.Roberge\AppData\Local\Programs\Python\Python310\lib\site-packages\pygments\regexopt.py) Did you mean regex_opt? [2]: why() Perhaps you meant to import regex_opt (from pygments.regexopt) instead of regexopt [3] import sys; [4] 'pygments' in sys.modules True@aroberge You comment shows that friendly_traceback does it but unfortunately, it doesn't mention how you are proposing us to do it so we cannot evaluate the solution :( Could you add a brief sketch of how you are doing it in
friendly_traceback?@pablogsal
friendly_tracebacklooks at the list of modules found insys.modulesand, if the desired module is found, it proceeds from there.The relevant code starts at:
https://github.com/friendly-traceback/friendly-traceback/blob/cdf87b1a623c4afcab4ad869eb29fe885e2d0621/friendly_traceback/runtime_errors/import_error.py#L104Perhaps the way that the information available to friendly_traceback after an exception has been raised is not yet available to CPython at that point, but I thought I should mention this possibility.
@pablogsal
friendly_tracebacklooks at the list of modules found insys.modulesand, if the desired module is found, it proceeds from there.The relevant code starts at: friendly-traceback/friendly-traceback@
cdf87b1/friendly_traceback/runtime_errors/import_error.py#L104Perhaps the way that the information available to friendly_traceback after an exception has been raised is not yet available to CPython at that point, but I thought I should mention this possibility.
The problem with this approach is that if we do this every time the interpreter raises an
ImportErrorwe will slow down the interpreter when someone is catching said exception and continuing so they will have to pay an important (unbounded) cost for a message they won't see. That is what I am trying to circumvent. We cannot also install any exception hooks because of bootstrapping problems and because the interpreter needs to raise them without executing Python in some scenarios.Reacted by Erlend E. AaslandUnderstood; most Python users (myself included) do appreciate this emphasis on speed. Those requiring better traceback analysis can use third-party packages, like friendly_traceback.
Reacted by Erlend E. AaslandYeah, I think this approach is not very useful for CPython. A lot of stdlib modules try to import their C implementation first, else fall back to a Python implementation. I think we can safely close this.
- Reacted by Erlend E. Aasland, Zac Hatfield-Dodds and Oleh Rybalchenko
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: