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

Dirty workaround for mypy 1.5 error #8142

Merged
merged 2 commits into from
Sep 7, 2023
Merged

Conversation

benbovy
Copy link
Member

@benbovy benbovy commented Sep 4, 2023

I wanted to fix the following error with mypy 1.5:

xarray/core/dataset.py:505: error: Definition of "__eq__" in base class "DatasetOpsMixin" is incompatible with definition in base class "Mapping"  [misc]

Which looks similar to python/mypy#9319. It is weird that here it worked with mypy versions < 1.5, though.

I don't know if there is a better fix, but I thought that redefining __eq__ in Dataset would be a bit less dirty workaround than adding type: ignore in the class declaration.

@Illviljan
Copy link
Contributor

Illviljan commented Sep 4, 2023

Here's an interesting read:

https://stackoverflow.com/questions/37557411/why-does-defining-the-argument-types-for-eq-throw-a-mypy-type-error

I can't check myself but does it work to just remove the Mapping base class?

@benbovy
Copy link
Member Author

benbovy commented Sep 4, 2023

I found this SO post too, but I think that the issue here is rather specific to how mypy handles (mishandles?) multiple inheritance. I don't see any error reported using pyright. Also mypy doesn't complain anymore if I move the Mapping base class declaration to DatasetOpsMixin in order to remove multiple inheritance of __eq__:

# in xarray/core/_typed_ops.pyi

from abc import ABCMeta

class DatasetOpsMixin(
    Mapping[Hashable, "DataArray"],
    metaclass=ABCMeta,
):
    ...

# in xarray/core/dataset.py

class Dataset(
    DataWithCoords,
    DatasetAggregations,
    DatasetArithmetic,
):
    ...

But that is not a very nice solution either.

Simply removing the Mapping base class breaks a few things (key lookup, etc.) and removes important typing information.

@max-sixty
Copy link
Collaborator

Ah, this is a better version of #8155, sorry — I missed it.

If it's OK, I'll merge main in here and see whether that passes?

@max-sixty max-sixty mentioned this pull request Sep 7, 2023
@max-sixty
Copy link
Collaborator

This passes the mypy check!

I've been out of the loop for a while, so don't want m return to be merging things I don't have much context for. But +1 on merging this, and will hit the button in a day or so unless someone objects.

@Illviljan
Copy link
Contributor

Id prefer a solution without a type: ignore, they're too easy to add and aren't questioned enough.

But i dont have the time to look into it now either so no strong opinions here.

@max-sixty
Copy link
Collaborator

Id prefer a solution without a type: ignore, they're too easy to add and aren't questioned enough.

(I agree, but it doesn't look easy — such that holding the line against ignores may not inspire a better fix — but again I'm not sure)

@benbovy
Copy link
Member Author

benbovy commented Sep 7, 2023

Let's merge this now so that we can have a few more green checks in main and in the updated PRs?

I asked upstream about the status of this issue. Hopefully this will be fixed at some point.

The workaround here looks reasonable to me, it doesn't add more type: ignore than there was before. Mypy still goes through all the Dataset class hierarchy.

@benbovy benbovy enabled auto-merge (squash) September 7, 2023 08:20
@benbovy benbovy disabled auto-merge September 7, 2023 08:21
@benbovy benbovy merged commit e2b6f34 into pydata:main Sep 7, 2023
@max-sixty
Copy link
Collaborator

We have green CI!

image

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