From 33261ccbe000323cfb1a227f079ab92226d794f8 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Wed, 24 Apr 2024 01:52:45 -0400 Subject: [PATCH] attempt to account for is_relative_to not available in py3.8 --- pipenv/environment.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/pipenv/environment.py b/pipenv/environment.py index 8bb171fa8..c1d310f94 100644 --- a/pipenv/environment.py +++ b/pipenv/environment.py @@ -528,16 +528,14 @@ def dist_is_in_project(self, dist: importlib_metadata.Distribution) -> bool: # Since is_relative_to is not available in Python 3.8, we use a workaround if sys.version_info == (3, 8): - def is_relative_to(path, to_path): - try: - # Attempt to resolve the relative path - path.relative_to(to_path) - return True - except ValueError: - # If ValueError, it means the path is not relative - return False - - return any(is_relative_to(location, Path(libdir)) for libdir in libdirs) + def is_subpath(path, base_path): + # Ensure both paths are absolute and resolve any symlinks + abs_path = path.resolve() + abs_base_path = base_path.resolve() + # Convert to string and check prefix match + return abs_path.parts[: len(abs_base_path.parts)] == abs_base_path.parts + + return any(is_subpath(location, Path(libdir)) for libdir in libdirs) else: return any(location.is_relative_to(libdir) for libdir in libdirs)