Skip to content

Address Ruff Rule C408 in astropy/io Sub-package - #14841

Merged
nstarman merged 1 commit into
astropy:mainfrom
Mubin17:Mubin17/14840Issue
May 28, 2023
Merged

nstarman merged 1 commit into
astropy:mainfrom
Mubin17:Mubin17/14840Issue

Conversation

@Mubin17

@Mubin17 Mubin17 commented May 17, 2023 •

Copy link
Copy Markdown
Contributor

Description

Refactored the code in the astropy/io sub-package to address the Ruff rule C408 violation. Replaced unnecessary dict calls with corresponding literals.
This pull request is to address Issue #14840

Fixes #14840

@github-actions

github-actions Bot commented May 17, 2023 •

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see "When to rebase and squash commits".
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • Is a milestone set? Milestone must be set but we cannot check for it on Actions; do not let the green checkmark fool you.
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@Mubin17 Mubin17 changed the title #14840 Issue: Address Ruff Rule C408 in astropy/io Sub-package Address Ruff Rule C408 in astropy/io Sub-package May 17, 2023
@nstarman nstarman added this to the v6.0 milestone May 18, 2023
@nstarman

Copy link
Copy Markdown
Member

Thanks @Mubin17! This all looks very good. And it's wise to tackle these corrections on a sub package basis.
Side note, we recommend installing pre-commit on your computer (see https://pre-commit.com/#usage), which should automatically apply black and ensure that the pre-commit CI always passes.
Also, now that C408 is enforced, let's keep it enforced!
In .ruff.toml, if you could remove C408 from the ignore list (

"C408", # unnecessary-collection-call
)
and copy it to the per-file-ignore list (at

astropy/.ruff.toml

Lines 287 to 309 in 12b01d7

# TODO: fix these, on a per-subpackage basis.
# When a general exclusion is being fixed, but it affects many subpackages, it
# is better to fix for subpackages individually. The general exclusion should be
# copied to these subpackage sections and fixed there.
"astropy/config" = []
"astropy/constants" = []
"astropy/convolution" = []
"astropy/coordinates" = []
"astropy/cosmology" = []
"astropy/io" = []
"astropy/modeling" = []
"astropy/nddata" = []
"astropy/samp" = []
"astropy/stats" = []
"astropy/table" = []
"astropy/tests" = []
"astropy/time" = []
"astropy/timeseries" = []
"astropy/uncertainty" = []
"astropy/units" = []
"astropy/utils" = []
"astropy/visualization" = []
"astropy/wcs" = []
), except for astropy/io of course, that would be great! Also, fixing C408 in the next sub package is as easy as removing it from the relevant per-file-ignore list!

@nstarman

Copy link
Copy Markdown
Member

@Mubin17, I found a configuration error in the .ruff.toml and am pushing a fix as part of #14843. Maybe hold off on editing .ruff.toml until that PR is in.

@taldcroft taldcroft left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks fine from the io.ascii perspective.

@matteobachetti

Copy link
Copy Markdown
Contributor

Modifications to io.misc are trivial, no issues there

@pllim pllim mentioned this pull request May 18, 2023
3 of 6 tasks
pllim
pllim previously requested changes May 19, 2023

@pllim pllim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please do not introduce merge commit in your PR commits. You need to rebase instead. Thanks!

@Mubin17

Mubin17 commented May 19, 2023

Copy link
Copy Markdown
Contributor Author

@pllim Sorry about that but I am lost kind of lost on what am I supposed to do next. I don’t have much experience with rebase, something goes wrong whenever I try it, so it would be great if you perform the rebase you can or guide me on how can I rebase. Thanks

@pllim

pllim commented May 19, 2023

Copy link
Copy Markdown
Member

@Mubin17 , please try the following:

  1. First make a backup branch, in case things go wrong.
  2. git fetch upstream main
  3. Then on this branch, do git rebase upstream/main
  4. If there is any conflict, it will guide you through conflict resolution, so read the prompts.
  5. If no conflicts or after they are resolved, do a git log. You should see only your commits without merge commit in between.
  6. If you are still unsure, do a git diff HEAD~2 (since right now you have 2 real commits) and you should see only your changes.

When you are confident it is correctly done, do git push origin Mubin17/14840Issue -f

Good luck!

@Mubin17
Mubin17 force-pushed the Mubin17/14840Issue branch from 1fb9e71 to f4effc3 Compare May 19, 2023 02:21
@Mubin17

Mubin17 commented May 19, 2023

Copy link
Copy Markdown
Contributor Author

@pllim Thanks for the help!

@eerovaher

Copy link
Copy Markdown
Member

I notice this pull request has two commits: the first fixes the rule violations in astropy.io, the second edits .ruff.toml. If pre-commit is set up then it would be simpler to first edit .ruff.toml and then to make the commit without manually editing any other files. If Ruff can autofix the rule (it can autofix C408) then the Ruff pre-commit hook automatically fixes the rule violations and the Black hook automatically fixes any code style issues (if any are needed).

@pllim
pllim dismissed their stale review May 19, 2023 19:08

Merge commit removed.

@Mubin17

Mubin17 commented May 23, 2023 •

Copy link
Copy Markdown
Contributor Author

I notice this pull request has two commits: the first fixes the rule violations in astropy.io, the second edits .ruff.toml. If pre-commit is set up then it would be simpler to first edit .ruff.toml and then to make the commit without manually editing any other files. If Ruff can autofix the rule (it can autofix C408) then the Ruff pre-commit hook automatically fixes the rule violations and the Black hook automatically fixes any code style issues (if any are needed).

Thanks @eerovaher for the suggestion! I feel like editing the .ruff.toml file first and then committing the changes without manual file editing sounds like a much smoother workflow. I'll definitely keep this in mind when I tackle Ruff rule-C408 in other Astropy sub-packages.

Just a quick question: Do you think the current pull request is ready to be merged, or if there are any additional changes or considerations you would like me to address? Thanks again!

@eerovaher

Copy link
Copy Markdown
Member

I suggest squashing the commits.

I have an additional recommendation about commit messages. GitHub knows which commits belong to which pull requests regardless of the commit messages, and linking pull requests to issues does not depend on the commit messages either. Including GitHub issues in commit messages therefore provides little benefit to GitHub users. Furthermore, issues and pull requests are a GitHub feature, not a Git feature, so including issue numbers in commit messages usually doesn't help anyone using Git either, although there are some exceptions to this. Replacing dict() calls with dictionary comprehensions does not depend on GitHub at all, so this is not one of the exceptions where adding an issue number to the commit message would be helpful.

@Mubin17
Mubin17 force-pushed the Mubin17/14840Issue branch from f4effc3 to 081c892 Compare May 24, 2023 01:47
@Mubin17

Mubin17 commented May 24, 2023

Copy link
Copy Markdown
Contributor Author

@eerovaher I've squashed all the commits into one and have updated the commit messages to an appropriate one.

@nstarman nstarman left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @Mubin17. This all looks very straightforward and it's nice there's an autofix available.

As reference for anyone looking at this: Rule C408 has 2 benefits:

  1. it's faster because there's no global namespace lookup.
  2. it's safer because callables like dict can be rebound, but language-level syntax like {} cannot, so {} unambiguously means a dict has been created.

@nstarman
nstarman requested review from eerovaher and pllim May 24, 2023 14:49

@eerovaher eerovaher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Currently C408 is being removed from the global ignore list and is added to the ignore lists of individual sub-packages, except for astropy/io in which the violations are fixed. However, there are subpackages in astropy that already comply with C408. I have highlighted astropy/config, but there are more. C408 should only be added to the ignore lists of the sub-packages where it is needed.

Comment thread .ruff.toml Outdated
"astropy/coordinates/*" = ["I001"]
"astropy/cosmology/*" = []
"astropy/__init__.py" = ["C408"]
"astropy/config/*" = ["C408"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

astropy/config already complies with C408, so Ruff shouldn't be told to ignore this rule there.

@Mubin17
Mubin17 force-pushed the Mubin17/14840Issue branch from 8c284af to 7c83005 Compare May 25, 2023 02:37
@Mubin17
Mubin17 requested a review from eerovaher May 25, 2023 03:24
@Mubin17

Mubin17 commented May 25, 2023 •

Copy link
Copy Markdown
Contributor Author

@eerovaher I have made the requested changes, can you please review them? Let me know if anything else needs changes. Thanks

@Mubin17

Mubin17 commented May 28, 2023

Copy link
Copy Markdown
Contributor Author

@nstarman Does this PR need any additional changes? If not can it be merged?

@nstarman

Copy link
Copy Markdown
Member

I think we’re good to go. IMO this is a clear and positive change. Thanks @Mubin17 for the PR!

@nstarman
nstarman merged commit 09f9a26 into astropy:main May 28, 2023
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.

Address Ruff Rule C408 in astropy/io Sub-package

6 participants