-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-113570: reprlib.repr does not use builtin __repr__ for reshadowed builtins #113577
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
gh-113570: reprlib.repr does not use builtin __repr__ for reshadowed builtins #113577
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally, looks ok to me, but there are several nitpicks :)
Fixed those two comments @sobolevn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the last comment, thank you! :)
…ng builtin "list" calls its own __repr__
That's sorted @sobolevn ! |
Is this all good now @sobolevn ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove unrelated style changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your update, @georgepittock. One minor nitpick. The rest LGTM.
Co-authored-by: Serhiy Storchaka <[email protected]>
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 <[email protected]>
…dowed builtins (pythonGH-113577) (cherry picked from commit 04d6dd2) Co-authored-by: George Pittock <[email protected]>
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. |
…adowed builtins (GH-113577) (GH-125655) (cherry picked from commit 04d6dd2) Co-authored-by: George Pittock <[email protected]>
…adowed builtins (GH-113577) (GH-125654) (cherry picked from commit 04d6dd2) Co-authored-by: George Pittock <[email protected]>
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): method
to map to the right method, as mentioned in the issue.