From 58a0e7afdd88b75886f2efab2d3cb6cc9c74c693 Mon Sep 17 00:00:00 2001 From: Tim Geypens Date: Fri, 12 Jan 2024 09:17:10 +0000 Subject: [PATCH 1/8] fscache: handle trailing slash in exists_case prefix --- mypy/fscache.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mypy/fscache.py b/mypy/fscache.py index 15679ad03e85..92b7db9a6831 100644 --- a/mypy/fscache.py +++ b/mypy/fscache.py @@ -230,6 +230,7 @@ def exists_case(self, path: str, prefix: str) -> bool: if path in self.exists_case_cache: return self.exists_case_cache[path] head, tail = os.path.split(path) + prefix = prefix.rstrip(os.sep) if not head.startswith(prefix) or not tail: # Only perform the check for paths under prefix. self.exists_case_cache[path] = True From 2ec8a8f9b0cd23972903ef5e83c3d512dd0702df Mon Sep 17 00:00:00 2001 From: Tim Geypens Date: Fri, 12 Jan 2024 11:09:08 +0000 Subject: [PATCH 2/8] add test --- mypy/test/testfscache.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mypy/test/testfscache.py b/mypy/test/testfscache.py index 44b0d32f5797..11fc67438294 100644 --- a/mypy/test/testfscache.py +++ b/mypy/test/testfscache.py @@ -88,6 +88,16 @@ def test_isfile_case_other_directory(self) -> None: # this path is not under the prefix, case difference is fine. assert self.isfile_case(os.path.join(other, "PKG/other_dir.py")) + def test_exists_case_1(self) -> None: + self.make_file("foo/bar/baz.py") + # Run twice to test both cached and non-cached code paths. + for i in range(2): + assert self.exists_case("foo/bar/baz.py", "foo") + assert self.exists_case("foo/bar", "foo") + assert not self.exists_case("foo/bar/non_existent1.py", "foo") + assert not self.exists_case("foo/bar/not_a_dir", "foo") + assert not self.exists_case("not_a_dir/not_a_subdir", "not_a_dir/") + def make_file(self, path: str, base: str | None = None) -> None: if base is None: base = self.tempdir @@ -99,3 +109,6 @@ def make_file(self, path: str, base: str | None = None) -> None: def isfile_case(self, path: str) -> bool: return self.fscache.isfile_case(os.path.join(self.tempdir, path), self.tempdir) + + def exists_case(self, path: str, prefix: str) -> bool: + return self.fscache.exists_case(os.path.join(self.tempdir, path), os.path.join(self.tempdir, prefix)) From 0da64a9b43282d09a64d39d46bff627274c7aada Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 12 Jan 2024 11:17:21 +0000 Subject: [PATCH 3/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/test/testfscache.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mypy/test/testfscache.py b/mypy/test/testfscache.py index 11fc67438294..277b0d78c00e 100644 --- a/mypy/test/testfscache.py +++ b/mypy/test/testfscache.py @@ -111,4 +111,6 @@ def isfile_case(self, path: str) -> bool: return self.fscache.isfile_case(os.path.join(self.tempdir, path), self.tempdir) def exists_case(self, path: str, prefix: str) -> bool: - return self.fscache.exists_case(os.path.join(self.tempdir, path), os.path.join(self.tempdir, prefix)) + return self.fscache.exists_case( + os.path.join(self.tempdir, path), os.path.join(self.tempdir, prefix) + ) From 3a490232ff85c1332181fa73f5c51ff17661900e Mon Sep 17 00:00:00 2001 From: Tim Geypens Date: Mon, 15 Jan 2024 14:17:09 +0000 Subject: [PATCH 4/8] fix tests for windows --- mypy/test/testfscache.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mypy/test/testfscache.py b/mypy/test/testfscache.py index 277b0d78c00e..326d1c7394d6 100644 --- a/mypy/test/testfscache.py +++ b/mypy/test/testfscache.py @@ -89,14 +89,14 @@ def test_isfile_case_other_directory(self) -> None: assert self.isfile_case(os.path.join(other, "PKG/other_dir.py")) def test_exists_case_1(self) -> None: - self.make_file("foo/bar/baz.py") + self.make_file(os.path.join("foo", "bar", "baz.py")) # Run twice to test both cached and non-cached code paths. for i in range(2): - assert self.exists_case("foo/bar/baz.py", "foo") - assert self.exists_case("foo/bar", "foo") - assert not self.exists_case("foo/bar/non_existent1.py", "foo") - assert not self.exists_case("foo/bar/not_a_dir", "foo") - assert not self.exists_case("not_a_dir/not_a_subdir", "not_a_dir/") + assert self.exists_case(os.path.join("foo", "bar", "baz.py"), "foo") + assert self.exists_case(os.path.join("foo", "bar"), "foo") + assert not self.exists_case(os.path.join("foo", "bar", "non_existent1.py"), "foo") + assert not self.exists_case(os.path.join("foo", "bar", "not_a_dir"), "foo") + assert not self.exists_case(os.path.join("not_a_dir", "not_a_subdir"), "not_a_dir" + os.sep) def make_file(self, path: str, base: str | None = None) -> None: if base is None: From c9db45cc90e6f951fc4bcc19c04b846e84c412cf Mon Sep 17 00:00:00 2001 From: Tim Geypens Date: Mon, 15 Jan 2024 14:58:16 +0000 Subject: [PATCH 5/8] catch another edge case --- mypy/fscache.py | 2 +- mypy/test/testfscache.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mypy/fscache.py b/mypy/fscache.py index 92b7db9a6831..8d890d4f7bf2 100644 --- a/mypy/fscache.py +++ b/mypy/fscache.py @@ -231,7 +231,7 @@ def exists_case(self, path: str, prefix: str) -> bool: return self.exists_case_cache[path] head, tail = os.path.split(path) prefix = prefix.rstrip(os.sep) - if not head.startswith(prefix) or not tail: + if not (head.startswith(prefix + os.sep) or head == prefix) or not tail: # Only perform the check for paths under prefix. self.exists_case_cache[path] = True return True diff --git a/mypy/test/testfscache.py b/mypy/test/testfscache.py index 326d1c7394d6..8eaaa846d923 100644 --- a/mypy/test/testfscache.py +++ b/mypy/test/testfscache.py @@ -94,7 +94,9 @@ def test_exists_case_1(self) -> None: for i in range(2): assert self.exists_case(os.path.join("foo", "bar", "baz.py"), "foo") assert self.exists_case(os.path.join("foo", "bar"), "foo") - assert not self.exists_case(os.path.join("foo", "bar", "non_existent1.py"), "foo") + assert self.exists_case(os.path.join("foo", "bar", "non_existent1.py"), "bar") + assert self.exists_case(os.path.join("foobar", "non_existent2.py"), "foo") + assert not self.exists_case(os.path.join("foo", "bar", "non_existent3.py"), "foo") assert not self.exists_case(os.path.join("foo", "bar", "not_a_dir"), "foo") assert not self.exists_case(os.path.join("not_a_dir", "not_a_subdir"), "not_a_dir" + os.sep) From 17fc5860d8595fb98f0d8396822491800c34a2a3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 15 Jan 2024 15:00:38 +0000 Subject: [PATCH 6/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/test/testfscache.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mypy/test/testfscache.py b/mypy/test/testfscache.py index 8eaaa846d923..66e9d70d2501 100644 --- a/mypy/test/testfscache.py +++ b/mypy/test/testfscache.py @@ -98,7 +98,9 @@ def test_exists_case_1(self) -> None: assert self.exists_case(os.path.join("foobar", "non_existent2.py"), "foo") assert not self.exists_case(os.path.join("foo", "bar", "non_existent3.py"), "foo") assert not self.exists_case(os.path.join("foo", "bar", "not_a_dir"), "foo") - assert not self.exists_case(os.path.join("not_a_dir", "not_a_subdir"), "not_a_dir" + os.sep) + assert not self.exists_case( + os.path.join("not_a_dir", "not_a_subdir"), "not_a_dir" + os.sep + ) def make_file(self, path: str, base: str | None = None) -> None: if base is None: From d66453b4fd6c245f39b7ef115580212b9271d1f7 Mon Sep 17 00:00:00 2001 From: Tim Geypens Date: Mon, 15 Jan 2024 15:40:20 +0000 Subject: [PATCH 7/8] handle empty prefix --- mypy/fscache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/fscache.py b/mypy/fscache.py index 8d890d4f7bf2..3247e0248774 100644 --- a/mypy/fscache.py +++ b/mypy/fscache.py @@ -231,7 +231,7 @@ def exists_case(self, path: str, prefix: str) -> bool: return self.exists_case_cache[path] head, tail = os.path.split(path) prefix = prefix.rstrip(os.sep) - if not (head.startswith(prefix + os.sep) or head == prefix) or not tail: + if (prefix != '' and not (head.startswith(prefix + os.sep) or head == prefix)) or not tail: # Only perform the check for paths under prefix. self.exists_case_cache[path] = True return True From 75fda8672decad3ce06def85cfdd41ef88a92187 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 15 Jan 2024 15:40:52 +0000 Subject: [PATCH 8/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/fscache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/fscache.py b/mypy/fscache.py index 3247e0248774..c4a1a016df67 100644 --- a/mypy/fscache.py +++ b/mypy/fscache.py @@ -231,7 +231,7 @@ def exists_case(self, path: str, prefix: str) -> bool: return self.exists_case_cache[path] head, tail = os.path.split(path) prefix = prefix.rstrip(os.sep) - if (prefix != '' and not (head.startswith(prefix + os.sep) or head == prefix)) or not tail: + if (prefix != "" and not (head.startswith(prefix + os.sep) or head == prefix)) or not tail: # Only perform the check for paths under prefix. self.exists_case_cache[path] = True return True