Skip to content

Commit 6d355f5

Browse files
AlexWaygoodAvasam
andauthored
[Release 1.1] Cherry-pick some mypyc build fixes (#14820)
The mypyc build was recently broken by a new release of `types-setuptools`. This was fixed on `master` by the following two PRs: - #14781 - #14787 However, the mypyc build is still broken on the 1.1 branch: https://github.com/python/mypy/actions/runs/4311688115/jobs/7521345529. This PR cherry-picks the two PRs that fixed the build to the 1.1 branch. --------- Co-authored-by: Avasam <[email protected]>
1 parent a27dec5 commit 6d355f5

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

Diff for: mypyc/build.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import re
2626
import sys
2727
import time
28-
from typing import TYPE_CHECKING, Any, Dict, Iterable, NoReturn, cast
28+
from typing import TYPE_CHECKING, Any, Dict, Iterable, NoReturn, Union, cast
2929

3030
from mypy.build import BuildSource
3131
from mypy.errors import CompileError
@@ -41,11 +41,17 @@
4141
from mypyc.options import CompilerOptions
4242

4343
if TYPE_CHECKING:
44-
from distutils.core import Extension
44+
from distutils.core import Extension as _distutils_Extension
45+
from typing_extensions import TypeAlias
46+
47+
from setuptools import Extension as _setuptools_Extension
48+
49+
Extension: TypeAlias = Union[_setuptools_Extension, _distutils_Extension]
50+
4551

4652
try:
4753
# Import setuptools so that it monkey-patch overrides distutils
48-
import setuptools # noqa: F401
54+
import setuptools
4955
except ImportError:
5056
if sys.version_info >= (3, 12):
5157
# Raise on Python 3.12, since distutils will go away forever
@@ -57,13 +63,16 @@ def get_extension() -> type[Extension]:
5763
# We can work with either setuptools or distutils, and pick setuptools
5864
# if it has been imported.
5965
use_setuptools = "setuptools" in sys.modules
66+
extension_class: type[Extension]
6067

6168
if not use_setuptools:
62-
from distutils.core import Extension
69+
import distutils.core
70+
71+
extension_class = distutils.core.Extension
6372
else:
64-
from setuptools import Extension
73+
extension_class = setuptools.Extension
6574

66-
return Extension
75+
return extension_class
6776

6877

6978
def setup_mypycify_vars() -> None:

Diff for: setup.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import os.path
88
import sys
9+
from typing import TYPE_CHECKING, Any
910

1011
if sys.version_info < (3, 7, 0):
1112
sys.stderr.write("ERROR: You need Python 3.7 or later to use mypy.\n")
@@ -17,11 +18,14 @@
1718
# This requires setuptools when building; setuptools is not needed
1819
# when installing from a wheel file (though it is still needed for
1920
# alternative forms of installing, as suggested by README.md).
20-
from setuptools import find_packages, setup
21+
from setuptools import Extension, find_packages, setup
2122
from setuptools.command.build_py import build_py
2223

2324
from mypy.version import __version__ as version
2425

26+
if TYPE_CHECKING:
27+
from typing_extensions import TypeGuard
28+
2529
description = "Optional static typing for Python"
2630
long_description = """
2731
Mypy -- Optional Static Typing for Python
@@ -36,6 +40,10 @@
3640
""".lstrip()
3741

3842

43+
def is_list_of_setuptools_extension(items: list[Any]) -> TypeGuard[list[Extension]]:
44+
return all(isinstance(item, Extension) for item in items)
45+
46+
3947
def find_package_data(base, globs, root="mypy"):
4048
"""Find all interesting data files, for setup(package_data=)
4149
@@ -166,6 +174,8 @@ def run(self):
166174
# our Appveyor builds run out of memory sometimes.
167175
multi_file=sys.platform == "win32" or force_multifile,
168176
)
177+
assert is_list_of_setuptools_extension(ext_modules), "Expected mypycify to use setuptools"
178+
169179
else:
170180
ext_modules = []
171181

0 commit comments

Comments
 (0)