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

Code cleanup #5234

Merged
merged 19 commits into from
May 13, 2021
Merged

Code cleanup #5234

merged 19 commits into from
May 13, 2021

Conversation

andersy005
Copy link
Member

@andersy005 andersy005 commented Apr 29, 2021

This PR introduces small changes to enhance code maintainability...

  • Passes pre-commit run --all-files

Copy link
Collaborator

@max-sixty max-sixty left a comment

Choose a reason for hiding this comment

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

Nice! Thanks @andersy005

Copy link
Collaborator

@mathause mathause left a comment

Choose a reason for hiding this comment

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

Thanks @andersy005 - I am not so sure about the inline dicts: once they begin to span more than 2 lines I find them more difficult than helpful.

xarray/backends/api.py Outdated Show resolved Hide resolved
xarray/backends/netCDF4_.py Outdated Show resolved Hide resolved
xarray/coding/strings.py Outdated Show resolved Hide resolved
xarray/backends/scipy_.py Outdated Show resolved Hide resolved
xarray/coding/strings.py Outdated Show resolved Hide resolved
xarray/core/coordinates.py Outdated Show resolved Hide resolved
xarray/core/utils.py Outdated Show resolved Hide resolved
xarray/core/utils.py Outdated Show resolved Hide resolved
else:
extend = "neither"
return extend
return "neither"
Copy link
Collaborator

Choose a reason for hiding this comment

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

