From 09b9094f92093c9c22c9fe5185e456fe1778d606 Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 19 Nov 2024 00:18:04 -0500 Subject: [PATCH] runtime typing fixes for typeshed return type merge --- setuptools/build_meta.py | 4 ++-- setuptools/command/bdist_egg.py | 2 +- setuptools/command/bdist_wheel.py | 2 +- setuptools/command/build_ext.py | 4 ++-- setuptools/command/build_py.py | 16 ++++++---------- setuptools/command/easy_install.py | 2 +- setuptools/command/editable_wheel.py | 2 +- setuptools/command/egg_info.py | 3 ++- setuptools/command/install_egg_info.py | 2 +- setuptools/command/saveopts.py | 2 +- setuptools/command/sdist.py | 4 ++-- setuptools/command/setopt.py | 2 +- setuptools/dist.py | 3 ++- setuptools/monkey.py | 6 +++--- setuptools/msvc.py | 4 ++-- setuptools/unicode_utils.py | 6 +++--- 16 files changed, 31 insertions(+), 33 deletions(-) diff --git a/setuptools/build_meta.py b/setuptools/build_meta.py index 23471accb6f..00fa5e1f704 100644 --- a/setuptools/build_meta.py +++ b/setuptools/build_meta.py @@ -91,11 +91,11 @@ def patch(cls): for the duration of this context. """ orig = distutils.core.Distribution - distutils.core.Distribution = cls + distutils.core.Distribution = cls # type: ignore[misc] # monkeypatching try: yield finally: - distutils.core.Distribution = orig + distutils.core.Distribution = orig # type: ignore[misc] # monkeypatching @contextlib.contextmanager diff --git a/setuptools/command/bdist_egg.py b/setuptools/command/bdist_egg.py index 04d7e945bc1..ac3e6ef1f9f 100644 --- a/setuptools/command/bdist_egg.py +++ b/setuptools/command/bdist_egg.py @@ -277,7 +277,7 @@ def zip_safe(self): log.warn("zip_safe flag not set; analyzing archive contents...") return analyze_egg(self.bdist_dir, self.stubs) - def gen_header(self) -> str: + def gen_header(self) -> Literal["w"]: return 'w' def copy_metadata_to(self, target_dir) -> None: diff --git a/setuptools/command/bdist_wheel.py b/setuptools/command/bdist_wheel.py index 5855a8a832e..07241004bf6 100644 --- a/setuptools/command/bdist_wheel.py +++ b/setuptools/command/bdist_wheel.py @@ -220,7 +220,7 @@ class bdist_wheel(Command): def initialize_options(self) -> None: self.bdist_dir: str | None = None - self.data_dir: str | None = None + self.data_dir = "" self.plat_name: str | None = None self.plat_tag: str | None = None self.format = "zip" diff --git a/setuptools/command/build_ext.py b/setuptools/command/build_ext.py index f098246b9bc..e5c6b76b381 100644 --- a/setuptools/command/build_ext.py +++ b/setuptools/command/build_ext.py @@ -95,7 +95,7 @@ class build_ext(_build_ext): def run(self): """Build extensions in build directory, then copy if --inplace""" - old_inplace, self.inplace = self.inplace, 0 + old_inplace, self.inplace = self.inplace, False _build_ext.run(self) self.inplace = old_inplace if old_inplace: @@ -248,7 +248,7 @@ def setup_shlib_compiler(self): compiler.set_link_objects(self.link_objects) # hack so distutils' build_extension() builds a library instead - compiler.link_shared_object = link_shared_object.__get__(compiler) + compiler.link_shared_object = link_shared_object.__get__(compiler) # type: ignore[method-assign] def get_export_symbols(self, ext): if isinstance(ext, Library): diff --git a/setuptools/command/build_py.py b/setuptools/command/build_py.py index f8c9b11676d..004a889c7e9 100644 --- a/setuptools/command/build_py.py +++ b/setuptools/command/build_py.py @@ -39,7 +39,7 @@ class build_py(orig.build_py): distribution: Distribution # override distutils.dist.Distribution with setuptools.dist.Distribution editable_mode: bool = False - existing_egg_info_dir: str | None = None #: Private API, internal use only. + existing_egg_info_dir: StrPath | None = None #: Private API, internal use only. def finalize_options(self): orig.build_py.finalize_options(self) @@ -47,7 +47,6 @@ def finalize_options(self): self.exclude_package_data = self.distribution.exclude_package_data or {} if 'data_files' in self.__dict__: del self.__dict__['data_files'] - self.__updated_files = [] def copy_file( # type: ignore[override] # No overload, no bytes support self, @@ -90,10 +89,7 @@ def __getattr__(self, attr: str): return orig.build_py.__getattr__(self, attr) def build_module(self, module, module_file, package): - outfile, copied = orig.build_py.build_module(self, module, module_file, package) - if copied: - self.__updated_files.append(outfile) - return outfile, copied + return orig.build_py.build_module(self, module, module_file, package) def _get_data_files(self): """Generate list of '(package,src_dir,build_dir,filenames)' tuples""" @@ -179,16 +175,16 @@ def build_package_data(self) -> None: make_writable(target) def analyze_manifest(self): - self.manifest_files = mf = {} + self.manifest_files: dict[str, list[str]] = {} if not self.distribution.include_package_data: return - src_dirs = {} + src_dirs: dict[str, str] = {} for package in self.packages or (): # Locate package source directory src_dirs[assert_relative(self.get_package_dir(package))] = package if ( - getattr(self, 'existing_egg_info_dir', None) + self.existing_egg_info_dir and Path(self.existing_egg_info_dir, "SOURCES.txt").exists() ): egg_info_dir = self.existing_egg_info_dir @@ -217,7 +213,7 @@ def analyze_manifest(self): importable = check.importable_subpackage(src_dirs[d], f) if importable: check.warn(importable) - mf.setdefault(src_dirs[d], []).append(path) + self.manifest_files.setdefault(src_dirs[d], []).append(path) def _filter_build_files(self, files: Iterable[str], egg_info: str) -> Iterator[str]: """ diff --git a/setuptools/command/easy_install.py b/setuptools/command/easy_install.py index b40610f8ba2..48c35e4bf21 100644 --- a/setuptools/command/easy_install.py +++ b/setuptools/command/easy_install.py @@ -354,7 +354,7 @@ def finalize_options(self): # noqa: C901 # is too complex (25) # FIXME "No urls, filenames, or requirements specified (see --help)" ) - self.outputs = [] + self.outputs: list[str] = [] @staticmethod def _process_site_dirs(site_dirs): diff --git a/setuptools/command/editable_wheel.py b/setuptools/command/editable_wheel.py index db9a50c3af2..2bbb10af760 100644 --- a/setuptools/command/editable_wheel.py +++ b/setuptools/command/editable_wheel.py @@ -784,7 +784,7 @@ def __init__(self, distribution, installation_dir, editable_name, src_root): self.src_root = src_root self.installation_dir = installation_dir self.editable_name = editable_name - self.outputs = [] + self.outputs: list[str] = [] self.dry_run = False def _get_nspkg_file(self): diff --git a/setuptools/command/egg_info.py b/setuptools/command/egg_info.py index 1411ac3d892..cd69f7c7dc6 100644 --- a/setuptools/command/egg_info.py +++ b/setuptools/command/egg_info.py @@ -7,6 +7,7 @@ import re import sys import time +from collections.abc import Callable import packaging import packaging.requirements @@ -338,7 +339,7 @@ def process_template_line(self, line): # patterns, (dir and patterns), or (dir_pattern). (action, patterns, dir, dir_pattern) = self._parse_template_line(line) - action_map = { + action_map: dict[str, Callable] = { 'include': self.include, 'exclude': self.exclude, 'global-include': self.global_include, diff --git a/setuptools/command/install_egg_info.py b/setuptools/command/install_egg_info.py index be4dd7b2291..b6f1786f4e1 100644 --- a/setuptools/command/install_egg_info.py +++ b/setuptools/command/install_egg_info.py @@ -26,7 +26,7 @@ def finalize_options(self): basename = f"{ei_cmd._get_egg_basename()}.egg-info" self.source = ei_cmd.egg_info self.target = os.path.join(self.install_dir, basename) - self.outputs = [] + self.outputs: list[str] = [] def run(self) -> None: self.run_command('egg_info') diff --git a/setuptools/command/saveopts.py b/setuptools/command/saveopts.py index f175de1015c..939dabd12ad 100644 --- a/setuptools/command/saveopts.py +++ b/setuptools/command/saveopts.py @@ -8,7 +8,7 @@ class saveopts(option_base): def run(self): dist = self.distribution - settings = {} + settings: dict[str, dict[str, str]] = {} for cmd in dist.command_options: if cmd == 'saveopts': diff --git a/setuptools/command/sdist.py b/setuptools/command/sdist.py index be69b335002..64e866c96b1 100644 --- a/setuptools/command/sdist.py +++ b/setuptools/command/sdist.py @@ -202,10 +202,10 @@ def read_manifest(self): """ log.info("reading manifest file '%s'", self.manifest) manifest = open(self.manifest, 'rb') - for line in manifest: + for bytes_line in manifest: # The manifest must contain UTF-8. See #303. try: - line = line.decode('UTF-8') + line = bytes_line.decode('UTF-8') except UnicodeDecodeError: log.warn("%r not UTF-8 decodable -- skipping" % line) continue diff --git a/setuptools/command/setopt.py b/setuptools/command/setopt.py index 75393f32f0b..200cdff0f72 100644 --- a/setuptools/command/setopt.py +++ b/setuptools/command/setopt.py @@ -37,7 +37,7 @@ def edit_config(filename, settings, dry_run=False): """ log.debug("Reading configuration from %s", filename) opts = configparser.RawConfigParser() - opts.optionxform = lambda x: x + opts.optionxform = lambda optionstr: optionstr # type: ignore[method-assign] # overriding method _cfg_read_utf8_with_fallback(opts, filename) for section, options in settings.items(): diff --git a/setuptools/dist.py b/setuptools/dist.py index 6062c4f8687..c0792294d9f 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -914,9 +914,10 @@ def get_cmdline_options(self): Note that options provided by config files are intentionally excluded. """ - d = {} + d: dict[str, dict[str, str | None]] = {} for cmd, opts in self.command_options.items(): + val: str | None for opt, (src, val) in opts.items(): if src != "command line": continue diff --git a/setuptools/monkey.py b/setuptools/monkey.py index 07919722b8a..d8e30dbb804 100644 --- a/setuptools/monkey.py +++ b/setuptools/monkey.py @@ -73,7 +73,7 @@ def patch_all(): import setuptools # we can't patch distutils.cmd, alas - distutils.core.Command = setuptools.Command + distutils.core.Command = setuptools.Command # type: ignore[misc,assignment] # monkeypatching _patch_distribution_metadata() @@ -82,8 +82,8 @@ def patch_all(): module.Distribution = setuptools.dist.Distribution # Install the patched Extension - distutils.core.Extension = setuptools.extension.Extension - distutils.extension.Extension = setuptools.extension.Extension + distutils.core.Extension = setuptools.extension.Extension # type: ignore[misc,assignment] # monkeypatching + distutils.extension.Extension = setuptools.extension.Extension # type: ignore[misc,assignment] # monkeypatching if 'distutils.command.build_ext' in sys.modules: sys.modules[ 'distutils.command.build_ext' diff --git a/setuptools/msvc.py b/setuptools/msvc.py index 6492d3be9d0..0b833376c21 100644 --- a/setuptools/msvc.py +++ b/setuptools/msvc.py @@ -436,7 +436,7 @@ def find_programdata_vs_vers(self): dict float version as key, path as value. """ - vs_versions = {} + vs_versions: dict[float, str] = {} instances_dir = r'C:\ProgramData\Microsoft\VisualStudio\Packages\_Instances' try: @@ -616,7 +616,7 @@ def WindowsSdkDir(self): # noqa: C901 # is too complex (12) # FIXME str path """ - sdkdir = '' + 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) diff --git a/setuptools/unicode_utils.py b/setuptools/unicode_utils.py index 862d79e8985..a6e33f2e0d2 100644 --- a/setuptools/unicode_utils.py +++ b/setuptools/unicode_utils.py @@ -1,6 +1,6 @@ import sys import unicodedata -from configparser import ConfigParser +from configparser import RawConfigParser from .compat import py39 from .warnings import SetuptoolsDeprecationWarning @@ -65,10 +65,10 @@ def _read_utf8_with_fallback(file: str, fallback_encoding=py39.LOCALE_ENCODING) def _cfg_read_utf8_with_fallback( - cfg: ConfigParser, file: str, fallback_encoding=py39.LOCALE_ENCODING + cfg: RawConfigParser, file: str, fallback_encoding=py39.LOCALE_ENCODING ) -> None: """Same idea as :func:`_read_utf8_with_fallback`, but for the - :meth:`ConfigParser.read` method. + :meth:`RawConfigParser.read` method. This method may call ``cfg.clear()``. """