26
26
import re
27
27
28
28
from typing import List , Tuple , Any , Optional , Dict , Union , Set , cast
29
- MYPY = False
30
- if MYPY :
31
- from typing import NoReturn
29
+ from typing_extensions import TYPE_CHECKING , NoReturn , Type
32
30
33
31
from mypy .main import process_options
34
32
from mypy .errors import CompileError
42
40
43
41
from mypyc import emitmodule
44
42
43
+ if TYPE_CHECKING :
44
+ from distutils .core import Extension # noqa
45
45
46
- # We can work with either setuptools or distutils, and pick setuptools
47
- # if it has been imported.
48
- assert 'setuptools' in sys .modules or 'distutils' in sys .modules , (
49
- "'setuptools' or 'distutils' must be imported before mypyc.build" )
50
- USE_SETUPTOOLS = 'setuptools' in sys .modules
46
+ from distutils import sysconfig , ccompiler
51
47
52
- if not USE_SETUPTOOLS :
53
- from distutils .core import setup , Extension
54
- from distutils .command .build_ext import build_ext # type: ignore
55
- else :
56
- from setuptools import setup , Extension # type: ignore # noqa
57
- from setuptools .command .build_ext import build_ext # type: ignore
58
48
59
- from distutils import sysconfig , ccompiler
49
+ def get_extension () -> Type ['Extension' ]:
50
+ # We can work with either setuptools or distutils, and pick setuptools
51
+ # if it has been imported.
52
+ use_setuptools = 'setuptools' in sys .modules
53
+
54
+ if not use_setuptools :
55
+ from distutils .core import Extension
56
+ else :
57
+ from setuptools import Extension # type: ignore # noqa
58
+
59
+ return Extension
60
60
61
61
62
62
def setup_mypycify_vars () -> None :
@@ -73,7 +73,7 @@ def setup_mypycify_vars() -> None:
73
73
vars ['CFLAGS' ] = vars ['CFLAGS' ].replace ('-arch i386' , '' )
74
74
75
75
76
- def fail (message : str ) -> ' NoReturn' :
76
+ def fail (message : str ) -> NoReturn :
77
77
# TODO: Is there something else we should do to fail?
78
78
sys .exit (message )
79
79
@@ -221,7 +221,7 @@ def build_using_shared_lib(sources: List[BuildSource],
221
221
deps : List [str ],
222
222
build_dir : str ,
223
223
extra_compile_args : List [str ],
224
- ) -> List [Extension ]:
224
+ ) -> List [' Extension' ]:
225
225
"""Produce the list of extension modules when a shared library is needed.
226
226
227
227
This creates one shared library extension module that all of the
@@ -233,7 +233,7 @@ def build_using_shared_lib(sources: List[BuildSource],
233
233
extension module that exports the real initialization functions in
234
234
Capsules stored in module attributes.
235
235
"""
236
- extensions = [Extension (
236
+ extensions = [get_extension () (
237
237
shared_lib_name (group_name ),
238
238
sources = cfiles ,
239
239
include_dirs = [include_dir ()],
@@ -251,7 +251,7 @@ def build_using_shared_lib(sources: List[BuildSource],
251
251
assert source .path
252
252
if os .path .split (source .path )[1 ] == '__init__.py' :
253
253
full_module_name += '.__init__'
254
- extensions .append (Extension (
254
+ extensions .append (get_extension () (
255
255
full_module_name ,
256
256
sources = [shim_file ],
257
257
extra_compile_args = extra_compile_args ,
@@ -263,12 +263,12 @@ def build_using_shared_lib(sources: List[BuildSource],
263
263
def build_single_module (sources : List [BuildSource ],
264
264
cfiles : List [str ],
265
265
extra_compile_args : List [str ],
266
- ) -> List [Extension ]:
266
+ ) -> List [' Extension' ]:
267
267
"""Produce the list of extension modules for a standalone extension.
268
268
269
269
This contains just one module, since there is no need for a shared module.
270
270
"""
271
- return [Extension (
271
+ return [get_extension () (
272
272
sources [0 ].module ,
273
273
sources = cfiles ,
274
274
include_dirs = [include_dir ()],
@@ -367,7 +367,7 @@ def mypycify(
367
367
multi_file : bool = False ,
368
368
separate : Union [bool , List [Tuple [List [str ], Optional [str ]]]] = False ,
369
369
skip_cgen_input : Optional [Any ] = None
370
- ) -> List [Extension ]:
370
+ ) -> List [' Extension' ]:
371
371
"""Main entry point to building using mypyc.
372
372
373
373
This produces a list of Extension objects that should be passed as the
@@ -500,9 +500,3 @@ def mypycify(
500
500
group_sources , cfilenames + shared_cfilenames , cflags ))
501
501
502
502
return extensions
503
-
504
-
505
- # For backwards compatibility we define this as an alias. Previous
506
- # versions used to require using it, and it is conceivable that future
507
- # versions will need it also.
508
- MypycifyBuildExt = build_ext
0 commit comments