Skip to content

Add ruff rules PIE #5033

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add ruff rules PIE #5033

wants to merge 1 commit into from

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Jun 14, 2025

Window test errors caused by

Summary of changes

https://docs.astral.sh/ruff/rules/#flake8-pie-pie

Fix three instances of:
% ruff rule PIE810

The startswith and endswith methods accept tuples of prefixes or suffixes respectively.
Passing a tuple of prefixes or suffixes is more efficient and readable than calling the method multiple times.

References

These transformations to reduce unnecessary repetitive function calls have been recommended since 2006 when they were introduced in Python 2.5

Pull Request Checklist

@cclauss cclauss force-pushed the ruff-rules-PIE branch 2 times, most recently from 684fac1 to dc47791 Compare June 14, 2025 14:20
cclauss added a commit to cclauss/setuptools that referenced this pull request Jun 14, 2025
Just a test to understand why tests are failing on:
* pypa#5033
Copy link
Contributor

@Avasam Avasam left a comment

Choose a reason for hiding this comment

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

PIE is indeed a pretty good rule group to enable unconditionally.

@abravalheri
Copy link
Contributor

abravalheri commented Jun 17, 2025

There is probably some recent update in Ruff causing the CI to fail in an unchanged portion of the code:

>           raise RuffError(stdout.decode())
E           pytest_ruff.RuffError: setuptools/tests/test_build_py.py:168:5: PT031 `pytest.warns()` block should contain a single simple statement
E               |
E           167 |       msg = r"Python recognizes 'mypkg\.tests' as an importable package"
E           168 | /     with pytest.warns(SetuptoolsDeprecationWarning, match=msg):
E           169 | |         # TODO: To fix #3260 we need some transition period to deprecate the
E           170 | |         # existing behavior of `include_package_data`. After the transition, we
E           171 | |         # should remove the warning and fix the behavior.
E           172 | |
E           173 | |         if os.getenv("SETUPTOOLS_USE_DISTUTILS") == "stdlib":
E           174 | |             # pytest.warns reset the warning filter temporarily
E           175 | |             # https://github.com/pytest-dev/pytest/issues/4011#issuecomment-423494810
E           176 | |             warnings.filterwarnings(
E           177 | |                 "ignore",
E           178 | |                 "'encoding' argument not specified",
E           179 | |                 module="distutils.text_file",
E           180 | |                 # This warning is already fixed in pypa/distutils but not in stdlib
E           181 | |             )
E           182 | |
E           183 | |         build_py.finalize_options()
E           184 | |         build_py.run()
E               | |______________________^ PT031
E           185 |
E           186 |       build_dir = Path(dist.get_command_obj("build_py").build_lib)

@abravalheri
Copy link
Contributor

We can probably (temporarily) pin Ruff < 0.12.0 to avoid problems with new rules being enforced.

@cclauss
Copy link
Contributor Author

cclauss commented Jun 17, 2025

Could build_py.finalize_options() go before the with block?

Or # noqa: PT031?

@abravalheri
Copy link
Contributor

I don't know in this case because of the warnings filter bespoke modification inside the context, may be fragile...

Let's see if the CI fails in #5042.

@Avasam
Copy link
Contributor

Avasam commented Jun 17, 2025

You can pin ruff temporarily as to not expand this PR's scope if you want.
I'm going through my active projects to update to 0.12 so I'll likely to it for setuptools+distutils as well today if no one else does it before me.

@abravalheri
Copy link
Contributor

abravalheri commented Jun 17, 2025

@Avasam I created something in #5042 just as a stopgap. We probably can merge and rebase this PR.

@cclauss feel free to rebase/merge on upstream/main.

@cclauss
Copy link
Contributor Author

cclauss commented Jun 17, 2025

#5042 (comment)

@cclauss cclauss requested a review from Avasam June 17, 2025 17:14
@cclauss
Copy link
Contributor Author

cclauss commented Jun 17, 2025

@abravalheri Your review, please.

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.

3 participants