Skip to content

Commit d3048fc

Browse files
committed
Apply more assorted Pyugrade suggestions (#4125)
2 parents e50a9f2 + 07774d9 commit d3048fc

19 files changed

+86
-81
lines changed

pkg_resources/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1895,7 +1895,7 @@ def _extract_resource(self, manager, zip_path): # noqa: C901
18951895
try:
18961896
rename(tmpnam, real_path)
18971897

1898-
except os.error:
1898+
except OSError:
18991899
if os.path.isfile(real_path):
19001900
if self._is_current(real_path, zip_path):
19011901
# the file became current since it was checked above,
@@ -1908,7 +1908,7 @@ def _extract_resource(self, manager, zip_path): # noqa: C901
19081908
return real_path
19091909
raise
19101910

1911-
except os.error:
1911+
except OSError:
19121912
# report a user-friendly error
19131913
manager.extraction_error()
19141914

@@ -2901,7 +2901,7 @@ def __getattr__(self, attr):
29012901

29022902
def __dir__(self):
29032903
return list(
2904-
set(super(Distribution, self).__dir__())
2904+
set(super().__dir__())
29052905
| set(attr for attr in self._provider.__dir__() if not attr.startswith('_'))
29062906
)
29072907

@@ -3168,7 +3168,7 @@ class RequirementParseError(packaging.requirements.InvalidRequirement):
31683168
class Requirement(packaging.requirements.Requirement):
31693169
def __init__(self, requirement_string):
31703170
"""DO NOT CALL THIS UNDOCUMENTED METHOD; use Requirement.parse()!"""
3171-
super(Requirement, self).__init__(requirement_string)
3171+
super().__init__(requirement_string)
31723172
self.unsafe_name = self.name
31733173
project_name = safe_name(self.name)
31743174
self.project_name, self.key = project_name, project_name.lower()

pkg_resources/tests/test_working_set.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ def parametrize_test_working_set_resolve(*test_list):
7676
requirements,
7777
expected1,
7878
expected2,
79-
) = [
79+
) = (
8080
strip_comments(s.lstrip())
8181
for s in textwrap.dedent(test).lstrip().split('\n\n', 5)
82-
]
82+
)
8383
installed_dists = list(parse_distributions(installed_dists))
8484
installable_dists = list(parse_distributions(installable_dists))
8585
requirements = list(pkg_resources.parse_requirements(requirements))

ruff.toml

+20
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,27 @@ ignore = [
1616
"ISC001",
1717
"ISC002",
1818
]
19+
extend-select = [
20+
"UP", # pyupgrade
21+
]
22+
extend-ignore = [
23+
"UP015", # redundant-open-modes, explicit is prefered
24+
"UP030", # temporarily disabled
25+
"UP031", # temporarily disabled
26+
"UP032", # temporarily disabled
27+
"UP036", # temporarily disabled
28+
]
29+
extend-exclude = [
30+
"**/_vendor",
31+
"setuptools/_distutils",
32+
"setuptools/config/_validate_pyproject",
33+
]
1934

2035
[format]
36+
extend-exclude = [
37+
"**/_vendor",
38+
"setuptools/_distutils",
39+
"setuptools/config/_validate_pyproject",
40+
]
2141
# https://docs.astral.sh/ruff/settings/#format-quote-style
2242
quote-style = "preserve"

setuptools/build_meta.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ def run_setup(self, setup_script='setup.py'):
477477
sys.argv[0] = setup_script
478478

479479
try:
480-
super(_BuildMetaLegacyBackend, self).run_setup(setup_script=setup_script)
480+
super().run_setup(setup_script=setup_script)
481481
finally:
482482
# While PEP 517 frontends should be calling each hook in a fresh
483483
# subprocess according to the standard (and thus it should not be

setuptools/command/bdist_egg.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,7 @@ def walk_egg(egg_dir):
321321
if 'EGG-INFO' in dirs:
322322
dirs.remove('EGG-INFO')
323323
yield base, dirs, files
324-
for bdf in walker:
325-
yield bdf
324+
yield from walker
326325

327326

328327
def analyze_egg(egg_dir, stubs):
@@ -406,14 +405,12 @@ def scan_module(egg_dir, base, name, stubs):
406405

407406
def iter_symbols(code):
408407
"""Yield names and strings used by `code` and its nested code objects"""
409-
for name in code.co_names:
410-
yield name
408+
yield from code.co_names
411409
for const in code.co_consts:
412410
if isinstance(const, str):
413411
yield const
414412
elif isinstance(const, CodeType):
415-
for name in iter_symbols(const):
416-
yield name
413+
yield from iter_symbols(const)
417414

418415

419416
def can_scan():

setuptools/command/easy_install.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1743,8 +1743,7 @@ class RewritePthDistributions(PthDistributions):
17431743
@classmethod
17441744
def _wrap_lines(cls, lines):
17451745
yield cls.prelude
1746-
for line in lines:
1747-
yield line
1746+
yield from lines
17481747
yield cls.postlude
17491748

17501749
prelude = _one_liner(
@@ -2024,7 +2023,7 @@ def chmod(path, mode):
20242023
log.debug("changing mode of %s to %o", path, mode)
20252024
try:
20262025
_chmod(path, mode)
2027-
except os.error as e:
2026+
except OSError as e:
20282027
log.debug("chmod failed: %s", e)
20292028

20302029

@@ -2180,8 +2179,7 @@ def get_args(cls, dist, header=None):
21802179
cls._ensure_safe_name(name)
21812180
script_text = cls.template % locals()
21822181
args = cls._get_script_args(type_, name, header, script_text)
2183-
for res in args:
2184-
yield res
2182+
yield from args
21852183

21862184
@staticmethod
21872185
def _ensure_safe_name(name):

setuptools/command/editable_wheel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def __init__(self, dist: Distribution, name: str, path_entries: List[Path]):
400400
self.path_entries = path_entries
401401

402402
def __call__(self, wheel: "WheelFile", files: List[str], mapping: Dict[str, str]):
403-
entries = "\n".join((str(p.resolve()) for p in self.path_entries))
403+
entries = "\n".join(str(p.resolve()) for p in self.path_entries)
404404
contents = _encode_pth(f"{entries}\n")
405405
wheel.writestr(f"__editable__.{self.name}.pth", contents)
406406

setuptools/command/sdist.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
def walk_revctrl(dirname=''):
1515
"""Find all files under revision control"""
1616
for ep in metadata.entry_points(group='setuptools.file_finders'):
17-
for item in ep.load()(dirname):
18-
yield item
17+
yield from ep.load()(dirname)
1918

2019

2120
class sdist(orig.sdist):
@@ -190,7 +189,7 @@ def _manifest_is_not_generated(self):
190189

191190
with open(self.manifest, 'rb') as fp:
192191
first_line = fp.readline()
193-
return first_line != '# file GENERATED by distutils, do NOT edit\n'.encode()
192+
return first_line != b'# file GENERATED by distutils, do NOT edit\n'
194193

195194
def read_manifest(self):
196195
"""Read the manifest file (named by 'self.manifest') and use it to

setuptools/dist.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -912,11 +912,9 @@ def get_cmdline_options(self):
912912
def iter_distribution_names(self):
913913
"""Yield all packages, modules, and extension names in distribution"""
914914

915-
for pkg in self.packages or ():
916-
yield pkg
915+
yield from self.packages or ()
917916

918-
for module in self.py_modules or ():
919-
yield module
917+
yield from self.py_modules or ()
920918

921919
for ext in self.ext_modules or ():
922920
if isinstance(ext, tuple):

setuptools/glob.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ def glob0(dirname, basename):
113113
def glob2(dirname, pattern):
114114
assert _isrecursive(pattern)
115115
yield pattern[:0]
116-
for x in _rlistdir(dirname):
117-
yield x
116+
yield from _rlistdir(dirname)
118117

119118

120119
# Recursively yields relative pathnames inside a literal directory.
@@ -126,7 +125,7 @@ def _rlistdir(dirname):
126125
dirname = os.curdir
127126
try:
128127
names = os.listdir(dirname)
129-
except os.error:
128+
except OSError:
130129
return
131130
for x in names:
132131
yield x

setuptools/msvc.py

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"""
1313

1414
import json
15-
from io import open
1615
from os import listdir, pathsep
1716
from os.path import join, isfile, isdir, dirname
1817
from subprocess import CalledProcessError

setuptools/package_index.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,13 @@ def egg_info_for_url(url):
112112
def distros_for_url(url, metadata=None):
113113
"""Yield egg or source distribution objects that might be found at a URL"""
114114
base, fragment = egg_info_for_url(url)
115-
for dist in distros_for_location(url, base, metadata):
116-
yield dist
115+
yield from distros_for_location(url, base, metadata)
117116
if fragment:
118117
match = EGG_FRAGMENT.match(fragment)
119118
if match:
120-
for dist in interpret_distro_name(
119+
yield from interpret_distro_name(
121120
url, match.group(1), metadata, precedence=CHECKOUT_DIST
122-
):
123-
yield dist
121+
)
124122

125123

126124
def distros_for_location(location, basename, metadata=None):
@@ -516,7 +514,7 @@ def obtain(self, requirement, installer=None):
516514
if dist in requirement:
517515
return dist
518516
self.debug("%s does not match %s", requirement, dist)
519-
return super(PackageIndex, self).obtain(requirement, installer)
517+
return super().obtain(requirement, installer)
520518

521519
def check_hash(self, checker, filename, tfp):
522520
"""

setuptools/tests/integration/helpers.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
def run(cmd, env=None):
1616
r = subprocess.run(
1717
cmd,
18-
stdout=subprocess.PIPE,
19-
stderr=subprocess.PIPE,
18+
capture_output=True,
2019
universal_newlines=True,
2120
env={**os.environ, **(env or {})},
2221
# ^-- allow overwriting instead of discarding the current env

setuptools/tests/test_archive_util.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# coding: utf-8
2-
31
import tarfile
42
import io
53

setuptools/tests/test_build_meta.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class BuildBackend(BuildBackendBase):
4040
"""PEP 517 Build Backend"""
4141

4242
def __init__(self, *args, **kwargs):
43-
super(BuildBackend, self).__init__(*args, **kwargs)
43+
super().__init__(*args, **kwargs)
4444
self.pool = futures.ProcessPoolExecutor(max_workers=1)
4545

4646
def __getattr__(self, name):
@@ -73,7 +73,7 @@ def _kill(self, pid):
7373

7474
class BuildBackendCaller(BuildBackendBase):
7575
def __init__(self, *args, **kwargs):
76-
super(BuildBackendCaller, self).__init__(*args, **kwargs)
76+
super().__init__(*args, **kwargs)
7777

7878
(self.backend_name, _, self.backend_obj) = self.backend_name.partition(':')
7979

setuptools/tests/test_dist.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_provides_extras_deterministic_order():
116116
# Invalid value type.
117117
(
118118
{
119-
'hello': str('*.msg'),
119+
'hello': '*.msg',
120120
},
121121
(
122122
"\"values of 'package_data' dict\" "
@@ -142,7 +142,7 @@ def test_check_package_data(package_data, expected_message):
142142
assert check_package_data(None, 'package_data', package_data) is None
143143
else:
144144
with pytest.raises(DistutilsSetupError, match=re.escape(expected_message)):
145-
check_package_data(None, str('package_data'), package_data)
145+
check_package_data(None, 'package_data', package_data)
146146

147147

148148
def test_check_specifier():

0 commit comments

Comments
 (0)