@@ -120,6 +120,7 @@ def __init__(self,
120
120
self .options = options
121
121
self .ns_packages = ns_packages or [] # type: List[str]
122
122
self .stdlib_py_versions = load_stdlib_py_versions ()
123
+ self .python2 = options and options .python_version [0 ] == 2
123
124
124
125
def clear (self ) -> None :
125
126
self .results .clear ()
@@ -230,6 +231,12 @@ def _find_module(self, id: str, use_typeshed: bool) -> ModuleSearchResult:
230
231
for pkg_dir in self .search_paths .package_path :
231
232
stub_name = components [0 ] + '-stubs'
232
233
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
233
240
if fscache .isdir (stub_dir ) and self ._is_compatible_stub_package (stub_dir ):
234
241
stub_typed_file = os .path .join (stub_dir , 'py.typed' )
235
242
stub_components = [stub_name ] + components [1 :]
@@ -291,7 +298,11 @@ def _find_module(self, id: str, use_typeshed: bool) -> ModuleSearchResult:
291
298
# Prefer package over module, i.e. baz/__init__.py* over baz.py*.
292
299
for extension in PYTHON_EXTENSIONS :
293
300
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
295
306
if fscache .isfile_case (path , dir_prefix ):
296
307
has_init = True
297
308
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:
367
378
whether the stubs are compatible with Python 2 and 3.
368
379
"""
369
380
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 ):
371
382
# Delay import for a possible minor performance win.
372
383
import toml
373
384
with open (metadata_fnam , 'r' ) as f :
374
385
metadata = toml .load (f )
375
- if self .options . python_version [ 0 ] == 2 :
386
+ if self .python2 :
376
387
return bool (metadata .get ('python2' , False ))
377
388
else :
378
389
return bool (metadata .get ('python3' , True ))
0 commit comments