Skip to content

gh-130821: Add type information to wrong type error messages - #130835

Merged
serhiy-storchaka merged 17 commits into
python:mainfrom
donbarbos:issue-130821
Aug 14, 2025
Merged

gh-130821: Add type information to wrong type error messages#130835
serhiy-storchaka merged 17 commits into
python:mainfrom
donbarbos:issue-130821

Conversation

@donbarbos

@donbarbos donbarbos commented Mar 4, 2025

Copy link
Copy Markdown
Contributor

I got next output for use case from issue:

Foo.__int__() must return an int, not NoneType
Foo.__float__() must return a float, not NoneType
Foo.__bytes__() must return a bytes, not NoneType
Foo.__complex__() must return a complex, not NoneType
Foo.__bool__() must return a bool, not NoneType
Foo.__str__() must return a str, not NoneType

but we still have a lot of other places where wrong type errors have inconsistent messages. should they be updated too?

@jstasiak jstasiak left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Drive-by comment: does it make sense to have tests for these changes?

@donbarbos

Copy link
Copy Markdown
Contributor Author

Drive-by comment: does it make sense to have tests for these changes?

it seems that we don't check the text of error messages if we don't want to check the error type itself

Comment thread Objects/abstract.c Outdated
@skirpichev
skirpichev self-requested a review April 28, 2025 13:19

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think many of the changes in wording are not clear improvements.

Comment thread Objects/abstract.c Outdated
Comment thread Objects/abstract.c
Comment thread Objects/abstract.c

if (!PyLong_Check(result)) {
PyErr_Format(PyExc_TypeError,
"__index__ returned non-int (type %.200s)",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't see much reason to change this error message either.

@donbarbos donbarbos Apr 29, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I changed it to the more common way as you recommended elsewhere: __method__() must return an int, not ...
and added type info before method name, like for other methods

Comment thread Objects/abstract.c
PyThreadState *tstate = _PyThreadState_GET();
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError)) {
_PyErr_Format(tstate, PyExc_TypeError,
"%.200s.%U() returned a non-iterable (type %.200s)",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here I strongly prefer the old message. Iterable is not a type, it's a category of types.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

what do you think about new message: "%T.%U() must return an iterable, not %T"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think new message adds something bad. Why someone might think, that iterable is a type here? (Another similar case, where new messages mention an iterator.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Why someone might think, that iterable is a type here?

it's just that in the previous iteration the error message was the phrase "must return type ..." :)

Comment thread Objects/abstract.c
Comment thread Objects/typeobject.c Outdated
@donbarbos
donbarbos requested a review from JelleZijlstra May 16, 2025 18:19
@serhiy-storchaka
serhiy-storchaka self-requested a review July 6, 2025 08:28
@skirpichev
skirpichev removed their request for review August 13, 2025 05:17
Comment thread Objects/abstract.c Outdated
Comment thread Objects/abstract.c Outdated
Comment thread Objects/abstract.c Outdated
Comment thread Objects/typeobject.c Outdated
Comment thread Objects/typeobject.c Outdated
Comment thread Objects/typeobject.c Outdated
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. 👍

@serhiy-storchaka
serhiy-storchaka merged commit 968f6e5 into python:main Aug 14, 2025
48 checks passed
@donbarbos

Copy link
Copy Markdown
Contributor Author

Thank you everyone for review

Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
Cityfishman added a commit to Cityfishman/cpython that referenced this pull request Feb 12, 2026
Update error messages in Modules/ directory to use consistent format
with %T formatter for type names, similar to changes made in PR python#130835
for Objects/ directory.

Changes:
- Modules/_abc.c: items() error message
- Modules/_datetimemodule.c: divmod() error message
- Modules/_pickle.c: read() error message
- Modules/_io/bufferedio.c: read() and readall() error messages
- Modules/_io/iobase.c: read() error message
- Modules/_io/textio.c: decoder and encoder error messages
- Modules/_csv.c: iterator error message
JMak-Security added a commit to JMak-Security/cpython that referenced this pull request Aug 20, 2026
When an item yielded by a class namespace's items() is not iterable
during abstract-method computation, include the type of the offending
item in the raised TypeError, matching the type-information convention
established in pythongh-130835.

This targets the same _abc.c hunk that pythongh-144737 (now stale) proposed,
using PyErr_ExceptionMatches to only replace the message on a genuine
TypeError instead of passing NULL through PySequence_Fast, which risks
a NULL PyErr_SetString call if that path is ever reached with a
different underlying error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants