From 9a4c8d484ad0010b5a0d9a97e645a50b4d657d13 Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 19 Nov 2024 19:27:36 -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 | 23 +++++++++-------------- setuptools/command/easy_install.py | 4 ++-- setuptools/command/editable_wheel.py | 4 ++-- setuptools/command/egg_info.py | 5 +++-- setuptools/command/install_egg_info.py | 4 ++-- setuptools/command/saveopts.py | 4 ++-- setuptools/command/sdist.py | 4 ++-- setuptools/command/setopt.py | 2 +- setuptools/dist.py | 5 +++-- setuptools/monkey.py | 6 +++--- setuptools/msvc.py | 8 ++++---- setuptools/unicode_utils.py | 6 +++--- 16 files changed, 42 insertions(+), 45 deletions(-) diff --git a/setuptools/build_meta.py b/setuptools/build_meta.py index 23471accb6..00fa5e1f70 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 04d7e945bc..ac3e6ef1f9 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 976a322b14..234df2a7c7 100644 --- a/setuptools/command/bdist_wheel.py +++ b/setuptools/command/bdist_wheel.py @@ -218,7 +218,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 f098246b9b..e5c6b76b38 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 f8c9b11676..e7d60c6440 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, @@ -89,12 +88,6 @@ def __getattr__(self, attr: str): return self.data_files 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 - def _get_data_files(self): """Generate list of '(package,src_dir,build_dir,filenames)' tuples""" self.analyze_manifest() @@ -178,17 +171,17 @@ def build_package_data(self) -> None: _outf, _copied = self.copy_file(srcfile, target) make_writable(target) - def analyze_manifest(self): - self.manifest_files = mf = {} + def analyze_manifest(self) -> None: + 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,9 +210,11 @@ 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]: + def _filter_build_files( + self, files: Iterable[str], egg_info: StrPath + ) -> Iterator[str]: """ ``build_meta`` may try to create egg_info outside of the project directory, and this can be problematic for certain plugins (reported in issue #3500). diff --git a/setuptools/command/easy_install.py b/setuptools/command/easy_install.py index b40610f8ba..66fe68f7a9 100644 --- a/setuptools/command/easy_install.py +++ b/setuptools/command/easy_install.py @@ -238,7 +238,7 @@ def _render_version(): print(f'setuptools {dist.version} from {dist.location} (Python {ver})') raise SystemExit - def finalize_options(self): # noqa: C901 # is too complex (25) # FIXME + def finalize_options(self) -> None: # noqa: C901 # is too complex (25) # FIXME self.version and self._render_version() py_version = sys.version.split()[0] @@ -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 db9a50c3af..6d23d11fad 100644 --- a/setuptools/command/editable_wheel.py +++ b/setuptools/command/editable_wheel.py @@ -779,12 +779,12 @@ def _empty_dir(dir_: _P) -> _P: class _NamespaceInstaller(namespaces.Installer): - def __init__(self, distribution, installation_dir, editable_name, src_root): + def __init__(self, distribution, installation_dir, editable_name, src_root) -> None: self.distribution = distribution 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 1411ac3d89..a300356d33 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 @@ -330,7 +331,7 @@ def __init__( super().__init__(warn, debug_print) self.ignore_egg_info_dir = ignore_egg_info_dir - def process_template_line(self, line): + def process_template_line(self, line) -> None: # Parse the line: split it up, make sure the right number of words # is there, and return the relevant words. 'action' is always # defined: it's the first word of the line. Which of the other @@ -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 be4dd7b229..a6e6ec6446 100644 --- a/setuptools/command/install_egg_info.py +++ b/setuptools/command/install_egg_info.py @@ -20,13 +20,13 @@ class install_egg_info(namespaces.Installer, Command): def initialize_options(self): self.install_dir = None - def finalize_options(self): + def finalize_options(self) -> None: self.set_undefined_options('install_lib', ('install_dir', 'install_dir')) ei_cmd = self.get_finalized_command("egg_info") 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 f175de1015..2a2cbce6e2 100644 --- a/setuptools/command/saveopts.py +++ b/setuptools/command/saveopts.py @@ -6,9 +6,9 @@ class saveopts(option_base): description = "save supplied options to setup.cfg or other config file" - def run(self): + def run(self) -> None: 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 be69b33500..64e866c96b 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 75393f32f0..200cdff0f7 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 6062c4f868..5b3175fb5b 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -904,7 +904,7 @@ def _parse_command_opts(self, parser, args): return nargs - def get_cmdline_options(self): + def get_cmdline_options(self) -> dict[str, dict[str, str | None]]: """Return a '{cmd: {opt:val}}' map of all command-line options Option names are all long, but do not include the leading '--', and @@ -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 07919722b8..d8e30dbb80 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 6492d3be9d..94c64871a6 100644 --- a/setuptools/msvc.py +++ b/setuptools/msvc.py @@ -426,7 +426,7 @@ def find_reg_vs_vers(self): vs_vers.append(ver) return sorted(vs_vers) - def find_programdata_vs_vers(self): + def find_programdata_vs_vers(self) -> dict[float, str]: r""" Find Visual studio 2017+ versions from information in "C:\ProgramData\Microsoft\VisualStudio\Packages\_Instances". @@ -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: @@ -607,7 +607,7 @@ def WindowsSdkLastVersion(self): return self._use_last_dir_name(os.path.join(self.WindowsSdkDir, 'lib')) @property - def WindowsSdkDir(self): # noqa: C901 # is too complex (12) # FIXME + def WindowsSdkDir(self) -> str | None: # noqa: C901 # is too complex (12) # FIXME """ Microsoft Windows SDK directory. @@ -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 862d79e898..a6e33f2e0d 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()``. """