Skip to content

Commit

Permalink
Runtime typing fixes for typeshed return type merge (#4753
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri authored Nov 20, 2024
2 parents e622859 + 9a4c8d4 commit 2c77cd2
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 45 deletions.
4 changes: 2 additions & 2 deletions setuptools/build_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/bdist_egg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/bdist_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions setuptools/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
23 changes: 9 additions & 14 deletions setuptools/command/build_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ 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)
self.package_data = self.distribution.package_data
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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions setuptools/command/easy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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):
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 @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions setuptools/command/egg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import re
import sys
import time
from collections.abc import Callable

import packaging
import packaging.requirements
Expand Down Expand Up @@ -330,15 +331,15 @@ 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
# three are defined depends on the action; it'll be either
# 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,
Expand Down
4 changes: 2 additions & 2 deletions setuptools/command/install_egg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions setuptools/command/saveopts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
4 changes: 2 additions & 2 deletions setuptools/command/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/setopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
5 changes: 3 additions & 2 deletions setuptools/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions setuptools/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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'
Expand Down
8 changes: 4 additions & 4 deletions setuptools/msvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand All @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions setuptools/unicode_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
import unicodedata
from configparser import ConfigParser
from configparser import RawConfigParser

from .compat import py39
from .warnings import SetuptoolsDeprecationWarning
Expand Down Expand Up @@ -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()``.
"""
Expand Down

0 comments on commit 2c77cd2

Please sign in to comment.