(I don't like the pattern so much but you could remove the else).

xarray/backends/api.py Show resolved Hide resolved
Copy link
Collaborator

@keewis keewis left a comment

Choose a reason for hiding this comment

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

thanks, @andersy005.

The multiline dict comprehensions are a matter of taste (I do prefer them over item assignment in a loop where possible), but as far as I can tell the only difference is the speed of the loop evaluation in CPython (dict comprehensions iterate in C)

xarray/backends/api.py Outdated Show resolved Hide resolved
xarray/backends/h5netcdf_.py Show resolved Hide resolved
xarray/backends/netCDF4_.py Show resolved Hide resolved
xarray/backends/plugins.py Outdated Show resolved Hide resolved
xarray/core/accessor_dt.py Outdated Show resolved Hide resolved
xarray/core/accessor_str.py Show resolved Hide resolved
xarray/core/alignment.py Outdated Show resolved Hide resolved
level_list = []
names = []

for i, index in enumerate(indexes):
Copy link
Collaborator

Choose a reason for hiding this comment

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

since i is only used once, you could also use zip(indexes, repeat_counts) instead of enumerate(...)

xarray/core/utils.py Outdated Show resolved Hide resolved
xarray/core/variable.py Outdated Show resolved Hide resolved
Co-authored-by: keewis <keewis@users.noreply.github.com>
Co-authored-by: Mathias Hauser <mathause@users.noreply.github.com>
@pep8speaks

This comment has been minimized.

xarray/backends/h5netcdf_.py Show resolved Hide resolved
xarray/backends/netCDF4_.py Outdated Show resolved Hide resolved
xarray/backends/netCDF4_.py Show resolved Hide resolved
xarray/coding/strings.py Outdated Show resolved Hide resolved
xarray/core/accessor_str.py Outdated Show resolved Hide resolved
xarray/backends/api.py Outdated Show resolved Hide resolved
xarray/backends/api.py Show resolved Hide resolved
@alexamici
Copy link
Collaborator

I feel this PR is too big and diverse to accept it as a whole.

For example I like most changes from older style formatting to f-strings, but I disagree strongly the moving the return statements into functions improves readability.

xarray/backends/api.py Outdated Show resolved Hide resolved
@max-sixty
Copy link
Collaborator

I feel this PR is too big and diverse to accept it as a whole.

For example I like most changes from older style formatting to f-strings, but I disagree strongly the moving the return statements into functions improves readability.

While I don't have the same sense on the specifics, if people feel strongly then it may be reasonable to revert or pause on some of these.

What do you suggest re the broader PR though? While focused PRs are easier to review and reach agreement — now this is here, it does seem on net beneficial, and we should take advantage of the benefit.

Are there a few items you feel strongly about that we could revert and then merge the rest?

@alexamici
Copy link
Collaborator

@max-sixty & @andersy005 mine was more of a general comment. It is OK to merge it for me, but please after the release!

@andersy005
Copy link
Member Author

I feel this PR is too big and diverse to accept it as a whole.

For example I like most changes from older style formatting to f-strings, but I disagree strongly the moving the return statements into functions improves readability.

If folks are in favor of the original style (inline variables that are immediately returned) throughout the codebase, I'm happy to revert back to the original style :)

xarray/backends/netCDF4_.py Show resolved Hide resolved
xarray/backends/h5netcdf_.py Outdated Show resolved Hide resolved
xarray/backends/netCDF4_.py Outdated Show resolved Hide resolved
xarray/backends/plugins.py Outdated Show resolved Hide resolved
xarray/backends/zarr.py Outdated Show resolved Hide resolved
xarray/coding/variables.py Outdated Show resolved Hide resolved
xarray/core/duck_array_ops.py Outdated Show resolved Hide resolved
xarray/core/groupby.py Outdated Show resolved Hide resolved
xarray/core/merge.py Outdated Show resolved Hide resolved
xarray/core/rolling.py Outdated Show resolved Hide resolved
@shoyer
Copy link
Member

shoyer commented May 7, 2021

@andersy005 thanks for your hard work here. On the whole, I think this will be a net benefit to the codebase, but in my review I pointed out a number of locations where I did not find the style to be improved (in my opinion). There is also one place where I think your logic may have an error.

It would be nice to come up with a policy around "style clean-up" PRs for the future. In general, I think these are not a good idea, for several reasons:

  1. Even code that has been carefully checked can introduce bugs. Style clean-up PRs thus still need to be reviewed carefully.
  2. Fewer lines or tokens of code is not always better. Good examples from this PR include reduced nesting with early return statements, comprehensions and conditional expressions. All of these would be unobjectionable to use in new code (and are often a good idea), but code that doesn't use them even when it could is fine, too.
  3. Some ugly code works fine, and would not benefit from clean-up. For example, xarray.util.print_versions (which was forked from pandas) is quite ugly code. But it's peripheral to the project, and has barely been touched aside from automated reformatting.

Instead, I would suggest a policy of a selective cleanup only. Feel free to clean-up style when it arises in the process of normal coding, e.g., when you are modifying nearly or related code for another reason. This reduces the risk of introducing inadvertent errors, and the fact that you were working on nearby code means that this part of the codebase is worthy of improvement.

Code-base wide clean-up for particular issues may be warranted occasionally, but I would suggest always discussing it with other core developers first.

@andersy005
Copy link
Member Author

@shoyer, Thank you for the thorough feedback.

Even code that has been carefully checked can introduce bugs. Style clean-up PRs thus still need to be reviewed carefully.

Fewer lines or tokens of code is not always better. Good examples from this PR include reduced nesting with early return statements, comprehensions and conditional expressions. All of these would be unobjectionable to use in new code (and are often a good idea), but code that doesn't use them even when it could is fine, too.

👍🏽. I reverted back to the original style in most places. There're a few comments that I haven't addressed yet. I will look into those sometime this weekend.

Code-base wide clean-up for particular issues may be warranted occasionally, but I would suggest always discussing it with other core developers first.

I agree. I didn't expect the PR to get this large. My original intent when I created the PR was to clean the backends code, and I ended up invading other areas :(. In the future, I'll definitely make sure to start a discussion before working on changes that may end up affecting a large chunk of the code base.

@max-sixty
Copy link
Collaborator

This is going to collect merge conflicts — shall we merge?

@andersy005
Copy link
Member Author

@max-sixty, I'm going to address a few issues pointed out during the review, then we can merge afterwards.

@dcherian
Copy link
Contributor

Shall we also add parts of Stephan's comment to the "Contributing Guide" in the documentation?

@andersy005
Copy link
Member Author

Shall we also add parts of Stephan's comment to the "Contributing Guide" in the documentation?

👍🏽 for addressing this in a separate PR

@andersy005
Copy link
Member Author

I believe I have addressed most, if not all changes requested during the review. If there are still disputable changes that need addressing, please let me know

xarray/coding/variables.py Outdated Show resolved Hide resolved
@dcherian
Copy link
Contributor

Looks like this is done. Thanks @andersy005

@dcherian dcherian merged commit 1a7b285 into pydata:master May 13, 2021
dcherian added a commit to TomNicholas/xarray that referenced this pull request May 13, 2021
* upstream/master:
  combine keep_attrs and combine_attrs in apply_ufunc (pydata#5041)
  Explained what a deprecation cycle is (pydata#5289)
  Code cleanup (pydata#5234)
  FacetGrid docstrings (pydata#5293)
  Add whats new for dataset interpolation with non-numerics (pydata#5297)
  Allow dataset interpolation with different datatypes (pydata#5008)
  Flexible indexes: add Index base class and xindexes properties (pydata#5102)
  pre-commit: autoupdate hook versions (pydata#5280)
  convert the examples for apply_ufunc to doctest (pydata#5279)
  fix the new whatsnew section
  Ensure `HighLevelGraph` layers are `Layer` instances (pydata#5271)
dcherian added a commit to matzegoebel/xarray that referenced this pull request May 13, 2021
* upstream/master: (23 commits)
  combine keep_attrs and combine_attrs in apply_ufunc (pydata#5041)
  Explained what a deprecation cycle is (pydata#5289)
  Code cleanup (pydata#5234)
  FacetGrid docstrings (pydata#5293)
  Add whats new for dataset interpolation with non-numerics (pydata#5297)
  Allow dataset interpolation with different datatypes (pydata#5008)
  Flexible indexes: add Index base class and xindexes properties (pydata#5102)
  pre-commit: autoupdate hook versions (pydata#5280)
  convert the examples for apply_ufunc to doctest (pydata#5279)
  fix the new whatsnew section
  Ensure `HighLevelGraph` layers are `Layer` instances (pydata#5271)
  New whatsnew section
  Release-workflow: Bug fix (pydata#5273)
  more maintenance on whats-new.rst (pydata#5272)
  v0.18.0 release highlights (pydata#5266)
  Fix exception when display_expand_data=False for file-backed array. (pydata#5235)
  Warn ignored keep attrs (pydata#5265)
  Disable workflows on forks (pydata#5267)
  fix the built wheel test (pydata#5270)
  pypi upload workflow maintenance (pydata#5269)
  ...
dcherian added a commit to gcaria/xarray that referenced this pull request May 13, 2021
…e_units

* upstream/master:
  combine keep_attrs and combine_attrs in apply_ufunc (pydata#5041)
  Explained what a deprecation cycle is (pydata#5289)
  Code cleanup (pydata#5234)
  FacetGrid docstrings (pydata#5293)
  Add whats new for dataset interpolation with non-numerics (pydata#5297)
  Allow dataset interpolation with different datatypes (pydata#5008)
  Flexible indexes: add Index base class and xindexes properties (pydata#5102)
dcherian added a commit to ahuang11/xarray that referenced this pull request May 13, 2021
* upstream/master:
  Update release guide (pydata#5274)
  combine keep_attrs and combine_attrs in apply_ufunc (pydata#5041)
  Explained what a deprecation cycle is (pydata#5289)
  Code cleanup (pydata#5234)
  FacetGrid docstrings (pydata#5293)
  Add whats new for dataset interpolation with non-numerics (pydata#5297)
  Allow dataset interpolation with different datatypes (pydata#5008)
@max-sixty
Copy link
Collaborator

Thanks @andersy005 !

@andersy005 andersy005 deleted the backends-cleanup branch June 16, 2023 22:52
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.

8 participants