Skip to content
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

Remove all __nonzero__ methods #8443

Closed
wants to merge 1 commit into from

Conversation

sobolevn
Copy link
Member

I think that it is almost time (maybe after new mypy release) to remove __nonzero__ methods from typeshed.

Why?

I've also removed all __nonzero__ methods from these libs:

Some libs, however, support both python2 and python3 (like python-dateutil).

@github-actions
Copy link
Contributor

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

@AlexWaygood
Copy link
Member

Typeshed no longer supports Python 2 (even if a third-party library supports Python 2, our stubs for that library don't), so I'm fully in support of this! I don't think we need to wait for the next mypy release.

One thing we should do, however, is add __nonzero__ (and probably all other Python-2 magic methods) to this list in stubtest: https://github.com/python/mypy/blob/0d7424c9f80e5695042f21e9d2ea44661d0d6a8a/mypy/stubtest.py#L1130. Otherwise, stubtest will complain about these methods being missing from the stub (unless stubtest is running with --ignore-missing-stub, which is the setting we use for most of our third-party libraries).

@JelleZijlstra
Copy link
Member

I'm not sure we should do this. You can still create a __nonzero__ method in Python 3; it just doesn't do anything magical. We should remove them only when they do not exist at runtime.

@sobolevn
Copy link
Member Author

sobolevn commented Jul 30, 2022

@JelleZijlstra

You can still create a __nonzero__ method in Python 3; it just doesn't do anything magical.

Yes, but in all cases these methods are just copies of __bool__. They are not "something special". I've also removed almost all of them from runtime as well (except python-dateutil, invoke, and tqdm because they do still support python2).

@AlexWaygood
Copy link
Member

AlexWaygood commented Jul 30, 2022

I think @JelleZijlstra has a good point, actually; I've changed my mind. Most of the __nonzero__ methods in typeshed are hangovers from Python 2, but what if some third-party code, that's potentially never supported Python 2, has a method that happens to be called __nonzero__ and means something special in the context of their third-party library or application?

We shouldn't necessarily remove all __nonzero__ methods just because they used to mean something special in the context of Python 2, and now no longer have that special meaning. They might still have other meanings in some circumstances, even if they no longer count as "magic" methods.

@sobolevn
Copy link
Member Author

sobolevn commented Jul 30, 2022

They might still have other meanings in some circumstances, even if they no longer count as "magic" methods.

But they don't in the cases of this PR 🙂
Check out my PRs I've listed in #8443 (comment)
And ones that I've not covered yet:

@AlexWaygood
Copy link
Member

But they don't in the cases of this PR 🙂

Sure -- maybe we should go ahead with this PR, then (after the runtime PRs have been merged), but not go ahead with the stubtest change? 🙂

@sobolevn
Copy link
Member Author

sobolevn commented Jul 30, 2022

but not go ahead with the stubtest change?

I can barely think of any reasonable case where __nonzero__ means somethings different.
And I think that magic methods are meant to be reserved for python-level magic, not something custom.

So, in my opinion ignoring legacy is a good thing. But, I am open for other opinions (especially with real-life use-cases).

@AlexWaygood
Copy link
Member

And I think that magic methods are meant to be reserved for python-level magic, not something custom.

That's how they're meant to be used, for sure, but third-party libraries define their own dunder methods relatively frequently anyway -- e.g. rich has the __rich_repr__ method.

@JelleZijlstra
Copy link
Member

I still don't see a reason for removing __nonzero__ methods from typeshed until after they have been removed in released versions of the libraries we have stubs for. Our job is to report what exists in those libraries, not what should exist.

@sobolevn
Copy link
Member Author

Agreed, let's wait a bit 👍

@srittau
Copy link
Collaborator

srittau commented Jul 31, 2022

I agree with @JelleZijlstra. Let's do whatever the stubbed library does. I'm absolutely in favor of removing __nonzero__ in the libraries themselves, though, if they dropped Python 2 support.

@AlexWaygood
Copy link
Member

We'd rather not make these changes until the various runtime libraries have cut releases which have the __nonzero__ methods removed. And when there are releases with the __nonzero__ methods removed, we'll have to make these changes as part of the version bump, rather than as a standalone PR. So, I'm going to close this for now :)

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.

4 participants