Skip to content

Commit b4e1b18

Browse files
committed
Find Python 2 variants of stub packages
This is currently needed for six.
1 parent 55bdd07 commit b4e1b18

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

mypy/modulefinder.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def __init__(self,
120120
self.options = options
121121
self.ns_packages = ns_packages or [] # type: List[str]
122122
self.stdlib_py_versions = load_stdlib_py_versions()
123+
self.python2 = options and options.python_version[0] == 2
123124

124125
def clear(self) -> None:
125126
self.results.clear()
@@ -230,6 +231,12 @@ def _find_module(self, id: str, use_typeshed: bool) -> ModuleSearchResult:
230231
for pkg_dir in self.search_paths.package_path:
231232
stub_name = components[0] + '-stubs'
232233
stub_dir = os.path.join(pkg_dir, stub_name)
234+
if self.python2:
235+
alt_stub_name = components[0] + '-python2-stubs'
236+
alt_stub_dir = os.path.join(pkg_dir, alt_stub_name)
237+
if fscache.isdir(alt_stub_dir):
238+
stub_name = alt_stub_name
239+
stub_dir = alt_stub_dir
233240
if fscache.isdir(stub_dir) and self._is_compatible_stub_package(stub_dir):
234241
stub_typed_file = os.path.join(stub_dir, 'py.typed')
235242
stub_components = [stub_name] + components[1:]
@@ -291,7 +298,11 @@ def _find_module(self, id: str, use_typeshed: bool) -> ModuleSearchResult:
291298
# Prefer package over module, i.e. baz/__init__.py* over baz.py*.
292299
for extension in PYTHON_EXTENSIONS:
293300
path = base_path + sepinit + extension
294-
path_stubs = base_path + '-stubs' + sepinit + extension
301+
suffix = '-stubs'
302+
if self.python2:
303+
if os.path.isdir(base_path + '-python2-stubs'):
304+
suffix = '-python2-stubs'
305+
path_stubs = base_path + suffix + sepinit + extension
295306
if fscache.isfile_case(path, dir_prefix):
296307
has_init = True
297308
if verify and not verify_module(fscache, id, path, dir_prefix):
@@ -367,12 +378,12 @@ def _is_compatible_stub_package(self, stub_dir: str) -> bool:
367378
whether the stubs are compatible with Python 2 and 3.
368379
"""
369380
metadata_fnam = os.path.join(stub_dir, 'METADATA.toml')
370-
if os.path.isfile(metadata_fnam) and self.options:
381+
if os.path.isfile(metadata_fnam):
371382
# Delay import for a possible minor performance win.
372383
import toml
373384
with open(metadata_fnam, 'r') as f:
374385
metadata = toml.load(f)
375-
if self.options.python_version[0] == 2:
386+
if self.python2:
376387
return bool(metadata.get('python2', False))
377388
else:
378389
return bool(metadata.get('python3', True))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy