Skip to content

Commit

Permalink
Fix a couple refurb warnings (#4172)
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Jan 5, 2024
2 parents d3048fc + 2975a39 commit 25b57b4
Show file tree
Hide file tree
Showing 19 changed files with 51 additions and 58 deletions.
35 changes: 15 additions & 20 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,20 +407,18 @@ def get_provider(moduleOrReq):
return _find_adapter(_provider_factories, loader)(module)


def _macos_vers(_cache=[]):
if not _cache:
version = platform.mac_ver()[0]
# fallback for MacPorts
if version == '':
plist = '/System/Library/CoreServices/SystemVersion.plist'
if os.path.exists(plist):
with open(plist, 'rb') as fh:
plist_content = plistlib.load(fh)
if 'ProductVersion' in plist_content:
version = plist_content['ProductVersion']

_cache.append(version.split('.'))
return _cache[0]
@functools.lru_cache(maxsize=None)
def _macos_vers():
version = platform.mac_ver()[0]
# fallback for MacPorts
if version == '':
plist = '/System/Library/CoreServices/SystemVersion.plist'
if os.path.exists(plist):
with open(plist, 'rb') as fh:
plist_content = plistlib.load(fh)
if 'ProductVersion' in plist_content:
version = plist_content['ProductVersion']
return version.split('.')


def _macos_arch(machine):
Expand Down Expand Up @@ -2422,12 +2420,9 @@ def _cygwin_patch(filename): # pragma: nocover
return os.path.abspath(filename) if sys.platform == 'cygwin' else filename


def _normalize_cached(filename, _cache={}):
try:
return _cache[filename]
except KeyError:
_cache[filename] = result = normalize_path(filename)
return result
@functools.lru_cache(maxsize=None)
def _normalize_cached(filename):
return normalize_path(filename)


def _is_egg_path(path):
Expand Down
4 changes: 3 additions & 1 deletion pkg_resources/tests/test_pkg_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ def fake_open(file, *args, **kwargs):

# Ensure that the _macos_vers works correctly
with mock.patch('builtins.open', mock.Mock(side_effect=fake_open)) as m:
assert pkg_resources._macos_vers([]) == ["11", "4"]
pkg_resources._macos_vers.cache_clear()
assert pkg_resources._macos_vers() == ["11", "4"]
pkg_resources._macos_vers.cache_clear()

m.assert_called()

Expand Down
2 changes: 1 addition & 1 deletion setuptools/build_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _file_with_extension(directory, extension):
raise ValueError(
'No distribution was found. Ensure that `setup.py` '
'is not empty and that it calls `setup()`.'
)
) from None
return file


Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def get_ext_filename(self, fullname):

if fullname in self.ext_map:
ext = self.ext_map[fullname]
use_abi3 = getattr(ext, 'py_limited_api') and get_abi3_suffix()
use_abi3 = ext.py_limited_api and get_abi3_suffix()
if use_abi3:
filename = filename[: -len(so_ext)]
so_ext = get_abi3_suffix()
Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/build_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def exclude_data_files(self, package, src_dir, files):
return list(unique_everseen(keepers))

@staticmethod
def _get_platform_patterns(spec, package, src_dir, extra_patterns=[]):
def _get_platform_patterns(spec, package, src_dir, extra_patterns=()):
"""
yield platform-specific path patterns (suitable for glob
or fn_match) from a glob-based spec (such as
Expand Down
4 changes: 2 additions & 2 deletions setuptools/command/editable_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from contextlib import suppress
from enum import Enum
from inspect import cleandoc
from itertools import chain
from itertools import chain, starmap
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import (
Expand Down Expand Up @@ -606,7 +606,7 @@ def _simple_layout(
layout = {pkg: find_package_path(pkg, package_dir, project_dir) for pkg in packages}
if not layout:
return set(package_dir) in ({}, {""})
parent = os.path.commonpath([_parent_path(k, v) for k, v in layout.items()])
parent = os.path.commonpath(starmap(_parent_path, layout.items()))
return all(
_path.same_path(Path(parent, *key.split('.')), value)
for key, value in layout.items()
Expand Down
5 changes: 2 additions & 3 deletions setuptools/command/egg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,8 @@ def process_template_line(self, line):
try:
process_action = action_map[action]
except KeyError:
raise DistutilsInternalError(
"this cannot happen: invalid action '{action!s}'".format(action=action),
)
msg = f"Invalid MANIFEST.in: unknown action {action!r} in {line!r}"
raise DistutilsInternalError(msg) from None

# OK, now we know that the action is valid and we have the
# right number of words on the line for that action -- so we
Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class NoValue:
yield
finally:
if orig_val is not NoValue:
setattr(os, 'link', orig_val)
os.link = orig_val

def add_defaults(self):
super().add_defaults()
Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def with_project_on_sys_path(self, func):
func()

@contextlib.contextmanager
def project_on_sys_path(self, include_dists=[]):
def project_on_sys_path(self, include_dists=()):
self.run_command('egg_info')

# Build extensions in-place
Expand Down
2 changes: 1 addition & 1 deletion setuptools/config/_validate_pyproject/error_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"must not be there",
)

_NEED_DETAILS = {"anyOf", "oneOf", "anyOf", "contains", "propertyNames", "not", "items"}
_NEED_DETAILS = {"anyOf", "oneOf", "allOf", "contains", "propertyNames", "not", "items"}

_CAMEL_CASE_SPLITTER = re.compile(r"\W+|([A-Z][^A-Z\W]*)")
_IDENTIFIER = re.compile(r"^[\w_]+$", re.I)
Expand Down
8 changes: 4 additions & 4 deletions setuptools/config/setupcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ def __setitem__(self, option_name, value):

try:
current_value = getattr(target_obj, option_name)
except AttributeError:
raise KeyError(option_name)
except AttributeError as e:
raise KeyError(option_name) from e

if current_value:
# Already inhabited. Skipping.
Expand Down Expand Up @@ -582,11 +582,11 @@ def _parse_version(self, value):
# accidentally include newlines and other unintended content
try:
Version(version)
except InvalidVersion:
except InvalidVersion as e:
raise OptionError(
f'Version loaded from {value} does not '
f'comply with PEP 440: {version}'
)
) from e

return version

Expand Down
2 changes: 1 addition & 1 deletion setuptools/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def patch_func(replacement, target_mod, func_name):


def get_unpatched_function(candidate):
return getattr(candidate, 'unpatched')
return candidate.unpatched


def patch_for_msvc_specialized_compiler():
Expand Down
16 changes: 6 additions & 10 deletions setuptools/tests/config/test_setupcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,17 +957,13 @@ class TestExternalSetters:
# pbr or something else setting these values.
def _fake_distribution_init(self, dist, attrs):
saved_dist_init(dist, attrs)
# see self._DISTUTUILS_UNSUPPORTED_METADATA
setattr(dist.metadata, 'long_description_content_type', 'text/something')
# see self._DISTUTILS_UNSUPPORTED_METADATA
dist.metadata.long_description_content_type = 'text/something'
# Test overwrite setup() args
setattr(
dist.metadata,
'project_urls',
{
'Link One': 'https://example.com/one/',
'Link Two': 'https://example.com/two/',
},
)
dist.metadata.project_urls = {
'Link One': 'https://example.com/one/',
'Link Two': 'https://example.com/two/',
}
return None

@patch.object(_Distribution, '__init__', autospec=True)
Expand Down
4 changes: 2 additions & 2 deletions setuptools/tests/test_build_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@


class BuildBackendBase:
def __init__(self, cwd='.', env={}, backend_name='setuptools.build_meta'):
def __init__(self, cwd='.', env=None, backend_name='setuptools.build_meta'):
self.cwd = cwd
self.env = env
self.env = env or {}
self.backend_name = backend_name


Expand Down
5 changes: 3 additions & 2 deletions setuptools/tests/test_easy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ def create_setup_requires_package(
version='0.1',
make_package=make_trivial_sdist,
setup_py_template=None,
setup_attrs={},
setup_attrs=None,
use_setup_cfg=(),
):
"""Creates a source tree under path for a trivial test package that has a
Expand All @@ -1213,7 +1213,8 @@ def create_setup_requires_package(
'setup_requires': ['%s==%s' % (distname, version)],
'dependency_links': [os.path.abspath(path)],
}
test_setup_attrs.update(setup_attrs)
if setup_attrs:
test_setup_attrs.update(setup_attrs)

test_pkg = os.path.join(path, 'test_pkg')
os.mkdir(test_pkg)
Expand Down
2 changes: 1 addition & 1 deletion setuptools/tests/test_msvc14.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_get_vc2017(self):
# This function cannot be mocked, so pass it if we find VS 2017
# and mark it skipped if we do not.
version, path = _msvccompiler._msvc14_find_vc2017()
if os.environ.get('APPVEYOR_BUILD_WORKER_IMAGE', '') in ['Visual Studio 2017']:
if os.environ.get('APPVEYOR_BUILD_WORKER_IMAGE', '') == 'Visual Studio 2017':
assert version
if version:
assert version >= 15
Expand Down
2 changes: 1 addition & 1 deletion setuptools/tests/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ def test_sdist_with_latin1_encoded_filename(self):
else:
# The Latin-1 filename should have been skipped
filename = filename.decode('latin-1')
filename not in cmd.filelist.files
assert filename not in cmd.filelist.files

_EXAMPLE_DIRECTIVES = {
"setup.cfg - long_description and version": """
Expand Down
6 changes: 3 additions & 3 deletions setuptools/tests/test_setuptools.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ def f1():
assert dep.extract_constant(fc, 'q', -1) is None

# constant assigned
dep.extract_constant(fc, 'x', -1) == "test"
assert dep.extract_constant(fc, 'x', -1) == "test"

# expression assigned
dep.extract_constant(fc, 'y', -1) == -1
assert dep.extract_constant(fc, 'y', -1) == -1

# recognized name, not assigned
dep.extract_constant(fc, 'z', -1) is None
assert dep.extract_constant(fc, 'z', -1) is None

def testFindModule(self):
with pytest.raises(ImportError):
Expand Down
4 changes: 2 additions & 2 deletions tools/build_launchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def get_msbuild():
]
try:
return subprocess.check_output(cmd, encoding='utf-8', text=True).strip()
except subprocess.CalledProcessError:
raise SystemExit("Unable to find MSBuild; check Visual Studio install")
except subprocess.CalledProcessError as e:
raise SystemExit("Unable to find MSBuild; check Visual Studio install") from e


def do_build(arena, platform, target):
Expand Down

0 comments on commit 25b57b4

Please sign in to comment.