Skip to content

fix Ruff rule B007 violation in astropy/coordinates - #17844

Closed
kangqiwang wants to merge 6 commits into
astropy:mainfrom
kangqiwang:ruff-kang
Closed

kangqiwang wants to merge 6 commits into
astropy:mainfrom
kangqiwang:ruff-kang

Conversation

@kangqiwang

@kangqiwang kangqiwang commented Mar 4, 2025 •

Copy link
Copy Markdown
Contributor

Description

This PR ensures compliance of astropy/coordinates to Ruff rule unused-loop-control-variable (B007)

Fixes partially #14818

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@github-actions

github-actions Bot commented Mar 4, 2025

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 instructions for rebase and squash.
  • 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?
  • 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.

@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.

All pull requests contributing to #14818 must edit .ruff.toml like the opening post of the issue describes. This is required to ensure that the pull request indeed addresses all violations of a rule in a sub-package, but also prevents new violations in future code contributions.

@kangqiwang
kangqiwang requested a review from taldcroft as a code owner March 5, 2025 11:19

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Another one of those ruff rules that are matters of taste -- certainly don't see much advantage of this, but it also doesn't matter much, I guess, although it adds yet another hard-to-understand hoop for new contributors.

Anyway, please revert the change commented on in-line (only replace unused names by underscores, don't make other changes).

Comment thread astropy/time/formats.py Outdated
for subfmt, dtype, convert, _ in subfmts:
convert_result = None
for _, dtype, convert, _ in subfmts:
convert_result = convert

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This change makes no sense - there is no need to introduce convert_result.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

when i change the code, I was confusing as well, but you can find similar issues has been discuss in B007 guidance when variable used outside the loop only

they are talking different way
1.

   convert_result = None
   for _, dtype, convert, _ in subfmts:
      convert_result = convert
      pass
   return convert_result 
   for _, dtype, convert, _convert in subfmts:
      pass
   return _convert 

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ours is like #2, but we also have an else: clause that ensures we can never have no result (if, e.g., subfmts is empty). So, really the code as is is fine, and no change beyond replacing the unused variables by _ should be needed. Any other warning is a bug if ruff.

@kangqiwang

Copy link
Copy Markdown
Contributor Author

All pull requests contributing to #14818 must edit .ruff.toml like the opening post of the issue describes. This is required to ensure that the pull request indeed addresses all violations of a rule in a sub-package, but also prevents new violations in future code contributions.

you can find update .ruff.toml file on latest commit

Comment thread astropy/time/formats.py
convert_result = None
for _, dtype, convert, _ in subfmts:
convert_result = convert
for _, dtype, _convert, _ in subfmts:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is nicer, but why is there a need for prefixing with underscore (replacing convert with _convert)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it will raises when the loop variable is not used inside the loop. but it allows the variable to be marked as private, then not used in the loop, but still used outside the loop.

I personally feel B007 is trying to do too much

@mhvk mhvk Mar 5, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm, I see. That sort-of makes sense (though really only sort of... really the code could look ahead and see if the variable is used later).

A work-around that keeps the code intact would then just move the if statement inline,

    for _, dtype, convert, _ in subfmts:
        if np.issubdtype(val_dtype, dtype):
            if convert is not None:
                try:
                    val1, val2 = convert(val1, val2)
                except Exception:
                    raise TypeError(
                        f"for {self.name} class, input should be (long) doubles, string, "
                        "or Decimal, and second values are only allowed for "
                        "(long) doubles."
                    )
            break
    else:
        raise ValueError("input type not among selected sub-formats.")

Not very elegant but maybe better?

EDIT: updated above, since initially I forgot the if np.issubdtype clause.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Might even make the control flow clearer, but otherwise I don't see much sense in restructuring this much code just because the linter can't figure out what is happening 4 lines down the code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@dhomeier - yes, I agree, and in a way the control flow is clearer as it is: first the loop matches the type and collects any associated convert function, and then the function is used.

@eerovaher

eerovaher commented Mar 5, 2025 •

Copy link
Copy Markdown
Member

Because this pull request is updating many sub-packages, it will require reviews from many maintainers. Having had a brief look at the code changes I can see that the reviews will not be trivial. If you want to address B007 in astropy then do it one sub-package at a time, or else it is very likely that this pull request will get stuck in review and will never be merged.

@github-actions

github-actions Bot commented Aug 3, 2025

Copy link
Copy Markdown
Contributor

Hi humans 👋 - this pull request hasn't had any new commits for approximately 4 months. I plan to close this in 30 days if the pull request doesn't have any new commits by then.

In lieu of a stalled pull request, please consider closing this and open an issue instead if a reminder is needed to revisit in the future. Maintainers may also choose to add keep-open label to keep this PR open but it is discouraged unless absolutely necessary.

If this PR still needs to be reviewed, as an author, you can rebase it to reset the clock.

If you believe I commented on this pull request incorrectly, please report this here.

@github-actions github-actions Bot added the closed-by-bot Closed by stale bot label Sep 3, 2025
@github-actions

github-actions Bot commented Sep 3, 2025

Copy link
Copy Markdown
Contributor

I'm going to close this pull request as per my previous message. If you think what is being added/fixed here is still important, please remember to open an issue to keep track of it. Thanks!

If this is the first time I am commenting on this issue, or if you believe I closed this issue incorrectly, please report this here.

@github-actions github-actions Bot closed this Sep 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Close? Tell stale bot that this issue/PR is stale closed-by-bot Closed by stale bot coordinates no-changelog-entry-needed Refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants