From 62562c1a2d9ec290faae51a93124c7798a80cf30 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 25 Feb 2023 15:05:13 -0500 Subject: [PATCH 1/4] Support next types-setuptools update --- mypyc/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypyc/build.py b/mypyc/build.py index cc03eba95b4e..17b0e5b234db 100644 --- a/mypyc/build.py +++ b/mypyc/build.py @@ -41,7 +41,7 @@ from mypyc.options import CompilerOptions if TYPE_CHECKING: - from distutils.core import Extension + from setuptools import Extension try: # Import setuptools so that it monkey-patch overrides distutils From d7102e0ef1babd910904bafa9f0bbfaca6de627c Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 25 Feb 2023 15:23:27 -0500 Subject: [PATCH 2/4] Either Extension can still be returned --- mypyc/build.py | 10 ++++++++-- setup.py | 20 ++++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/mypyc/build.py b/mypyc/build.py index 17b0e5b234db..f90e82abc7fc 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 setuptools 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 516a639f3bb2..b45bcd2c4dfb 100644 --- a/setup.py +++ b/setup.py @@ -6,6 +6,7 @@ import os import os.path import sys +from typing import cast if sys.version_info < (3, 7, 0): sys.stderr.write("ERROR: You need Python 3.7 or later to use mypy.\n") @@ -17,7 +18,7 @@ # 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 @@ -158,13 +159,16 @@ def run(self): opt_level = os.getenv("MYPYC_OPT_LEVEL", "3") debug_level = os.getenv("MYPYC_DEBUG_LEVEL", "1") force_multifile = os.getenv("MYPYC_MULTI_FILE", "") == "1" - ext_modules = mypycify( - mypyc_targets + ["--config-file=mypy_bootstrap.ini"], - opt_level=opt_level, - debug_level=debug_level, - # Use multi-file compilation mode on windows because without it - # our Appveyor builds run out of memory sometimes. - multi_file=sys.platform == "win32" or force_multifile, + ext_modules = cast( + Extension, + mypycify( + mypyc_targets + ["--config-file=mypy_bootstrap.ini"], + opt_level=opt_level, + debug_level=debug_level, + # Use multi-file compilation mode on windows because without it + # our Appveyor builds run out of memory sometimes. + multi_file=sys.platform == "win32" or force_multifile, + ), ) else: ext_modules = [] From d9ffbea75f82b89a3ec0b97665b428aadfb7d95d Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 25 Feb 2023 16:55:09 -0500 Subject: [PATCH 3/4] assert ext_modules is list of setuptools.Extension --- setup.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/setup.py b/setup.py index b45bcd2c4dfb..40de4c4dd76f 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ import os import os.path import sys -from typing import cast +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") @@ -23,6 +23,9 @@ 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 @@ -37,6 +40,10 @@ """.lstrip() +def is_list_of_setuptools_extension(items: list[Any]) -> TypeGuard[list[Extension]]: + return all(item is Extension for item in items) + + def find_package_data(base, globs, root="mypy"): """Find all interesting data files, for setup(package_data=) @@ -159,17 +166,16 @@ def run(self): opt_level = os.getenv("MYPYC_OPT_LEVEL", "3") debug_level = os.getenv("MYPYC_DEBUG_LEVEL", "1") force_multifile = os.getenv("MYPYC_MULTI_FILE", "") == "1" - ext_modules = cast( - Extension, - mypycify( - mypyc_targets + ["--config-file=mypy_bootstrap.ini"], - opt_level=opt_level, - debug_level=debug_level, - # Use multi-file compilation mode on windows because without it - # our Appveyor builds run out of memory sometimes. - multi_file=sys.platform == "win32" or force_multifile, - ), + ext_modules = mypycify( + mypyc_targets + ["--config-file=mypy_bootstrap.ini"], + opt_level=opt_level, + debug_level=debug_level, + # Use multi-file compilation mode on windows because without it + # 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 a39c68eca31870ef37f19ba3452d91a6a3dd55de Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 25 Feb 2023 17:33:05 -0500 Subject: [PATCH 4/4] Update setup.py Co-authored-by: Jelle Zijlstra --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 40de4c4dd76f..5d5ea06fb714 100644 --- a/setup.py +++ b/setup.py @@ -41,7 +41,7 @@ def is_list_of_setuptools_extension(items: list[Any]) -> TypeGuard[list[Extension]]: - return all(item is Extension for item in items) + return all(isinstance(item, Extension) for item in items) def find_package_data(base, globs, root="mypy"):