Skip to content

Delete many redundant method redefinitions #6877

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

Merged
merged 8 commits into from
Jan 9, 2022

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Jan 9, 2022

All classes implicitly inherit from object in Python 3, so it's almost never necessary to explicitly define __str__ or __repr__ in the stubs (there are a few exceptions, if it's e.g. marked as an abstract method in a base class).

I haven't touched any of the files in the stubs directory in this PR, as some of those need compatibility with Python 2; this PR only touches the standard-library stubs.

@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member Author

Diff from mypy_primer, showing the effect of this PR on open source code:

zulip (https://github.com/zulip/zulip)
+ corporate/views/billing_page.py:115: error: Unrecognized format specification "%B"  [str-format]
+ corporate/views/billing_page.py:117: error: Unrecognized format specification "%B"  [str-format]

pandas (https://github.com/pandas-dev/pandas)
+ pandas/io/formats/format.py:2065: error: Non-overlapping equality check (left operand type: "Decimal", right operand type: "Literal[0]")  [comparison-overlap]

I assume Decimal.__eq__ and datetime.__format__ are special-cased by mypy in some way. I'll add them back.

@github-actions

This comment has been minimized.

@JelleZijlstra
Copy link
Member

I assume Decimal.eq and datetime.format are special-cased by mypy in some way. I'll add them back.

More likely it distinguishes between classes that override __eq__ or __format__ and ones that just use the signature from object. This makes sense because object.__format__ errors on any non-empty format string, but an override may accept more arguments.

@JelleZijlstra
Copy link
Member

So as a corollary, we should not remove any other __eq__/__format__/__ne__ methods.

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Jan 9, 2022

More likely it distinguishes between classes that override eq or format and ones that just use the signature from object. This makes sense because object.format errors on any non-empty format string, but an override may accept more arguments.

Sure, that makes sense for __format__, but I still don't really get Decimal.__eq__. Should I put all of the __eq__ methods back?

@github-actions

This comment has been minimized.

@JelleZijlstra
Copy link
Member

I don't have time to look into the details right now (got a lot of things to catch up with), but I believe mypy's logic for unsafe == treats overridden __eq__ specially.

@AlexWaygood
Copy link
Member Author

I don't have time to look into the details right now (got a lot of things to catch up with), but I believe mypy's logic for unsafe == treats overridden __eq__ specially.

I'm reverting it as we speak, dw! Was just curious, it's obvs not your job to figure it out for me :)


class _ExceptionWithTraceback:
exc: BaseException
tb: TracebackType
def __init__(self, exc: BaseException, tb: TracebackType) -> None: ...
def __reduce__(self) -> str | tuple[Any, ...]: ...
Copy link
Member

Choose a reason for hiding this comment

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

Not 100% sure about this since I could imagine a type checker checking whether an object can be pickled safely and checking __reduce__ overrides.

Copy link
Member Author

Choose a reason for hiding this comment

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

You've changed your tune ;)

Copy link
Member

Choose a reason for hiding this comment

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

I guess I just default to not making changes when we don't have a clear reason for change :)

Copy link
Member Author

Choose a reason for hiding this comment

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

A sound policy.

@github-actions

This comment has been minimized.

2 similar comments
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 9, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra JelleZijlstra merged commit 4e04616 into python:master Jan 9, 2022
@AlexWaygood AlexWaygood deleted the redundant-methods branch January 9, 2022 19:21
@AlexWaygood
Copy link
Member Author

Thanks!

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.

3 participants