Skip to content

Conversation

@johnslavik
Copy link
Member

@johnslavik johnslavik commented Jan 13, 2026

@johnslavik johnslavik marked this pull request as ready for review January 13, 2026 19:18
Comment on lines 1140 to 1141
wrong_name = getattr(exc_value, "name", None)
if wrong_name is not None and wrong_name in sys.stdlib_module_names:
Copy link
Member Author

@johnslavik johnslavik Jan 13, 2026

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:
Copy link
Member Author

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.

Copy link
Contributor

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)
Copy link
Contributor

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" ?

Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants