gh-113570: reprlib.repr does not use builtin __repr__ for reshadowed builtins#113577
gh-113570: reprlib.repr does not use builtin __repr__ for reshadowed builtins#113577serhiy-storchaka merged 17 commits intopython:mainfrom
Conversation
…thod on shadowed Python built-in types.
|
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 |
sobolevn
left a comment
There was a problem hiding this comment.
Please, also take a look at this doctest failure:
Document: library/reprlib
-------------------------
**********************************************************************
File "library/reprlib.rst", line 481, in default
Failed example:
import reprlib
import sys
class MyRepr(reprlib.Repr):
def repr_TextIOWrapper(self, obj, level):
if obj.name in {'<stdin>', '<stdout>', '<stderr>'}:
return obj.name
return repr(obj)
aRepr = MyRepr()
print(aRepr.repr(sys.stdin)) # prints '<stdin>'
Expected:
<stdin>
Got:
<_io.TextIOWr...oding='utf-8'>
1 items passed all tests:
8 tests in indent
**********************************************************************
1 items had failures:
1 of 7 in default
15 tests in 2 items.
14 passed and 1 failed.
***Test Failed*** 1 failures.
|
To handle the failing doctest some more edge cases need to be handled so cases where someone has defined a custom method still work. To keep something somewhat similar to what we had there a (4) cases. I am saying there is a predefined method for an object of type a if the class has a method
In cases 1 and 4 we will return I have added an explicit unit test for case 2 in my latest commit, cases 1, 3 and 4 are already there. |
|
@sobolevn are you able to look at the updated changes from your comments? |
sobolevn
left a comment
There was a problem hiding this comment.
Generally, looks ok to me, but there are several nitpicks :)
|
Fixed those two comments @sobolevn |
sobolevn
left a comment
There was a problem hiding this comment.
This is the last comment, thank you! :)
…ng builtin "list" calls its own __repr__
|
That's sorted @sobolevn ! |
|
Is this all good now @sobolevn ? |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Please remove unrelated style changes.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Thank you for your update, @georgepittock. One minor nitpick. The rest LGTM.
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
|
CI failure looks unrelated, merging main into branch so trigger CI again... |
|
Thanks @georgepittock for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. |
…dowed builtins (pythonGH-113577) (cherry picked from commit 04d6dd2) Co-authored-by: George Pittock <66332098+georgepittock@users.noreply.github.com>
…dowed builtins (pythonGH-113577) (cherry picked from commit 04d6dd2) Co-authored-by: George Pittock <66332098+georgepittock@users.noreply.github.com>
|
GH-125654 is a backport of this pull request to the 3.13 branch. |
|
GH-125655 is a backport of this pull request to the 3.12 branch. |
Fixes issue: gh-113570 where objects that reshadow certain builtins would call a specific method and could cause errors, e.g.
This uses a mapping of
(module, object name): methodto map to the right method, as mentioned in the issue.