-
-
Notifications
You must be signed in to change notification settings - Fork 31.2k
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
Improve name suggestions for NameError/AttributeError by respecting underscore conventions #116871
Comments
I agree with the pandas example, but I disagree with the _foo = 3
print(foo) For the code above, suggesting using import bar
bar.foo # there's a bar._foo For the same reason, Both existing behaviors that exclude underscores listed are a very clear usage from external. They are valid, but that does not justify we remove underscored variables from |
I would also note that I think that deciding "can we use |
Only include underscored names in name suggestions for AttributeError and ImportError if the original name was underscored.
It is difficult to distinguish |
|
I agree generally that it would be difficult to distinguish If some Of course, IDEs will implement their own suggestions (and auto-completions) anyway.... |
It's very common that someone forgets a method/attribute is internal only. I don't think the suggestion is only for typos. It's helpful to remind the developers that they might've forgotten the underscore. If someone types |
They may not be considered important enough to add special-case handling for in suggestions, but in some instances there are underscore names which are public/documented, e.g. namedtuple types have |
I implemented @gaogaotiantian's suggestions. Is it all? |
Only include underscored names in name suggestions for AttributeError and ImportError if the original name was underscored.
I have merged the PR with @gaogaotiantian's suggestions. If there are other suggestions, they can be implemented in a separate PR. |
Feature or enhancement
Proposal:
Problem description
In more recent versions of Python, for uncaught
NameError
s andAttributeErrors
, the system tries to suggest names that might have been typoed:However, the suggestion logic apparently does not take underscore conventions into account:
Commonly, leading underscores on names are used by convention to mark names that should not be referenced directly in client code. (Of course, it's occasionally necessary or desirable to call dunder methods directly, particularly when inheritance is involved, but generally not outside of classes.)
This has the negative effect, that Python itself could recommend that users invoke undocumented, unstable or unintentional APIs without good reason. One current real-world example of this occurs with Pandas, where version 2.0 removed the
append
method fromDataFrame
s, but there happens to be an_append
method left behind as an implementation detail:Proposal
I propose that: when the invalid identifier or attribute name does not already start with an underscore, valid names that do start with an underscore should be excluded from suggestions. As mentioned in the linked discussion, there is already precedent for this -
help
:It still makes sense IMO to suggest underscore names when the invalid name already starts with an underscore - even mixing and matching single- and double-underscore cases. For example,
_init_
is a very plausible typo for__init__
, especially for beginners who are learning from a book or an old PDF.Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/name-suggestions-for-attributeerrors-and-possibly-nameerrors-should-not-include-names-with-single-leading-underscores/48588
Linked PRs
The text was updated successfully, but these errors were encountered: