Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions pytest_mypy_plugins/item.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import importlib
import io
import os
import re
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -92,6 +93,17 @@ class ReturnCodes:
FATAL_ERROR = 2


def _get_sqlite_num_shards(cache_dir: str) -> int:
"""Return the number of SQLite shards in the cache directory, or 0 if none exist."""
cache_dir_path = Path(cache_dir)
shard_files = [f for f in cache_dir_path.glob("cache.*.db") if re.match(r"^cache\.\d+\.db$", f.name)]
if shard_files:
return len(shard_files)
if (cache_dir_path / "cache.db").is_file():
return 1
return 0


def run_mypy_typechecking(cmd_options: List[str], stdout: TextIO, stderr: TextIO) -> int:
fscache = FileSystemCache()
sources, options = process_options(cmd_options, fscache=fscache)
Expand Down Expand Up @@ -374,7 +386,16 @@ def remove_cache_files(self, fpath_no_suffix: Path) -> None:

# Build entry names to remove for each path component so namespace-package
# cache entries are also cleaned (e.g. "pkg.*", "pkg/sub.*", "pkg/sub/mod.*").
suffixes = (".meta.json", ".data.json", ".err.json", ".meta.ff", ".data.ff", ".err.ff")
suffixes = (
".meta.json",
".data.json",
".err.json",
".meta_ex.json",
".meta.ff",
".data.ff",
".err.ff",
".meta_ex.ff",
)
entries: list[str] = []
accumulated = ""
for i, part in enumerate(fpath_no_suffix.parts):
Expand All @@ -388,8 +409,9 @@ def remove_cache_files(self, fpath_no_suffix: Path) -> None:
entries.extend(accumulated + s for s in suffixes)

stores: list[MetadataStore] = []
if os.path.isfile(os.path.join(cache_dir, "cache.db")):
stores.append(SqliteMetadataStore(cache_dir))
num_shards = _get_sqlite_num_shards(cache_dir)
if num_shards > 0:
stores.append(SqliteMetadataStore(cache_dir, num_shards=num_shards))
stores.append(FilesystemMetadataStore(cache_dir))

for store in stores:
Expand Down
6 changes: 5 additions & 1 deletion pytest_mypy_plugins/tests/test_mypy_cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import subprocess
from collections.abc import Generator
from pathlib import Path
Expand Down Expand Up @@ -212,7 +213,10 @@ def make_yaml_test_file(
def get_created_cache_files(cache_dir: Path, module_rel_paths_no_suffix: tuple[str, ...]) -> list[str]:
stores: list[MetadataStore] = []
cache_dir_str = str(cache_dir)
if (cache_dir / "cache.db").is_file():
shard_files = [f for f in cache_dir.glob("cache.*.db") if re.match(r"^cache\.\d+\.db$", f.name)]
if shard_files:
stores.append(SqliteMetadataStore(cache_dir_str, num_shards=len(shard_files)))
elif (cache_dir / "cache.db").is_file():
stores.append(SqliteMetadataStore(cache_dir_str))
if cache_dir.is_dir():
stores.append(FilesystemMetadataStore(cache_dir_str))
Expand Down
10 changes: 6 additions & 4 deletions pytest_mypy_plugins/tests/test_site_packages.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os.path
import site
import subprocess
from pathlib import Path

import mypy
import pytest


Expand Down Expand Up @@ -155,7 +155,9 @@ def make_yaml_test_file(
file_base_name: str = "test-case",
) -> None:
output_path = root_dir.joinpath(file_base_name).with_suffix(".yml")
site_packages_path: Path | str = Path(site.getsitepackages()[0])
site_packages_path = os.path.relpath(site_packages_path, start=output_path)
contents = contents.format(site_packages_path=site_packages_path)
# Use the directory containing the installed mypy package as the site-packages path,
# since mypy may be installed in a user site-packages directory rather than the system one.
site_packages_path: Path = Path(mypy.__file__).parent.parent
site_packages_path_str = os.path.relpath(site_packages_path, start=output_path)
contents = contents.format(site_packages_path=site_packages_path_str)
output_path.write_text(contents)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ types-decorator
types-PyYAML
types-setuptools
types-regex
mypy==1.20.2
mypy==2.1.0
-e .
Loading