MCPcopy Create free account
hub / github.com/python/mypy / is_sub_path_normabs

Function is_sub_path_normabs

mypy/util.py:422–441  ·  view source on GitHub ↗

Given two paths, return if path is a sub-path of dir. Moral equivalent of: Path(dir) in Path(path).parents Similar to the pathlib version: - Treats paths case-sensitively - Does not fully handle unnormalised paths (e.g. paths with "..") - Does not handle a mix of absolute and r

(path: str, dir: str)

Source from the content-addressed store, hash-verified

420
421
422def is_sub_path_normabs(path: str, dir: str) -> bool:
423 """Given two paths, return if path is a sub-path of dir.
424
425 Moral equivalent of: Path(dir) in Path(path).parents
426
427 Similar to the pathlib version:
428 - Treats paths case-sensitively
429 - Does not fully handle unnormalised paths (e.g. paths with "..")
430 - Does not handle a mix of absolute and relative paths
431 Unlike the pathlib version:
432 - Fast
433 - On Windows, assumes input has been slash normalised
434 - Handles even fewer unnormalised paths (e.g. paths with "." and "//")
435
436 As a result, callers should ensure that inputs have had os.path.abspath called on them
437 (note that os.path.abspath will normalise)
438 """
439 if not dir.endswith(os.sep):
440 dir += os.sep
441 return path.startswith(dir)
442
443
444if sys.platform == "linux" or sys.platform == "darwin":

Callers 1

is_silent_import_moduleFunction · 0.90

Calls 2

endswithMethod · 0.45
startswithMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…