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

Enforce ruff/refurb rules (FURB) #4386

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
4 changes: 3 additions & 1 deletion pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ class _ZipLoaderModule(Protocol):
__loader__: zipimport.zipimporter


_PEP440_FALLBACK = re.compile(r"^v?(?P<safe>(?:[0-9]+!)?[0-9]+(?:\.[0-9]+)*)", re.I)
_PEP440_FALLBACK = re.compile(
r"^v?(?P<safe>(?:[0-9]+!)?[0-9]+(?:\.[0-9]+)*)", re.IGNORECASE
)


class PEP440Warning(RuntimeWarning):
Expand Down
1 change: 1 addition & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extend-select = [
"F", # Pyflakes
"F404", # late-future-import
"FA", # flake8-future-annotations
"FURB", # refurb
"I", # isort
"PERF", # Perflint
"PGH", # pygrep-hooks (blanket-* rules)
Expand Down
10 changes: 6 additions & 4 deletions setuptools/_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import packaging

# https://packaging.python.org/en/latest/specifications/core-metadata/#name
_VALID_NAME = re.compile(r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$", re.I)
_UNSAFE_NAME_CHARS = re.compile(r"[^A-Z0-9._-]+", re.I)
_NON_ALPHANUMERIC = re.compile(r"[^A-Z0-9]+", re.I)
_PEP440_FALLBACK = re.compile(r"^v?(?P<safe>(?:[0-9]+!)?[0-9]+(?:\.[0-9]+)*)", re.I)
_VALID_NAME = re.compile(r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$", re.IGNORECASE)
_UNSAFE_NAME_CHARS = re.compile(r"[^A-Z0-9._-]+", re.IGNORECASE)
_NON_ALPHANUMERIC = re.compile(r"[^A-Z0-9]+", re.IGNORECASE)
_PEP440_FALLBACK = re.compile(
r"^v?(?P<safe>(?:[0-9]+!)?[0-9]+(?:\.[0-9]+)*)", re.IGNORECASE
)


def safe_identifier(name: str) -> str:
Expand Down
3 changes: 1 addition & 2 deletions setuptools/command/bdist_egg.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def _get_purelib():
def strip_module(filename):
if '.' in filename:
filename = os.path.splitext(filename)[0]
if filename.endswith('module'):
filename = filename[:-6]
filename = filename.removesuffix('module')
return filename


Expand Down
3 changes: 2 additions & 1 deletion setuptools/command/build_ext.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import itertools
import operator
import os
import sys
from collections.abc import Iterator
Expand Down Expand Up @@ -326,7 +327,7 @@ def get_outputs(self) -> list[str]:
def get_output_mapping(self) -> dict[str, str]:
"""See :class:`setuptools.commands.build.SubCommand`"""
mapping = self._get_output_mapping()
return dict(sorted(mapping, key=lambda x: x[0]))
return dict(sorted(mapping, key=operator.itemgetter(0)))

def __get_stubs_outputs(self):
# assemble the base name for each extension that needs a stub
Expand Down
3 changes: 2 additions & 1 deletion setuptools/command/build_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fnmatch
import itertools
import operator
import os
import stat
import textwrap
Expand Down Expand Up @@ -147,7 +148,7 @@ def get_output_mapping(self) -> dict[str, str]:
self._get_package_data_output_mapping(),
self._get_module_mapping(),
)
return dict(sorted(mapping, key=lambda x: x[0]))
return dict(sorted(mapping, key=operator.itemgetter(0)))

def _get_module_mapping(self) -> Iterator[tuple[str, str]]:
"""Iterate over all modules producing (dest, src) pairs."""
Expand Down
5 changes: 3 additions & 2 deletions setuptools/command/editable_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import io
import logging
import operator
import os
import shutil
import traceback
Expand Down Expand Up @@ -650,7 +651,7 @@ def _parent_path(pkg, pkg_path):
>>> _parent_path("b", "src/c")
'src/c'
"""
parent = pkg_path[: -len(pkg)] if pkg_path.endswith(pkg) else pkg_path
parent = pkg_path.removesuffix(pkg)
return parent.rstrip("/" + os.sep)


Expand Down Expand Up @@ -891,7 +892,7 @@ def _finder_template(
"""Create a string containing the code for the``MetaPathFinder`` and
``PathEntryFinder``.
"""
mapping = dict(sorted(mapping.items(), key=lambda p: p[0]))
mapping = dict(sorted(mapping.items(), key=operator.itemgetter(0)))
return _FINDER_TEMPLATE.format(name=name, mapping=mapping, namespaces=namespaces)


Expand Down
3 changes: 1 addition & 2 deletions setuptools/command/egg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,7 @@ def global_exclude(self, pattern):
return self._remove_files(match.match)

def append(self, item) -> None:
if item.endswith('\r'): # Fix older sdists built on Windows
item = item[:-1]
item = item.removesuffix('\r')
DimitriPapadopoulos marked this conversation as resolved.
Show resolved Hide resolved
path = convert_path(item)

if self._safe_path(path):
Expand Down
3 changes: 1 addition & 2 deletions setuptools/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,8 +953,7 @@ def iter_distribution_names(self):
name, _buildinfo = ext
else:
name = ext.name
if name.endswith('module'):
name = name[:-6]
name = name.removesuffix('module')
yield name

def handle_display_options(self, option_order):
Expand Down
2 changes: 1 addition & 1 deletion setuptools/msvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def _find_latest_available_vs_ver(self):

vc_vers = set(reg_vc_vers)
vc_vers.update(self.known_vs_paths)
return sorted(vc_vers)[-1]
return max(vc_vers)

def find_reg_vs_vers(self):
"""
Expand Down
8 changes: 5 additions & 3 deletions setuptools/package_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@
from distutils.errors import DistutilsError

EGG_FRAGMENT = re.compile(r'^egg=([-A-Za-z0-9_.+!]+)$')
HREF = re.compile(r"""href\s*=\s*['"]?([^'"> ]+)""", re.I)
HREF = re.compile(r"""href\s*=\s*['"]?([^'"> ]+)""", re.IGNORECASE)
PYPI_MD5 = re.compile(
r'<a href="([^"#]+)">([^<]+)</a>\n\s+\(<a (?:title="MD5 hash"\n\s+)'
r'href="[^?]+\?:action=show_md5&amp;digest=([0-9a-f]{32})">md5</a>\)'
)
URL_SCHEME = re.compile('([-+.a-z0-9]{2,}):', re.I).match
URL_SCHEME = re.compile('([-+.a-z0-9]{2,}):', re.IGNORECASE).match
EXTENSIONS = ".tar.gz .tar.bz2 .tar .zip .tgz".split()

__all__ = [
Expand Down Expand Up @@ -214,7 +214,9 @@ def wrapper(*args, **kwargs):
return wrapper


REL = re.compile(r"""<([^>]*\srel\s{0,10}=\s{0,10}['"]?([^'" >]+)[^>]*)>""", re.I)
REL = re.compile(
r"""<([^>]*\srel\s{0,10}=\s{0,10}['"]?([^'" >]+)[^>]*)>""", re.IGNORECASE
)
"""
Regex for an HTML tag with 'rel="val"' attributes.
"""
Expand Down
2 changes: 1 addition & 1 deletion setuptools/tests/config/test_apply_pyprojecttoml.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def test_not_listed_in_dynamic(self, tmp_path, attr, field, value):
"""Setuptools cannot set a field if not listed in ``dynamic``"""
pyproject = self.pyproject(tmp_path, [])
dist = makedist(tmp_path, **{attr: value})
msg = re.compile(f"defined outside of `pyproject.toml`:.*{field}", re.S)
msg = re.compile(f"defined outside of `pyproject.toml`:.*{field}", re.DOTALL)
with pytest.warns(_MissingDynamic, match=msg):
dist = pyprojecttoml.apply_configuration(dist, pyproject)

Expand Down
6 changes: 4 additions & 2 deletions setuptools/tests/config/test_pyprojecttoml.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def test_scripts_not_listed_in_dynamic(self, tmp_path, missing_dynamic):
dynamic = {"scripts", "gui-scripts", "entry-points"} - {missing_dynamic}

msg = f"defined outside of `pyproject.toml`:.*{missing_dynamic}"
with pytest.raises(OptionError, match=re.compile(msg, re.S)):
with pytest.raises(OptionError, match=re.compile(msg, re.DOTALL)):
expand_configuration(self.pyproject(dynamic), tmp_path)


Expand Down Expand Up @@ -325,7 +325,9 @@ def test_invalid_example(tmp_path, example, error_msg):
pyproject = tmp_path / "pyproject.toml"
pyproject.write_text(cleandoc(example), encoding="utf-8")

pattern = re.compile(f"invalid pyproject.toml.*{error_msg}.*", re.M | re.S)
pattern = re.compile(
f"invalid pyproject.toml.*{error_msg}.*", re.MULTILINE | re.DOTALL
)
with pytest.raises(ValueError, match=pattern):
read_configuration(pyproject)

Expand Down
2 changes: 1 addition & 1 deletion setuptools/tests/test_dist_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_invalid_version(self, tmp_path):
"""
config = "[metadata]\nname=proj\nversion=42\n[egg_info]\ntag_build=invalid!!!\n"
(tmp_path / "setup.cfg").write_text(config, encoding="utf-8")
msg = re.compile("invalid version", re.M | re.I)
msg = re.compile("invalid version", re.MULTILINE | re.IGNORECASE)
proc = run_command_inner("dist_info", cwd=tmp_path, check=False)
assert proc.returncode
assert msg.search(proc.stdout)
Expand Down
10 changes: 5 additions & 5 deletions setuptools/tests/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ def build_wheel(extra_file_defs=None, **kwargs):


def tree_set(root):
contents = set()
for dirpath, dirnames, filenames in os.walk(root):
for filename in filenames:
contents.add(os.path.join(os.path.relpath(dirpath, root), filename))
return contents
return {
os.path.join(os.path.relpath(dirpath, root), filename)
for dirpath, dirnames, filenames in os.walk(root)
for filename in filenames
}


def flatten_tree(tree):
Expand Down
2 changes: 1 addition & 1 deletion tools/finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _repair_changelog():
"""
changelog_fn = pathlib.Path('NEWS.rst')
changelog = changelog_fn.read_text(encoding='utf-8')
fixed = re.sub(r'^(v[0-9.]+)v[0-9.]+$', r'\1', changelog, flags=re.M)
fixed = re.sub(r'^(v[0-9.]+)v[0-9.]+$', r'\1', changelog, flags=re.MULTILINE)
changelog_fn.write_text(fixed, encoding='utf-8')
subprocess.check_output(['git', 'add', changelog_fn])

Expand Down
Loading