Skip to content

gh-74185: repr() of ImportError now contains attributes name and path #136770

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

Open
wants to merge 29 commits into
base: main
Choose a base branch
from

Conversation

Yoav11
Copy link

@Yoav11 Yoav11 commented Jul 19, 2025

This PR was created during Europython 2025's sprint weekend.

This is a patch on #1011. authored by serhiy-storchaka and arhadthedev

@python-cla-bot
Copy link

python-cla-bot bot commented Jul 19, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Jul 19, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@Yoav11 Yoav11 changed the title gh-74185: (patch) repr() of ImportError now contains attributes name and path. gh-74185: repr() of ImportError now contains attributes name and path. (patch) Jul 19, 2025
@bedevere-app
Copy link

bedevere-app bot commented Jul 19, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@StanFromIreland
Copy link
Member

You need to sign the CLA.

Requesting @encukou who wanted to finish this per #1011 (comment)

@picnixz picnixz changed the title gh-74185: repr() of ImportError now contains attributes name and path. (patch) gh-74185: repr() of ImportError now contains attributes name and path Jul 19, 2025
@@ -1864,6 +1864,41 @@ ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
return res;
}

static PyObject *
ImportError_repr(PyObject *self)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there other exceptions that will benefit from having keywords in their reprs? AFAICT, the keywords are only rendered if they were passed to the constructor as keyword arguments. In particular, we could make a generic repr helper for that which takes into account them, something like:

static PyObject *
repr_with_keywords(PyObject *exc, const char * const *kwlist)
{
	/* format using kwlist[i]=getattr(exc, kwlist[i]) 
     * with kwlist NULL-terminated */
}

Its usage would be

static char *kwlist[] = {"name", "path", NULL};
repr_with_keywords(exc, kwlist)

Copy link
Member

@picnixz picnixz Jul 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, let's hold off this idea as I don't know if it can be useful. But this can be worth investigating instead of storing the given keywords explicitly.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes - AFAIK only ImportError and ModuleNotFoundError (which hinnerits this functionality as per the tests) have optional arguments ? but makes sense to me that if other exception types in the future have optionals then we could extract this out.

Copy link
Member

@picnixz picnixz Jul 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but let's do it in a follow-up PR so that we can revert the helper commit more easily. Also it depends on whether we would store the keywords passed to the constructor (this is #11580)

@ynir3
Copy link

ynir3 commented Jul 19, 2025

@picnixz any more comments ? :)

@ynir3
Copy link

ynir3 commented Jul 20, 2025

@picnixz anything else I can improve ?

@Yoav11 Yoav11 marked this pull request as ready for review July 20, 2025 07:46
@Yoav11 Yoav11 requested a review from iritkatriel as a code owner July 20, 2025 07:46
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a What's New entry as well (in Doc/whatsnew/3.15.rst)

@Yoav11 Yoav11 requested a review from picnixz July 20, 2025 08:48
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good now. I'll let @serhiy-storchaka have a look as well.

@Yoav11
Copy link
Author

Yoav11 commented Jul 20, 2025

This looks good now. I'll let @serhiy-storchaka have a look as well.

thanks for the review @picnixz ! 🙏

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.

8 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy