From 3acab2caf956f16befee832223e63fecb28562d4 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 3 Jan 2025 02:11:44 -0500 Subject: [PATCH] Simplified typed assignments --- pkg_resources/__init__.py | 4 +- setuptools/command/_requirestxt.py | 4 +- setuptools/command/bdist_wheel.py | 2 +- setuptools/command/editable_wheel.py | 2 +- setuptools/config/pyprojecttoml.py | 2 +- setuptools/config/setupcfg.py | 2 +- setuptools/dist.py | 4 +- setuptools/msvc.py | 87 ++++++++++++++-------------- setuptools/tests/test_wheel.py | 20 +++---- 9 files changed, 62 insertions(+), 65 deletions(-) diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 74b0465bfa..d175100f2a 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -875,9 +875,7 @@ def resolve( # Mapping of requirement to set of distributions that required it; # useful for reporting info about conflicts. - required_by: collections.defaultdict[Requirement, set[str]] = ( - collections.defaultdict(set) - ) + required_by = collections.defaultdict[Requirement, set[str]](set) while requirements: # process dependencies breadth-first diff --git a/setuptools/command/_requirestxt.py b/setuptools/command/_requirestxt.py index 171f41b87e..9029b12514 100644 --- a/setuptools/command/_requirestxt.py +++ b/setuptools/command/_requirestxt.py @@ -38,13 +38,13 @@ def _prepare( def _convert_extras_requirements( extras_require: Mapping[str, _StrOrIter], -) -> Mapping[str, _Ordered[Requirement]]: +) -> defaultdict[str, _Ordered[Requirement]]: """ Convert requirements in `extras_require` of the form `"extra": ["barbazquux; {marker}"]` to `"extra:{marker}": ["barbazquux"]`. """ - output: Mapping[str, _Ordered[Requirement]] = defaultdict(dict) + output = defaultdict[str, _Ordered[Requirement]](dict) for section, v in extras_require.items(): # Do not strip empty sections. output[section] diff --git a/setuptools/command/bdist_wheel.py b/setuptools/command/bdist_wheel.py index 234df2a7c7..32ed1378c9 100644 --- a/setuptools/command/bdist_wheel.py +++ b/setuptools/command/bdist_wheel.py @@ -500,7 +500,7 @@ def license_paths(self) -> Iterable[str]: # Setuptools has resolved any patterns to actual file names return self.distribution.metadata.license_files or () - files: set[str] = set() + files = set[str]() metadata = self.distribution.get_option_dict("metadata") if setuptools_major_version >= 42: # Setuptools recognizes the license_files option but does not do globbing diff --git a/setuptools/command/editable_wheel.py b/setuptools/command/editable_wheel.py index 6d23d11fad..b03e677757 100644 --- a/setuptools/command/editable_wheel.py +++ b/setuptools/command/editable_wheel.py @@ -506,7 +506,7 @@ def template_vars(self) -> tuple[str, str, dict[str, str], dict[str, list[str]]] package_dir = self.dist.package_dir or {} roots = _find_package_roots(top_level, package_dir, src_root) - namespaces_: dict[str, list[str]] = dict( + namespaces_ = dict( chain( _find_namespaces(self.dist.packages or [], roots), ((ns, []) for ns in _find_virtual_namespaces(roots)), diff --git a/setuptools/config/pyprojecttoml.py b/setuptools/config/pyprojecttoml.py index 15b0baa18e..fd6c5968c8 100644 --- a/setuptools/config/pyprojecttoml.py +++ b/setuptools/config/pyprojecttoml.py @@ -185,7 +185,7 @@ def __init__( self.dynamic_cfg = self.setuptools_cfg.get("dynamic", {}) self.ignore_option_errors = ignore_option_errors self._dist = dist - self._referenced_files: set[str] = set() + self._referenced_files = set[str]() def _ensure_dist(self) -> Distribution: from setuptools.dist import Distribution diff --git a/setuptools/config/setupcfg.py b/setuptools/config/setupcfg.py index b35d0b00cd..0d0d73c77c 100644 --- a/setuptools/config/setupcfg.py +++ b/setuptools/config/setupcfg.py @@ -253,7 +253,7 @@ def __init__( self.sections = dict(self._section_options(options)) self.set_options: list[str] = [] self.ensure_discovered = ensure_discovered - self._referenced_files: set[str] = set() + self._referenced_files = set[str]() """After parsing configurations, this property will enumerate all files referenced by the "file:" directive. Private API for setuptools only. """ diff --git a/setuptools/dist.py b/setuptools/dist.py index 5b3175fb5b..5c7e0da4e7 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -321,7 +321,7 @@ def __init__(self, attrs: MutableMapping[str, Any] | None = None) -> None: # Private API (setuptools-use only, not restricted to Distribution) # Stores files that are referenced by the configuration and need to be in the # sdist (e.g. `version = file: VERSION.txt`) - self._referenced_files: set[str] = set() + self._referenced_files = set[str]() self.set_defaults = ConfigDiscovery(self) @@ -400,7 +400,7 @@ def _normalize_requires(self): def _finalize_license_files(self) -> None: """Compute names of all license files which should be included.""" license_files: list[str] | None = self.metadata.license_files - patterns: list[str] = license_files if license_files else [] + patterns = license_files or [] license_file: str | None = self.metadata.license_file if license_file and license_file not in patterns: diff --git a/setuptools/msvc.py b/setuptools/msvc.py index 8d6d2cf084..9c9a63568e 100644 --- a/setuptools/msvc.py +++ b/setuptools/msvc.py @@ -108,7 +108,7 @@ def current_dir(self, hidex86=False, x64=False) -> str: if (self.current_cpu == 'x86' and hidex86) else r'\x64' if (self.current_cpu == 'amd64' and x64) - else r'\%s' % self.current_cpu + else rf'\{self.current_cpu}' ) def target_dir(self, hidex86=False, x64=False) -> str: @@ -132,7 +132,7 @@ def target_dir(self, hidex86=False, x64=False) -> str: if (self.target_cpu == 'x86' and hidex86) else r'\x64' if (self.target_cpu == 'amd64' and x64) - else r'\%s' % self.target_cpu + else rf'\{self.target_cpu}' ) def cross_dir(self, forcex86=False): @@ -155,7 +155,7 @@ def cross_dir(self, forcex86=False): return ( '' if self.target_cpu == current - else self.target_dir().replace('\\', '\\%s_' % current) + else self.target_dir().replace('\\', f'\\{current}_') ) @@ -497,11 +497,11 @@ def VSInstallDir(self): """ # Default path default = os.path.join( - self.ProgramFilesx86, 'Microsoft Visual Studio %0.1f' % self.vs_ver + self.ProgramFilesx86, f'Microsoft Visual Studio {self.vs_ver:0.1f}' ) # Try to get path from registry, if fail use default path - return self.ri.lookup(self.ri.vs, '%0.1f' % self.vs_ver) or default + return self.ri.lookup(self.ri.vs, f'{self.vs_ver:0.1f}') or default @property def VCInstallDir(self): @@ -561,16 +561,17 @@ def _guess_vc_legacy(self): path """ default = os.path.join( - self.ProgramFilesx86, r'Microsoft Visual Studio %0.1f\VC' % self.vs_ver + self.ProgramFilesx86, + rf'Microsoft Visual Studio {self.vs_ver:0.1f}\VC', ) # Try to get "VC++ for Python" path from registry as default path - reg_path = os.path.join(self.ri.vc_for_python, '%0.1f' % self.vs_ver) + reg_path = os.path.join(self.ri.vc_for_python, f'{self.vs_ver:0.1f}') python_vc = self.ri.lookup(reg_path, 'installdir') default_vc = os.path.join(python_vc, 'VC') if python_vc else default # Try to get path from registry, if fail use default path - return self.ri.lookup(self.ri.vc, '%0.1f' % self.vs_ver) or default_vc + return self.ri.lookup(self.ri.vc, f'{self.vs_ver:0.1f}') or default_vc @property def WindowsSdkVersion(self) -> tuple[LiteralString, ...]: @@ -619,13 +620,13 @@ def WindowsSdkDir(self) -> str | None: # noqa: C901 # is too complex (12) # F sdkdir: str | None = '' for ver in self.WindowsSdkVersion: # Try to get it from registry - loc = os.path.join(self.ri.windows_sdk, 'v%s' % ver) + loc = os.path.join(self.ri.windows_sdk, f'v{ver}') sdkdir = self.ri.lookup(loc, 'installationfolder') if sdkdir: break if not sdkdir or not os.path.isdir(sdkdir): # Try to get "VC++ for Python" version from registry - path = os.path.join(self.ri.vc_for_python, '%0.1f' % self.vc_ver) + path = os.path.join(self.ri.vc_for_python, f'{self.vc_ver:0.1f}') install_base = self.ri.lookup(path, 'installdir') if install_base: sdkdir = os.path.join(install_base, 'WinSDK') @@ -633,14 +634,14 @@ def WindowsSdkDir(self) -> str | None: # noqa: C901 # is too complex (12) # F # If fail, use default new path for ver in self.WindowsSdkVersion: intver = ver[: ver.rfind('.')] - path = r'Microsoft SDKs\Windows Kits\%s' % intver + path = rf'Microsoft SDKs\Windows Kits\{intver}' d = os.path.join(self.ProgramFiles, path) if os.path.isdir(d): sdkdir = d if not sdkdir or not os.path.isdir(sdkdir): # If fail, use default old path for ver in self.WindowsSdkVersion: - path = r'Microsoft SDKs\Windows\v%s' % ver + path = rf'Microsoft SDKs\Windows\v{ver}' d = os.path.join(self.ProgramFiles, path) if os.path.isdir(d): sdkdir = d @@ -666,8 +667,8 @@ def WindowsSDKExecutablePath(self): else: netfxver = 40 hidex86 = True if self.vs_ver <= 12.0 else False - arch = self.pi.current_dir(x64=True, hidex86=hidex86) - fx = 'WinSDK-NetFx%dTools%s' % (netfxver, arch.replace('\\', '-')) + arch = self.pi.current_dir(x64=True, hidex86=hidex86).replace('\\', '-') + fx = f'WinSDK-NetFx{netfxver}Tools{arch}' # list all possibles registry paths regpaths = [] @@ -676,7 +677,7 @@ def WindowsSDKExecutablePath(self): regpaths += [os.path.join(self.ri.netfx_sdk, ver, fx)] for ver in self.WindowsSdkVersion: - regpaths += [os.path.join(self.ri.windows_sdk, 'v%sA' % ver, fx)] + regpaths += [os.path.join(self.ri.windows_sdk, f'v{ver}A', fx)] # Return installation folder from the more recent path for path in regpaths: @@ -696,7 +697,7 @@ def FSharpInstallDir(self): str path """ - path = os.path.join(self.ri.visualstudio, r'%0.1f\Setup\F#' % self.vs_ver) + path = os.path.join(self.ri.visualstudio, rf'{self.vs_ver:0.1f}\Setup\F#') return self.ri.lookup(path, 'productdir') or '' @property @@ -714,7 +715,7 @@ def UniversalCRTSdkDir(self): # Find path of the more recent Kit for ver in vers: - sdkdir = self.ri.lookup(self.ri.windows_kits_roots, 'kitsroot%s' % ver) + sdkdir = self.ri.lookup(self.ri.windows_kits_roots, f'kitsroot{ver}') if sdkdir: return sdkdir or '' @@ -838,8 +839,8 @@ def _find_dot_net_versions(self, bits) -> tuple[str, ...]: versions """ # Find actual .NET version in registry - reg_ver = self.ri.lookup(self.ri.vc, 'frameworkver%d' % bits) - dot_net_dir = getattr(self, 'FrameworkDir%d' % bits) + reg_ver = self.ri.lookup(self.ri.vc, f'frameworkver{bits}') + dot_net_dir = getattr(self, f'FrameworkDir{bits}') ver = reg_ver or self._use_last_dir_name(dot_net_dir, 'v') or '' # Set .NET versions for specified MSVC++ version @@ -960,7 +961,7 @@ def VSTools(self): arch_subdir = self.pi.current_dir(hidex86=True, x64=True) paths += [r'Common7\IDE\CommonExtensions\Microsoft\TestWindow'] paths += [r'Team Tools\Performance Tools'] - paths += [r'Team Tools\Performance Tools%s' % arch_subdir] + paths += [rf'Team Tools\Performance Tools{arch_subdir}'] return [os.path.join(self.si.VSInstallDir, path) for path in paths] @@ -993,10 +994,10 @@ def VCLibraries(self): arch_subdir = self.pi.target_dir(x64=True) else: arch_subdir = self.pi.target_dir(hidex86=True) - paths = ['Lib%s' % arch_subdir, r'ATLMFC\Lib%s' % arch_subdir] + paths = [f'Lib{arch_subdir}', rf'ATLMFC\Lib{arch_subdir}'] if self.vs_ver >= 14.0: - paths += [r'Lib\store%s' % arch_subdir] + paths += [rf'Lib\store{arch_subdir}'] return [os.path.join(self.si.VCInstallDir, path) for path in paths] @@ -1030,10 +1031,10 @@ def VCTools(self): forcex86 = True if self.vs_ver <= 10.0 else False arch_subdir = self.pi.cross_dir(forcex86) if arch_subdir: - tools += [os.path.join(si.VCInstallDir, 'Bin%s' % arch_subdir)] + tools += [os.path.join(si.VCInstallDir, f'Bin{arch_subdir}')] if self.vs_ver == 14.0: - path = 'Bin%s' % self.pi.current_dir(hidex86=True) + path = f'Bin{self.pi.current_dir(hidex86=True)}' tools += [os.path.join(si.VCInstallDir, path)] elif self.vs_ver >= 15.0: @@ -1068,13 +1069,13 @@ def OSLibraries(self): """ if self.vs_ver <= 10.0: arch_subdir = self.pi.target_dir(hidex86=True, x64=True) - return [os.path.join(self.si.WindowsSdkDir, 'Lib%s' % arch_subdir)] + return [os.path.join(self.si.WindowsSdkDir, f'Lib{arch_subdir}')] else: arch_subdir = self.pi.target_dir(x64=True) lib = os.path.join(self.si.WindowsSdkDir, 'lib') libver = self._sdk_subdir - return [os.path.join(lib, '%sum%s' % (libver, arch_subdir))] + return [os.path.join(lib, f'{libver}um{arch_subdir}')] @property def OSIncludes(self): @@ -1097,9 +1098,9 @@ def OSIncludes(self): else: sdkver = '' return [ - os.path.join(include, '%sshared' % sdkver), - os.path.join(include, '%sum' % sdkver), - os.path.join(include, '%swinrt' % sdkver), + os.path.join(include, f'{sdkver}shared'), + os.path.join(include, f'{sdkver}um'), + os.path.join(include, f'{sdkver}winrt'), ] @property @@ -1134,7 +1135,7 @@ def OSLibpath(self): self.si.WindowsSdkDir, 'ExtensionSDKs', 'Microsoft.VCLibs', - '%0.1f' % self.vs_ver, + f'{self.vs_ver:0.1f}', 'References', 'CommonConfiguration', 'neutral', @@ -1169,7 +1170,7 @@ def _sdk_tools(self): if not self.pi.current_is_x86(): arch_subdir = self.pi.current_dir(x64=True) - path = 'Bin%s' % arch_subdir + path = f'Bin{arch_subdir}' yield os.path.join(self.si.WindowsSdkDir, path) if self.vs_ver in (10.0, 11.0): @@ -1177,14 +1178,14 @@ def _sdk_tools(self): arch_subdir = '' else: arch_subdir = self.pi.current_dir(hidex86=True, x64=True) - path = r'Bin\NETFX 4.0 Tools%s' % arch_subdir + path = rf'Bin\NETFX 4.0 Tools{arch_subdir}' yield os.path.join(self.si.WindowsSdkDir, path) elif self.vs_ver >= 15.0: path = os.path.join(self.si.WindowsSdkDir, 'Bin') arch_subdir = self.pi.current_dir(x64=True) sdkver = self.si.WindowsSdkLastVersion - yield os.path.join(path, '%s%s' % (sdkver, arch_subdir)) + yield os.path.join(path, f'{sdkver}{arch_subdir}') if self.si.WindowsSDKExecutablePath: yield self.si.WindowsSDKExecutablePath @@ -1200,7 +1201,7 @@ def _sdk_subdir(self): subdir """ ucrtver = self.si.WindowsSdkLastVersion - return ('%s\\' % ucrtver) if ucrtver else '' + return (f'{ucrtver}\\') if ucrtver else '' @property def SdkSetup(self): @@ -1262,7 +1263,7 @@ def NetFxSDKLibraries(self): return [] arch_subdir = self.pi.target_dir(x64=True) - return [os.path.join(self.si.NetFxSdkDir, r'lib\um%s' % arch_subdir)] + return [os.path.join(self.si.NetFxSdkDir, rf'lib\um{arch_subdir}')] @property def NetFxSDKIncludes(self): @@ -1310,7 +1311,7 @@ def MSBuild(self): base_path = self.si.VSInstallDir arch_subdir = '' - path = r'MSBuild\%0.1f\bin%s' % (self.vs_ver, arch_subdir) + path = rf'MSBuild\{self.vs_ver:0.1f}\bin{arch_subdir}' build = [os.path.join(base_path, path)] if self.vs_ver >= 15.0: @@ -1350,7 +1351,7 @@ def UCRTLibraries(self): arch_subdir = self.pi.target_dir(x64=True) lib = os.path.join(self.si.UniversalCRTSdkDir, 'lib') ucrtver = self._ucrt_subdir - return [os.path.join(lib, '%sucrt%s' % (ucrtver, arch_subdir))] + return [os.path.join(lib, f'{ucrtver}ucrt{arch_subdir}')] @property def UCRTIncludes(self): @@ -1366,7 +1367,7 @@ def UCRTIncludes(self): return [] include = os.path.join(self.si.UniversalCRTSdkDir, 'include') - return [os.path.join(include, '%sucrt' % self._ucrt_subdir)] + return [os.path.join(include, f'{self._ucrt_subdir}ucrt')] @property def _ucrt_subdir(self): @@ -1379,7 +1380,7 @@ def _ucrt_subdir(self): subdir """ ucrtver = self.si.UniversalCRTSdkLastVersion - return ('%s\\' % ucrtver) if ucrtver else '' + return (f'{ucrtver}\\') if ucrtver else '' @property def FSharp(self): @@ -1403,7 +1404,7 @@ def VCRuntimeRedist(self) -> str | None: Returns the first suitable path found or None. """ - vcruntime = 'vcruntime%d0.dll' % self.vc_ver + vcruntime = f'vcruntime{self.vc_ver}0.dll' arch_subdir = self.pi.target_dir(x64=True).strip('\\') # Installation prefixes candidates @@ -1419,9 +1420,9 @@ def VCRuntimeRedist(self) -> str | None: # CRT directory crt_dirs = ( - 'Microsoft.VC%d.CRT' % (self.vc_ver * 10), + f'Microsoft.VC{self.vc_ver * 10}.CRT', # Sometime store in directory with VS version instead of VC - 'Microsoft.VC%d.CRT' % (int(self.vs_ver) * 10), + f'Microsoft.VC{int(self.vs_ver) * 10}.CRT', ) # vcruntime path @@ -1520,7 +1521,7 @@ def _build_paths(self, name, spec_path_lists, exists): paths = itertools.chain(spec_paths, env_paths) extant_paths = list(filter(os.path.isdir, paths)) if exists else paths if not extant_paths: - msg = "%s environment variable is empty" % name.upper() + msg = f"{name.upper()} environment variable is empty" raise distutils.errors.DistutilsPlatformError(msg) unique_paths = unique_everseen(extant_paths) return os.pathsep.join(unique_paths) diff --git a/setuptools/tests/test_wheel.py b/setuptools/tests/test_wheel.py index 5724c6eabc..70165c608b 100644 --- a/setuptools/tests/test_wheel.py +++ b/setuptools/tests/test_wheel.py @@ -176,7 +176,7 @@ def __init__(self, id, **kwargs): self._fields = kwargs def __repr__(self) -> str: - return '%s(**%r)' % (self._id, self._fields) + return f'{self._id}(**{self._fields!r})' # Using Any to avoid possible type union issues later in test @@ -367,11 +367,10 @@ def __repr__(self) -> str: ), dict( id='requires2', - install_requires=""" + install_requires=f""" bar - foo<=2.0; %r in sys_platform - """ - % sys.platform, + foo<=2.0; {sys.platform!r} in sys_platform + """, requires_txt=DALS( """ bar @@ -381,10 +380,9 @@ def __repr__(self) -> str: ), dict( id='requires3', - install_requires=""" - bar; %r != sys_platform - """ - % sys.platform, + install_requires=f""" + bar; {sys.platform!r} != sys_platform + """, ), dict( id='requires4', @@ -406,7 +404,7 @@ def __repr__(self) -> str: dict( id='requires5', extras_require={ - 'extra': 'foobar; %r != sys_platform' % sys.platform, + 'extra': f'foobar; {sys.platform!r} != sys_platform', }, requires_txt=DALS( """ @@ -605,7 +603,7 @@ def test_wheel_install_pep_503(): def test_wheel_no_dist_dir(): project_name = 'nodistinfo' version = '1.0' - wheel_name = '{0}-{1}-py2.py3-none-any.whl'.format(project_name, version) + wheel_name = f'{project_name}-{version}-py2.py3-none-any.whl' with tempdir() as source_dir: wheel_path = os.path.join(source_dir, wheel_name) # create an empty zip file