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]