From e392c027ba6e325078ae4df0ce40bc6a67ebea8f Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Mon, 9 Feb 2026 15:51:26 +0000 Subject: [PATCH] Stop looking for .gitignore at top level of working tree I have a checkout where I'm using the new `exclude_gitignore` facility introduced in #18696, and where a parent directory has a `.gitignore` file that happens to match the full path of the checkout. I just spent quite some time being very confused about why mypy reported success despite the code having obvious typing errors. Unlike git itself, mypy didn't stop looking for `.gitignore` files at the top level of the working tree; now it does. --- mypy/modulefinder.py | 2 +- test-data/unit/cmdline.test | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/mypy/modulefinder.py b/mypy/modulefinder.py index 6a11d18601d8..b3b88e5aac65 100644 --- a/mypy/modulefinder.py +++ b/mypy/modulefinder.py @@ -726,7 +726,7 @@ def matches_gitignore(subpath: str, fscache: FileSystemCache, verbose: bool) -> @functools.lru_cache def find_gitignores(dir: str) -> list[tuple[str, PathSpec]]: parent_dir = os.path.dirname(dir) - if parent_dir == dir: + if parent_dir == dir or os.path.exists(os.path.join(dir, ".git")): parent_gitignores = [] else: parent_gitignores = find_gitignores(parent_dir) diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index 1a2e37e66546..180f6e2efec9 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -957,6 +957,16 @@ bpkg.* [out] c/cpkg.py:1: error: "int" not callable +[case testCmdlineExcludeGitignoreOnlyWithinTree] +# cmd: mypy --exclude-gitignore b +[file .gitignore] +b +[file b/.git/config] +[file b/bpkg.py] +1() +[out] +b/bpkg.py:1: error: "int" not callable + [case testCmdlineCfgExclude] # cmd: mypy . [file mypy.ini]