Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make is_sub_path faster #17962

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ def default_lib_path(
path: list[str] = []

if custom_typeshed_dir:
custom_typeshed_dir = os.path.normcase(custom_typeshed_dir)
typeshed_dir = os.path.join(custom_typeshed_dir, "stdlib")
mypy_extensions_dir = os.path.join(custom_typeshed_dir, "stubs", "mypy-extensions")
versions_file = os.path.join(typeshed_dir, "VERSIONS")
Expand Down
9 changes: 5 additions & 4 deletions mypy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import hashlib
import io
import os
import pathlib
import re
import shutil
import sys
Expand Down Expand Up @@ -411,9 +410,11 @@ def replace_object_state(
pass


def is_sub_path(path1: str, path2: str) -> bool:
"""Given two paths, return if path1 is a sub-path of path2."""
return pathlib.Path(path2) in pathlib.Path(path1).parents
def is_sub_path(path: str, dir: str) -> bool:
"""Given two paths, return if path is a sub-path of dir."""
if not dir.endswith(os.sep):
dir += os.sep
return path.startswith(dir)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this will work reliably on Windows, where path separators can be / or \, and these are largely equivalent? Should we normalize separators on Windows, or can we assume everything is normalized here? I noticed that you added a call to os.path.normcase, so maybe this is already ok.

Maybe update docstring to mention Windows-specific issues, and case insensitivity issues?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented the shortcomings of the faster function + invariants we require in modulefinder. Also renamed the function so it sounds a little scarier



if sys.platform == "linux" or sys.platform == "darwin":
Expand Down
Loading