-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-143811: Add helpful default error messages to AttributeError and NameError
#143813
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
base: main
Are you sure you want to change the base?
gh-143811: Add helpful default error messages to AttributeError and NameError
#143813
Conversation
| wrong_name = getattr(exc_value, "name", None) | ||
| if wrong_name is not None and wrong_name in sys.stdlib_module_names: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of scope, within the same function: wrong_name = ... and wrong_name is not None and could be removed here.
| elif exc_type and issubclass(exc_type, (NameError, AttributeError)) and \ | ||
| getattr(exc_value, "name", None) is not None: | ||
| wrong_name = getattr(exc_value, "name", None) | ||
| (wrong_name := getattr(exc_value, "name", None)) is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This semantic change reduces the possibility of wrong_name being None completely. I'm convinced that this was the original intent of the author.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This all looks good to me. I think you're right about the original intent. Likely they wanted to check if the attribute name exists on the exception, and only then proceed with formatting suggestions.
| obj_type_name = object.__getattribute__(obj_type, "__name__") | ||
| self._str = f"{obj_type_name!r} object has no attribute {wrong_name!r}" | ||
| else: # NameError | ||
| self._str = repr(wrong_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency with the existing error patterns should we change this to self._str = f"name {wrong_name!r} is not defined" ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! I've missed it, thanks. Good idea. I'll fix it.
AttributeErrorin__getattr__shows a garbled message with attribute suggestions #143811CC @a12k for review