Skip to content

Fix flake8 bugbear - #14692

Merged
mhvk merged 7 commits into
astropy:mainfrom
nstarman:fix-flake8-bugbear
May 3, 2023
Merged

mhvk merged 7 commits into
astropy:mainfrom
nstarman:fix-flake8-bugbear

Conversation

@nstarman

Copy link
Copy Markdown
Member

Ruff flake8 bugbear fixes

Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
@dhomeier

Copy link
Copy Markdown
Contributor

Could you point us to some background info which toolset made these changes and why, like is this a change to our current ruff setup?

@nstarman

nstarman commented Apr 26, 2023 •

Copy link
Copy Markdown
Member Author

Could you point us to some background info which toolset made these changes and why, like is this a change to our current ruff setup?

Yes, this is removing some ignores from our ruff config. This is for the flake8-bugbear rule set.

  • B004, Using hasattr(x, '__call__') to test if x is callable is unreliable. Use callable(x) for consistent results.
  • B028, Checks for warnings.warn calls without an explicit stacklevel keyword argument. "The warnings.warn method uses a stacklevel of 1 by default, which limits the rendered stack trace to that of the line on which the warn method is called. It's recommended to use a stacklevel of 2 or higher, give the caller more context about the warning." https://beta.ruff.rs/docs/rules/no-explicit-stacklevel/
  • B024, an abstract base class has no abstract methods (and shouldn't be abstract).
  • B018, found a useless expression. Either assign it to a variable or remove it.
  • B020, a loop control variable overrides the iterable it iterates. This is dangerous and hard to debug.
  • B026, found func(x=..., *args) which is very dangerous and only works if x is actually the first argument.
  • B005, "Using .strip() with multi-character strings is misleading the reader. It looks like stripping a substring. Move your character set to a constant if this is deliberate. Use .replace(), .removeprefix(), .removesuffix() or regular expressions to remove string fragments." https://pypi.org/project/flake8-bugbear/

@nstarman
nstarman force-pushed the fix-flake8-bugbear branch from cd18342 to 0ccce0f Compare April 26, 2023 01:59
@dhomeier

Copy link
Copy Markdown
Contributor

Yes, this is removing some ignores from our ruff config. This is for the flake8-bugbear rule set.

Thanks; it's becoming a bit difficult to follow up on which tools we have decided to use, and with which settings. Hopefully this can be discussed at the meeting next week.

I agree that changes affecting functionality like B028 (some of the others above may fall into that category as well, not sure) should be committed separately. It seems, as @saimn's example shows, which stacklevel is most helpful depends quite on the scope of the immediate caller function (and possibly those in the levels above), and would need closer case-by-case inspection (especially if B028 means setting blanket stacklevel=2 everywhere).

Comment thread astropy/modeling/core.py Resolved
@nstarman

Copy link
Copy Markdown
Member Author

It seems, as @saimn's example shows, which stacklevel is most helpful depends quite on the scope of the immediate caller function (and possibly those in the levels above), and would need closer case-by-case inspection (especially if B028 means setting blanket stacklevel=2 everywhere).

I agree. I've removed that commit to tackle in a future PR.

Comment thread astropy/io/ascii/latex.py Resolved
Comment thread astropy/io/fits/util.py Resolved
Comment thread astropy/io/registry/base.py Resolved
Comment thread pyproject.toml Resolved
Comment thread astropy/modeling/core.py Outdated
Comment on lines +3279 to +3280
_ = self.left.inverse
_ = self.right.inverse

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.

I do not like this, please use the # noqa instead

@dhomeier dhomeier Apr 26, 2023 •

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.

Are there any instances where B018 comes over as helpful? If not, maybe that rule should also be moved to a possible later PR, in particular as looking e.g. at astral-sh/ruff#3831 its implementation still seems to be evolving.

Comment thread astropy/modeling/functional_models.py Resolved
Comment thread astropy/modeling/functional_models.py Resolved
Comment thread astropy/modeling/functional_models.py Resolved
Comment thread astropy/modeling/functional_models.py Resolved
Comment thread astropy/modeling/spline.py Resolved
Comment thread astropy/modeling/spline.py Resolved
Signed-off-by: Nathaniel Starkman (@nstarman) <[email protected]>
@nstarman
nstarman force-pushed the fix-flake8-bugbear branch from 0ccce0f to 931adf3 Compare April 26, 2023 17:13
@nstarman
nstarman force-pushed the fix-flake8-bugbear branch from 931adf3 to 9085fea Compare April 26, 2023 17:23
@nstarman
nstarman force-pushed the fix-flake8-bugbear branch from 9085fea to 75f3417 Compare April 26, 2023 17:26
@mhvk

mhvk commented Apr 27, 2023

Copy link
Copy Markdown
Contributor

I also wondered about just ignoring B018 altogether, but you probably got the right balance here, by ignoring it in test files. Other changes all look good.

@nstarman
nstarman requested review from mhvk and pllim May 3, 2023 13:00

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

Looks great! Indeed, for timeseries/binned.py, this corrects an actual buglet.

@WilliamJamieson - could you check that your comments are addressed, so this can go in?

@nstarman

nstarman commented May 3, 2023 •

Copy link
Copy Markdown
Member Author

Thanks @eerovaher & @mhvk for the reviews!
Giving that #14744 is coming, I'd love to get this in. @WilliamJamieson @pllim @mhvk for getting an approval critical mass.

@nstarman nstarman mentioned this pull request May 3, 2023
@mhvk
mhvk merged commit 5f8921a into astropy:main May 3, 2023
@nstarman
nstarman deleted the fix-flake8-bugbear branch May 3, 2023 17:28
@dhomeier

dhomeier commented May 3, 2023

Copy link
Copy Markdown
Contributor

Looks great! Indeed, for timeseries/binned.py, this corrects an actual buglet.

Was that once valid in some Python version?

@pllim

pllim commented May 4, 2023

Copy link
Copy Markdown
Member

Re: #14692 (comment)

I think with this change, format can no longer be passed in as positional argument. 🤷

@mhvk

mhvk commented May 4, 2023

Copy link
Copy Markdown
Contributor

It is OK - format is just being passed on, and the change ensures that other positional arguments can be passed on as well (though obviously they are rarely needed, since nobody found the error this would have produced...)

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

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

9 participants