Skip to content

Commit 5e8815e

Browse files
emmatypinggvanrossum
authored andcommitted
Use package_data to ship typeshed and xml files (#5517)
Data files have caused many issues where mypy cannot find typeshed. Using package_data allows us to find typeshed relative to the mypy package itself, which avoids a lot of fragile path computation. Fixes #5307 Fixes #5319
1 parent 2b15be3 commit 5e8815e

File tree

11 files changed

+19
-79
lines changed

11 files changed

+19
-79
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "typeshed"]
2-
path = typeshed
2+
path = mypy/typeshed
33
url = https://github.com/python/typeshed

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ recursive-include scripts *
22
recursive-include test-data *
33
recursive-include extensions *
44
recursive-include docs *
5+
recursive-include mypy/typeshed *.py *.pyi
6+
recursive-include mypy/xml *.xsd *.xslt *.css
57
include mypy_self_check.ini
68
include LICENSE

appveyor.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ environment:
99
PYTHON_ARCH: "64"
1010

1111
install:
12-
- "git config core.symlinks true"
13-
- "git reset --hard"
14-
- "git submodule update --init typeshed"
15-
- "cd typeshed && git config core.symlinks true && git reset --hard && cd .."
12+
- "git submodule update --init mypy/typeshed"
1613
- "%PYTHON%\\python.exe -m pip install -U setuptools tox"
1714
- "%PYTHON%\\python.exe -m tox -e py37 --notest"
1815

mypy/build.py

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import hashlib
2121
import json
2222
import os.path
23+
import pathlib
2324
import re
2425
import site
2526
import stat
@@ -326,7 +327,7 @@ def _build(sources: List[BuildSource],
326327
# This seems the most reasonable place to tune garbage collection.
327328
gc.set_threshold(50000)
328329

329-
data_dir = default_data_dir(bin_dir)
330+
data_dir = default_data_dir()
330331
fscache = fscache or FileSystemCache()
331332

332333
search_paths = compute_search_paths(sources, options, data_dir, fscache, alt_lib_path)
@@ -365,68 +366,9 @@ def _build(sources: List[BuildSource],
365366
reports.finish()
366367

367368

368-
def default_data_dir(bin_dir: Optional[str]) -> str:
369-
"""Returns directory containing typeshed directory
370-
371-
Args:
372-
bin_dir: directory containing the mypy script
373-
"""
374-
if not bin_dir:
375-
if os.name == 'nt':
376-
prefixes = [os.path.join(sys.prefix, 'Lib')]
377-
try:
378-
prefixes.append(os.path.join(site.getuserbase(), 'lib'))
379-
except AttributeError:
380-
# getuserbase in not available in virtualenvs
381-
prefixes.append(os.path.join(get_python_lib(), 'lib'))
382-
for parent in prefixes:
383-
data_dir = os.path.join(parent, 'mypy')
384-
if os.path.exists(data_dir):
385-
return data_dir
386-
mypy_package = os.path.dirname(__file__)
387-
parent = os.path.dirname(mypy_package)
388-
if (os.path.basename(parent) == 'site-packages' or
389-
os.path.basename(parent) == 'dist-packages'):
390-
# Installed in site-packages or dist-packages, but invoked with python3 -m mypy;
391-
# __file__ is .../blah/lib/python3.N/site-packages/mypy/build.py
392-
# or .../blah/lib/python3.N/dist-packages/mypy/build.py (Debian)
393-
# or .../blah/lib64/python3.N/dist-packages/mypy/build.py (Gentoo)
394-
# or .../blah/lib/site-packages/mypy/build.py (Windows)
395-
# blah may be a virtualenv or /usr/local. We want .../blah/lib/mypy.
396-
lib = parent
397-
for i in range(2):
398-
lib = os.path.dirname(lib)
399-
if os.path.basename(lib) in ('lib', 'lib32', 'lib64'):
400-
return os.path.join(os.path.dirname(lib), 'lib/mypy')
401-
subdir = os.path.join(parent, 'lib', 'mypy')
402-
if os.path.isdir(subdir):
403-
# If installed via buildout, the __file__ is
404-
# somewhere/mypy/__init__.py and what we want is
405-
# somewhere/lib/mypy.
406-
return subdir
407-
# Default to directory containing this file's parent.
408-
return parent
409-
base = os.path.basename(bin_dir)
410-
dir = os.path.dirname(bin_dir)
411-
if (sys.platform == 'win32' and base.lower() == 'scripts'
412-
and not os.path.isdir(os.path.join(dir, 'typeshed'))):
413-
# Installed, on Windows.
414-
return os.path.join(dir, 'Lib', 'mypy')
415-
elif base == 'scripts':
416-
# Assume that we have a repo check out or unpacked source tarball.
417-
return dir
418-
elif base == 'bin':
419-
# Installed to somewhere (can be under /usr/local or anywhere).
420-
return os.path.join(dir, 'lib', 'mypy')
421-
elif base == 'python3':
422-
# Assume we installed python3 with brew on os x
423-
return os.path.join(os.path.dirname(dir), 'lib', 'mypy')
424-
elif dir.endswith('python-exec'):
425-
# Gentoo uses a python wrapper in /usr/lib to which mypy is a symlink.
426-
return os.path.join(os.path.dirname(dir), 'mypy')
427-
else:
428-
# Don't know where to find the data files!
429-
raise RuntimeError("Broken installation: can't determine base dir")
369+
def default_data_dir() -> str:
370+
"""Returns directory containing typeshed directory."""
371+
return os.path.dirname(__file__)
430372

431373

432374
def mypy_path() -> List[str]:
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ exclude =
2222
# fixtures have their own .pyi-specific configuration
2323
test-data/*,
2424
# typeshed has its own .pyi-specific configuration
25-
typeshed/*,
25+
mypy/typeshed/*,
2626
# flake8 might be started when there's still examples in the temp dir
2727
tmp-test-dirs/*
2828
.tox

0 commit comments

Comments
 (0)