From 933405676b119b828af7548482752f5185f847ae Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 8 Aug 2024 12:03:45 -0400 Subject: [PATCH 1/2] Runtime changes for typeshed merge Co-authored-by: Anderson Bravalheri --- newsfragments/4505.feature.rst | 1 + setuptools/command/build_ext.py | 3 +-- setuptools/command/easy_install.py | 14 ++++++++++---- setuptools/command/editable_wheel.py | 10 ++++++++-- setuptools/dist.py | 2 +- setuptools/extension.py | 5 +++-- 6 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 newsfragments/4505.feature.rst diff --git a/newsfragments/4505.feature.rst b/newsfragments/4505.feature.rst new file mode 100644 index 0000000000..e032dd997e --- /dev/null +++ b/newsfragments/4505.feature.rst @@ -0,0 +1 @@ +Changed the order of type checks in ``setuptools.command.easy_install.CommandSpec.from_param`` to support any `collections.abc.Iterable` of `str` param -- by :user:`Avasam` diff --git a/setuptools/command/build_ext.py b/setuptools/command/build_ext.py index 508704f3c0..da8d56fac2 100644 --- a/setuptools/command/build_ext.py +++ b/setuptools/command/build_ext.py @@ -8,7 +8,6 @@ from typing import Iterator from pathlib import Path -from distutils.command.build_ext import build_ext as _du_build_ext from distutils.ccompiler import new_compiler from distutils.sysconfig import customize_compiler, get_config_var from distutils import log @@ -24,7 +23,7 @@ # also. Ref #1229. __import__('Cython.Compiler.Main') except ImportError: - _build_ext = _du_build_ext + from distutils.command.build_ext import build_ext as _build_ext # make sure _config_vars is initialized get_config_var("LDSHARED") diff --git a/setuptools/command/easy_install.py b/setuptools/command/easy_install.py index 36114d40ed..74b79d1adc 100644 --- a/setuptools/command/easy_install.py +++ b/setuptools/command/easy_install.py @@ -12,6 +12,7 @@ from __future__ import annotations +from collections.abc import Iterable from glob import glob from distutils.util import get_platform from distutils.util import convert_path, subst_vars @@ -26,6 +27,7 @@ from distutils.command import install import sys import os +from typing import TYPE_CHECKING import zipimport import shutil import tempfile @@ -78,6 +80,8 @@ from .._path import ensure_directory from jaraco.text import yield_lines +if TYPE_CHECKING: + from typing_extensions import Self # Turn on PEP440Warnings warnings.filterwarnings("default", category=pkg_resources.PEP440Warning) @@ -2055,19 +2059,21 @@ def _sys_executable(cls): return os.environ.get('__PYVENV_LAUNCHER__', _default) @classmethod - def from_param(cls, param): + def from_param(cls, param: Self | str | Iterable[str] | None) -> Self: """ Construct a CommandSpec from a parameter to build_scripts, which may be None. """ if isinstance(param, cls): return param - if isinstance(param, list): + if isinstance(param, str): + return cls.from_string(param) + if isinstance(param, Iterable): return cls(param) if param is None: return cls.from_environment() - # otherwise, assume it's a string. - return cls.from_string(param) + # AttributeError to keep backwards compatibility, this should really be a TypeError though + raise AttributeError(f"Argument has an unsupported type {type(param)}") @classmethod def from_environment(cls): diff --git a/setuptools/command/editable_wheel.py b/setuptools/command/editable_wheel.py index 49fd609b15..4bfa1d3e27 100644 --- a/setuptools/command/editable_wheel.py +++ b/setuptools/command/editable_wheel.py @@ -466,8 +466,14 @@ def _create_file(self, relative_output: str, src_file: str, link=None): def _create_links(self, outputs, output_mapping): self.auxiliary_dir.mkdir(parents=True, exist_ok=True) link_type = "sym" if _can_symlink_files(self.auxiliary_dir) else "hard" - mappings = {self._normalize_output(k): v for k, v in output_mapping.items()} - mappings.pop(None, None) # remove files that are not relative to build_lib + mappings = { + k: v + for k, v in ( + (self._normalize_output(k), v) for k, v in output_mapping.items() + ) + # remove files that are not relative to build_lib + if k is not None + } for output in outputs: relative = self._normalize_output(output) diff --git a/setuptools/dist.py b/setuptools/dist.py index 6a29b2b2b7..6c73ae792f 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -55,7 +55,7 @@ def assert_string_list(dist, attr, value): try: # verify that value is a list or tuple to exclude unordered # or single-use iterables - assert isinstance(value, (list, tuple)) + assert isinstance(value, sequence) # verify that elements of value are strings assert ''.join(value) != value except (TypeError, ValueError, AttributeError, AssertionError) as e: diff --git a/setuptools/extension.py b/setuptools/extension.py index 25420f42de..8a8a9206fe 100644 --- a/setuptools/extension.py +++ b/setuptools/extension.py @@ -1,3 +1,4 @@ +from __future__ import annotations import re import functools import distutils.core @@ -126,10 +127,10 @@ class Extension(_Extension): specified on Windows. (since v63) """ - def __init__(self, name, sources, *args, **kw): + def __init__(self, name: str, sources, *args, py_limited_api: bool = False, **kw): # The *args is needed for compatibility as calls may use positional # arguments. py_limited_api may be set only via keyword. - self.py_limited_api = kw.pop("py_limited_api", False) + self.py_limited_api = py_limited_api super().__init__(name, sources, *args, **kw) def _convert_pyx_sources_to_lang(self): From 90f87d219aea87d13b24e7ddd609adb687307051 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Thu, 8 Aug 2024 18:08:00 +0100 Subject: [PATCH 2/2] Update setuptools/command/editable_wheel.py --- setuptools/command/editable_wheel.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/setuptools/command/editable_wheel.py b/setuptools/command/editable_wheel.py index 4bfa1d3e27..5ad8ecdd6a 100644 --- a/setuptools/command/editable_wheel.py +++ b/setuptools/command/editable_wheel.py @@ -466,14 +466,9 @@ def _create_file(self, relative_output: str, src_file: str, link=None): def _create_links(self, outputs, output_mapping): self.auxiliary_dir.mkdir(parents=True, exist_ok=True) link_type = "sym" if _can_symlink_files(self.auxiliary_dir) else "hard" - mappings = { - k: v - for k, v in ( - (self._normalize_output(k), v) for k, v in output_mapping.items() - ) - # remove files that are not relative to build_lib - if k is not None - } + normalised = ((self._normalize_output(k), v) for k, v in output_mapping.items()) + # remove files that are not relative to build_lib + mappings = {k: v for k, v in normalised if k is not None} for output in outputs: relative = self._normalize_output(output)