Skip to content

Enforce ruff/flake8-simplify rules (SIM) #10462

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
Jun 30, 2025
Merged

Conversation

DimitriPapadopoulos
Copy link
Contributor

  • Closes #xxxx
  • Tests added
  • User visible changes (including notable bug fixes) are documented in whats-new.rst
  • New functions/methods are listed in api.rst

@@ -8980,7 +8979,7 @@ def pad(
variables[name] = var
elif name in self.data_vars:
if utils.is_dict_like(constant_values):
if name in constant_values.keys():
if name in constant_values:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see how this change causes the new mypy errors:

Found 2 errors in 1 file (checked 191 source files)
xarray/core/dataset.py:8986: error: Value expression in dictionary comprehension has incompatible type "float | tuple[float, float] | Mapping[Any, float | tuple[float, float]]"; expected type "float | tuple[float, float]"  [misc]
xarray/core/dataset.py:8991: error: Incompatible types in assignment (expression has type "float | tuple[float, float] | Mapping[Any, float | tuple[float, float]] | Mapping[Any, float | tuple[float, float] | Mapping[Any, float | tuple[float, float]]] | None", variable has type "float | tuple[float, float] | Mapping[Any, float | tuple[float, float]]")  [assignment]

Wasn't this a pre-existing issue? If so, not sure why mypy only flags it after this change.

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes agree it's confusing...

@DimitriPapadopoulos DimitriPapadopoulos marked this pull request as ready for review June 28, 2025 21:09
SIM102 Use a single `if` statement instead of nested `if` statements
SIM113 Use `enumerate()` for index variable in `for` loop
SIM114 Combine `if` branches using logical `or` operator
@DimitriPapadopoulos DimitriPapadopoulos force-pushed the SIM branch 2 times, most recently from 4ba8c59 to b2ac9ec Compare June 29, 2025 13:50
SIM118 Use `key in dict` instead of `key in dict.keys()`
SIM905 Consider using a list literal instead of `str.split`
SIM910 Use `.get(...)` instead of `.get(..., None)`
@max-sixty max-sixty merged commit 5d5bc5b into pydata:main Jun 30, 2025
33 checks passed
@keewis
Copy link
Collaborator

keewis commented Jun 30, 2025

it looks like there's a SIM118 error on main now. I'd argue that we should not accept that particular one (iterating over a dataset feels confusing to me: are we iterating over all variables, coordinates, or data vars?), or if we do we should access the appropriate property to be explicit about what would happen (i.e. access .data_vars, .coords, or .variables)

@max-sixty
Copy link
Collaborator

sorry re error, I'll revert. (but why no error in the tests?)

no strong view re datasets; I was partial to the proposed code but no strong view

@keewis
Copy link
Collaborator

keewis commented Jun 30, 2025

no idea why it didn't fail here.

to be clear, the difference is between iterating over self.ds or self.ds.keys(). Dataset is dict-like, but it's usually not clear what we iterate over by default (I think data variables only?). So while we can for sure omit the .keys(), I'd argue self.ds.data_vars would be much easier to understand (or similar, should I have forgotten the behavior of ds.keys()).

Edit: thanks for reverting, although I think fixing the failing check in a new PR would have also worked

@DimitriPapadopoulos
Copy link
Contributor Author

My wrong, if it's a dataset, obviously keeping keys() is better. Besides, the SIM rules are more error-prone, that's why they're often unsafe rules with no auto fixes. Would you like to ignore rule SIM118 globally or use noqa on a case by case basis?

@max-sixty
Copy link
Collaborator

I would probably go by rule rather than adding noqas but no strong view!

@DimitriPapadopoulos
Copy link
Contributor Author

DimitriPapadopoulos commented Jul 1, 2025

I would rather ignore globally too, see #10480 (this time without SIM118).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants