gh-130821: Add type information to error messages for remaining magic methods#144827
Open
RoryGlenn wants to merge 1 commit intopython:mainfrom
Open
gh-130821: Add type information to error messages for remaining magic methods#144827RoryGlenn wants to merge 1 commit intopython:mainfrom
RoryGlenn wants to merge 1 commit intopython:mainfrom
Conversation
… magic methods Add type information to error messages for __getnewargs__, __getnewargs_ex__, __hash__, and __init__ when they return the wrong type. This is a follow-up to pythonGH-130835, which fixed similar inconsistencies for other magic methods. Error messages now use the format 'Type.__method__() must return ...' consistently, replacing the old format that used '%.200s' for type names and lacked the type prefix. Changes: - __getnewargs_ex__: 'should return' -> '%T.__getnewargs_ex__() must return' - __getnewargs__: 'should return' -> '%T.__getnewargs__() must return' - __hash__: '__hash__ method should return an integer' -> '%T.__hash__() must return an integer, not %T' - __init__: 'should return None' -> 'must return None' with %T format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to GH-130835 (merged). Fixes remaining inconsistent error messages for magic methods that return the wrong type in
Objects/typeobject.candPython/bytecodes.c.Changes
Updated error messages to use the consistent
%T.__method__() must return ...format with%Ttype specifier:__getnewargs_ex____getnewargs_ex__ should return a tuple, not '%.200s'%T.__getnewargs_ex__() must return a tuple, not %T__getnewargs____getnewargs__ should return a tuple, not '%.200s'%T.__getnewargs__() must return a tuple, not %T__hash____hash__ method should return an integer%T.__hash__() must return an integer, not %T__init__(typeobject.c)__init__() should return None, not '%.200s'%T.__init__() must return None, not %T__init__(bytecodes.c)__init__() should return None, not '%.200s'__init__() must return None, not %TAlso updated related messages for
__getnewargs_ex__tuple length validation and sub-item type checks.Example output
Files changed
Objects/typeobject.c— Main error message updatesPython/bytecodes.c—EXIT_INIT_CHECKinstructionPython/generated_cases.c.h— Auto-generated from bytecodes.cPython/executor_cases.c.h— Auto-generated from bytecodes.cModules/_testinternalcapi/test_cases.c.h— Auto-generated from bytecodes.c