Skip to content

Commit 9dea7c7

Browse files
authored
Add support for building with mypyc to setup.py (#5627)
The mypyc build defaults to off and can be enabled either by passing `--use-mypyc` or setting `MYPY_USE_MYPYC=1` as an environment variable.
1 parent 5751f42 commit 9dea7c7

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

setup.py

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,66 @@ def run(self):
6565
build_py.run(self)
6666

6767

68+
cmdclass = {'build_py': CustomPythonBuild}
69+
6870
package_data = ['py.typed']
6971

7072
package_data += find_package_data(os.path.join('mypy', 'typeshed'), ['*.py', '*.pyi'])
7173

7274
package_data += find_package_data(os.path.join('mypy', 'xml'), ['*.xsd', '*.xslt', '*.css'])
7375

76+
USE_MYPYC = False
77+
# To compile with mypyc, a mypyc checkout must be present on the PYTHONPATH
78+
if len(sys.argv) > 1 and sys.argv[1] == '--use-mypyc':
79+
sys.argv.pop(1)
80+
USE_MYPYC = True
81+
if os.getenv('MYPY_USE_MYPYC', None) == '1':
82+
USE_MYPYC = True
83+
84+
if USE_MYPYC:
85+
MYPYC_BLACKLIST = (
86+
'mypyc_hacks.py',
87+
'interpreted_plugin.py',
88+
89+
'__main__.py',
90+
'bogus_type.py',
91+
'dmypy.py',
92+
'gclogger.py',
93+
'main.py',
94+
'memprofile.py',
95+
'version.py',
96+
)
97+
98+
everything = find_package_data('mypy', ['*.py'])
99+
# Start with all the .py files
100+
all_real_pys = [x for x in everything if not x.startswith('typeshed/')]
101+
# Strip out anything in our blacklist
102+
mypyc_targets = [x for x in all_real_pys if x not in MYPYC_BLACKLIST]
103+
# Strip out any test code
104+
mypyc_targets = [x for x in mypyc_targets if not x.startswith('test/')]
105+
# ... and add back in the one test module we need
106+
mypyc_targets.append('test/visitors.py')
107+
108+
# Fix the paths to be full
109+
mypyc_targets = [os.path.join('mypy', x) for x in mypyc_targets]
110+
111+
# This bit is super unfortunate: we want to use the mypy packaged
112+
# with mypyc. It will arrange for the path to be setup so it can
113+
# find it, but we've already imported parts, so we remove the
114+
# modules that we've imported already, which will let the right
115+
# versions be imported by mypyc.
116+
del sys.modules['mypy']
117+
del sys.modules['mypy.version']
118+
del sys.modules['mypy.git']
119+
120+
from mypyc.build import mypycify, MypycifyBuildExt
121+
opt_level = os.getenv('MYPYC_OPT_LEVEL', '3')
122+
ext_modules = mypycify(mypyc_targets, ['--config-file=mypy_bootstrap.ini'], opt_level)
123+
cmdclass['build_ext'] = MypycifyBuildExt
124+
description += " (mypyc-compiled version)"
125+
else:
126+
ext_modules = []
127+
74128

75129
classifiers = [
76130
'Development Status :: 3 - Alpha',
@@ -84,7 +138,7 @@ def run(self):
84138
'Topic :: Software Development',
85139
]
86140

87-
setup(name='mypy',
141+
setup(name='mypy' if not USE_MYPYC else 'mypy-mypyc',
88142
version=version,
89143
description=description,
90144
long_description=long_description,
@@ -93,14 +147,15 @@ def run(self):
93147
url='http://www.mypy-lang.org/',
94148
license='MIT License',
95149
py_modules=[],
150+
ext_modules=ext_modules,
96151
packages=['mypy', 'mypy.test', 'mypy.server', 'mypy.plugins'],
97152
package_data={'mypy': package_data},
98153
entry_points={'console_scripts': ['mypy=mypy.__main__:console_entry',
99154
'stubgen=mypy.stubgen:main',
100155
'dmypy=mypy.dmypy:main',
101156
]},
102157
classifiers=classifiers,
103-
cmdclass={'build_py': CustomPythonBuild},
158+
cmdclass=cmdclass,
104159
install_requires = ['typed-ast >= 1.1.0, < 1.2.0',
105160
'mypy_extensions >= 0.4.0, < 0.5.0',
106161
],

0 commit comments

Comments
 (0)