Skip to content

Commit da465ed

Browse files
authored
Remove Python 2 logic from modulefinder (#13275)
1 parent cbabbf7 commit da465ed

File tree

1 file changed

+3
-82
lines changed

1 file changed

+3
-82
lines changed

mypy/modulefinder.py

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ class SearchPaths(NamedTuple):
4747

4848
PYTHON_EXTENSIONS: Final = [".pyi", ".py"]
4949

50-
PYTHON2_STUB_DIR: Final = "@python2"
51-
5250

5351
# TODO: Consider adding more reasons here?
5452
# E.g. if we deduce a module would likely be found if the user were
@@ -192,7 +190,6 @@ def __init__(
192190
self.stdlib_py_versions = stdlib_py_versions or load_stdlib_py_versions(
193191
custom_typeshed_dir
194192
)
195-
self.python_major_ver = 3 if options is None else options.python_version[0]
196193

197194
def clear(self) -> None:
198195
self.results.clear()
@@ -292,9 +289,6 @@ def get_toplevel_possibilities(self, lib_path: Tuple[str, ...], id: str) -> List
292289
name = os.path.splitext(name)[0]
293290
components.setdefault(name, []).append(dir)
294291

295-
if self.python_major_ver == 2:
296-
components = {id: filter_redundant_py2_dirs(dirs) for id, dirs in components.items()}
297-
298292
self.initial_components[lib_path] = components
299293
return components.get(id, [])
300294

@@ -439,12 +433,6 @@ def _find_module(self, id: str, use_typeshed: bool) -> ModuleSearchResult:
439433
for pkg_dir in self.search_paths.package_path:
440434
stub_name = components[0] + "-stubs"
441435
stub_dir = os.path.join(pkg_dir, stub_name)
442-
if self.python_major_ver == 2:
443-
alt_stub_name = components[0] + "-python2-stubs"
444-
alt_stub_dir = os.path.join(pkg_dir, alt_stub_name)
445-
if fscache.isdir(alt_stub_dir):
446-
stub_name = alt_stub_name
447-
stub_dir = alt_stub_dir
448436
if fscache.isdir(stub_dir) and self._is_compatible_stub_package(stub_dir):
449437
stub_typed_file = os.path.join(stub_dir, "py.typed")
450438
stub_components = [stub_name] + components[1:]
@@ -506,11 +494,7 @@ def _find_module(self, id: str, use_typeshed: bool) -> ModuleSearchResult:
506494
# Prefer package over module, i.e. baz/__init__.py* over baz.py*.
507495
for extension in PYTHON_EXTENSIONS:
508496
path = base_path + sepinit + extension
509-
suffix = "-stubs"
510-
if self.python_major_ver == 2:
511-
if os.path.isdir(base_path + "-python2-stubs"):
512-
suffix = "-python2-stubs"
513-
path_stubs = base_path + suffix + sepinit + extension
497+
path_stubs = base_path + "-stubs" + sepinit + extension
514498
if fscache.isfile_case(path, dir_prefix):
515499
has_init = True
516500
if verify and not verify_module(fscache, id, path, dir_prefix):
@@ -591,10 +575,7 @@ def _is_compatible_stub_package(self, stub_dir: str) -> bool:
591575
if os.path.isfile(metadata_fnam):
592576
with open(metadata_fnam, "rb") as f:
593577
metadata = tomllib.load(f)
594-
if self.python_major_ver == 2:
595-
return bool(metadata.get("python2", False))
596-
else:
597-
return bool(metadata.get("python3", True))
578+
return bool(metadata.get("python3", True))
598579
return True
599580

600581
def find_modules_recursive(self, module: str) -> List[BuildSource]:
@@ -726,10 +707,6 @@ def default_lib_path(
726707
data_dir = auto
727708
typeshed_dir = os.path.join(data_dir, "typeshed", "stdlib")
728709
mypy_extensions_dir = os.path.join(data_dir, "typeshed", "stubs", "mypy-extensions")
729-
if pyversion[0] == 2:
730-
# Python 2 variants of certain stdlib modules are in a separate directory.
731-
python2_dir = os.path.join(typeshed_dir, PYTHON2_STUB_DIR)
732-
path.append(python2_dir)
733710
path.append(typeshed_dir)
734711

735712
# Get mypy-extensions stubs from typeshed, since we treat it as an
@@ -782,25 +759,6 @@ def get_search_dirs(python_executable: Optional[str]) -> Tuple[List[str], List[s
782759
return sys_path, site_packages
783760

784761

785-
def add_py2_mypypath_entries(mypypath: List[str]) -> List[str]:
786-
"""Add corresponding @python2 subdirectories to mypypath.
787-
788-
For each path entry 'x', add 'x/@python2' before 'x' if the latter is
789-
a directory.
790-
"""
791-
result = []
792-
for item in mypypath:
793-
python2_dir = os.path.join(item, PYTHON2_STUB_DIR)
794-
if os.path.isdir(python2_dir):
795-
# @python2 takes precedence, but we also look into the parent
796-
# directory.
797-
result.append(python2_dir)
798-
result.append(item)
799-
else:
800-
result.append(item)
801-
return result
802-
803-
804762
def compute_search_paths(
805763
sources: List[BuildSource], options: Options, data_dir: str, alt_lib_path: Optional[str] = None
806764
) -> SearchPaths:
@@ -863,11 +821,6 @@ def compute_search_paths(
863821
if alt_lib_path:
864822
mypypath.insert(0, alt_lib_path)
865823

866-
# When type checking in Python 2 module, add @python2 subdirectories of
867-
# path items into the search path.
868-
if options.python_version[0] == 2:
869-
mypypath = add_py2_mypypath_entries(mypypath)
870-
871824
sys_path, site_packages = get_search_dirs(options.python_executable)
872825
# We only use site packages for this check
873826
for site in site_packages:
@@ -919,19 +872,6 @@ def load_stdlib_py_versions(custom_typeshed_dir: Optional[str]) -> StdlibVersion
919872
parse_version(versions[1]) if len(versions) >= 2 and versions[1].strip() else None
920873
)
921874
result[module] = min_version, max_version
922-
923-
# Modules that are Python 2 only or have separate Python 2 stubs
924-
# have stubs in @python2/ and may need an override.
925-
python2_dir = os.path.join(stdlib_dir, PYTHON2_STUB_DIR)
926-
try:
927-
for fnam in os.listdir(python2_dir):
928-
fnam = fnam.replace(".pyi", "")
929-
max_version = result.get(fnam, ((2, 7), None))[1]
930-
result[fnam] = (2, 7), max_version
931-
except FileNotFoundError:
932-
# Ignore error to support installations where Python 2 stubs aren't available.
933-
pass
934-
935875
return result
936876

937877

@@ -944,23 +884,4 @@ def typeshed_py_version(options: Options) -> Tuple[int, int]:
944884
"""Return Python version used for checking whether module supports typeshed."""
945885
# Typeshed no longer covers Python 3.x versions before 3.6, so 3.6 is
946886
# the earliest we can support.
947-
if options.python_version[0] >= 3:
948-
return max(options.python_version, (3, 6))
949-
else:
950-
return options.python_version
951-
952-
953-
def filter_redundant_py2_dirs(dirs: List[str]) -> List[str]:
954-
"""If dirs has <dir>/@python2 followed by <dir>, filter out the latter."""
955-
if len(dirs) <= 1 or not any(d.endswith(PYTHON2_STUB_DIR) for d in dirs):
956-
# Fast path -- nothing to do
957-
return dirs
958-
seen = []
959-
result = []
960-
for d in dirs:
961-
if d.endswith(PYTHON2_STUB_DIR):
962-
seen.append(os.path.dirname(d))
963-
result.append(d)
964-
elif d not in seen:
965-
result.append(d)
966-
return result
887+
return max(options.python_version, (3, 6))

0 commit comments

Comments
 (0)