Skip to content

Commit

Permalink
Find pyproject.toml only once, and use it in CONFIG_FILES
Browse files Browse the repository at this point in the history
  • Loading branch information
Felixoid committed Feb 29, 2024
1 parent 65324b8 commit f4e0e40
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
26 changes: 1 addition & 25 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,28 +229,6 @@ def split_commas(value: str) -> list[str]:
)


def _find_pyproject() -> list[str]:
"""Search for file pyproject.toml in the parent directories recursively.
It resolves symlinks, so if there is any symlink up in the tree, it does not respect them
"""
# We start from the parent dir, since 'pyproject.toml' is already parsed
current_dir = os.path.abspath(os.path.join(os.path.curdir, os.path.pardir))
is_root = False
while not is_root:
for pyproject_name in defaults.PYPROJECT_CONFIG_FILES:
config_file = os.path.join(current_dir, pyproject_name)
if os.path.isfile(config_file):
return [os.path.abspath(config_file)]
parent = os.path.abspath(os.path.join(current_dir, os.path.pardir))
is_root = current_dir == parent or any(
os.path.isdir(os.path.join(current_dir, cvs_root)) for cvs_root in (".git", ".hg")
)
current_dir = parent

return []


def parse_config_file(
options: Options,
set_strict_flags: Callable[[], None],
Expand All @@ -270,9 +248,7 @@ def parse_config_file(
if filename is not None:
config_files: tuple[str, ...] = (filename,)
else:
config_files_iter: Iterable[str] = map(
os.path.expanduser, defaults.CONFIG_FILES + _find_pyproject()
)
config_files_iter: Iterable[str] = map(os.path.expanduser, defaults.CONFIG_FILES)
config_files = tuple(config_files_iter)

config_parser = configparser.RawConfigParser()
Expand Down
26 changes: 25 additions & 1 deletion mypy/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,33 @@
# mypy, at least version PYTHON3_VERSION is needed.
PYTHON3_VERSION_MIN: Final = (3, 8) # Keep in sync with typeshed's python support


def find_pyproject() -> str:
"""Search for file pyproject.toml in the parent directories recursively.
It resolves symlinks, so if there is any symlink up in the tree, it does not respect them
If the file is not found until the root of FS or repository, PYPROJECT_FILE is used
"""
current_dir = os.path.curdir
is_root = False
while not is_root:
config_file = os.path.join(current_dir, PYPROJECT_FILE)
if os.path.isfile(config_file):
return config_file
parent = os.path.join(current_dir, os.path.pardir)
is_root = current_dir == parent or any(
os.path.isdir(os.path.join(current_dir, cvs_root)) for cvs_root in (".git", ".hg")
)
current_dir = parent

return PYPROJECT_FILE


CACHE_DIR: Final = ".mypy_cache"
CONFIG_FILE: Final = ["mypy.ini", ".mypy.ini"]
PYPROJECT_CONFIG_FILES: Final = ["pyproject.toml"]
PYPROJECT_FILE: Final = "pyproject.toml"
PYPROJECT_CONFIG_FILES: Final = [find_pyproject()]
SHARED_CONFIG_FILES: Final = ["setup.cfg"]
USER_CONFIG_FILES: Final = ["~/.config/mypy/config", "~/.mypy.ini"]
if os.environ.get("XDG_CONFIG_HOME"):
Expand Down

0 comments on commit f4e0e40

Please sign in to comment.