From 31803441590c8721fcc05ab153c02939d203fd36 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sun, 26 Feb 2023 09:39:01 -0500 Subject: [PATCH 1/2] Support next types-setuptools update (#14781) A simple type-only change since mypyc explicitely uses setuptools' monkeypatching. And `setup.py` imports from setuptools. Tests should stay the same. This should fix a sudden failure of tests once https://github.com/python/typeshed/pull/9795 is merged. --- mypyc/build.py | 10 ++++++++-- setup.py | 12 +++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/mypyc/build.py b/mypyc/build.py index cc03eba95b4ee..f90e82abc7fcb 100644 --- a/mypyc/build.py +++ b/mypyc/build.py @@ -25,7 +25,7 @@ import re import sys import time -from typing import TYPE_CHECKING, Any, Dict, Iterable, NoReturn, cast +from typing import TYPE_CHECKING, Any, Dict, Iterable, NoReturn, Union, cast from mypy.build import BuildSource from mypy.errors import CompileError @@ -41,7 +41,13 @@ from mypyc.options import CompilerOptions if TYPE_CHECKING: - from distutils.core import Extension + from distutils.core import Extension as _distutils_Extension + from typing_extensions import TypeAlias + + from setuptools import Extension as _setuptools_Extension + + Extension: TypeAlias = Union[_setuptools_Extension, _distutils_Extension] + try: # Import setuptools so that it monkey-patch overrides distutils diff --git a/setup.py b/setup.py index 516a639f3bb2b..5d5ea06fb7148 100644 --- a/setup.py +++ b/setup.py @@ -6,6 +6,7 @@ import os import os.path import sys +from typing import TYPE_CHECKING, Any if sys.version_info < (3, 7, 0): sys.stderr.write("ERROR: You need Python 3.7 or later to use mypy.\n") @@ -17,11 +18,14 @@ # This requires setuptools when building; setuptools is not needed # when installing from a wheel file (though it is still needed for # alternative forms of installing, as suggested by README.md). -from setuptools import find_packages, setup +from setuptools import Extension, find_packages, setup from setuptools.command.build_py import build_py from mypy.version import __version__ as version +if TYPE_CHECKING: + from typing_extensions import TypeGuard + description = "Optional static typing for Python" long_description = """ Mypy -- Optional Static Typing for Python @@ -36,6 +40,10 @@ """.lstrip() +def is_list_of_setuptools_extension(items: list[Any]) -> TypeGuard[list[Extension]]: + return all(isinstance(item, Extension) for item in items) + + def find_package_data(base, globs, root="mypy"): """Find all interesting data files, for setup(package_data=) @@ -166,6 +174,8 @@ def run(self): # our Appveyor builds run out of memory sometimes. multi_file=sys.platform == "win32" or force_multifile, ) + assert is_list_of_setuptools_extension(ext_modules), "Expected mypycify to use setuptools" + else: ext_modules = [] From 441610e88811029c42f365083c59922502409b13 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 28 Feb 2023 07:02:25 +0000 Subject: [PATCH 2/2] Unpin the `types-setuptools` build dependency (#14787) - The latest release of `types-setuptools` started causing type-check errors (and, therefore, mypyc build errors) on the `master` branch: see, e.g. https://github.com/python/mypy/actions/runs/4275505309/jobs/7442902732. - #14781 addressed some of the new errors, but didn't quite fix the build. - #14788 then pinned the `types-setuptools` dependency as a temporary stopgap measure. - This PR now attempts to unpin `types-setuptools` and fix the build errors in a more principled way. --- mypyc/build.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/mypyc/build.py b/mypyc/build.py index f90e82abc7fcb..8e1ee8078c11f 100644 --- a/mypyc/build.py +++ b/mypyc/build.py @@ -51,7 +51,7 @@ try: # Import setuptools so that it monkey-patch overrides distutils - import setuptools # noqa: F401 + import setuptools except ImportError: if sys.version_info >= (3, 12): # Raise on Python 3.12, since distutils will go away forever @@ -63,13 +63,16 @@ def get_extension() -> type[Extension]: # We can work with either setuptools or distutils, and pick setuptools # if it has been imported. use_setuptools = "setuptools" in sys.modules + extension_class: type[Extension] if not use_setuptools: - from distutils.core import Extension + import distutils.core + + extension_class = distutils.core.Extension else: - from setuptools import Extension + extension_class = setuptools.Extension - return Extension + return extension_class def setup_mypycify_vars() -> None: