Skip to content

Remove cache files created for namespace packages#180

Merged
sobolevn merged 4 commits intotypeddjango:masterfrom
bzoracler:remove-namespace-cache
Jan 16, 2026
Merged

Remove cache files created for namespace packages#180
sobolevn merged 4 commits intotypeddjango:masterfrom
bzoracler:remove-namespace-cache

Conversation

@bzoracler
Copy link
Contributor

This PR fixes an issue where cache files created from namespace packages are not cleaned up.


For the following concrete example,

- case: my_test_case
  main: |
    import pkg.module
    import nspkg.module
  files:
    - path: pkg/__init__.py
    - path: pkg/module.py
    - path: nspkg/module.py

during pytest, mypy creates the following under /tmp/.mypy_cache/3.1x/:

  • pkg/__init__.{data & meta}.json
  • pkg/module.{data & meta}.json
  • nspkg/module.{data & meta}.json
  • nspkg.{data & meta}.json

The existing implementation of YamlTestItem.remove_cache_files only passes pathlib.Path arguments pointing to files, not directories, so while mypy creates .{data,meta}.json cache files for namespace packages' directory names, .remove_cache_files() never removes these cache files.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Can we test?

cache_file /= ".".join([str(part) for part in sys.version_info[:2]])
for part in fpath_no_suffix.parts:
for i, part in enumerate(fpath_no_suffix.parts):
if (i == 0) and part.endswith("-stubs") and ((cache_file / part.removesuffix("-stubs")).is_dir()):
Copy link
Contributor Author

@bzoracler bzoracler Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This guards against removal of unrelated cache files.

For example, if we used pytest-mypy-plugins to type-check a file called abc-stubs.pyi, it should not remove the cache files abc.data.json and abc.meta.json.

I didn't add tests for this; testing this requires getting pytest-mypy-plugins to type-check a module that's not called main.py, or to arbitrarily check an entire package, and I don't think we can do that?

@bzoracler bzoracler requested a review from sobolevn January 16, 2026 01:37
@sobolevn sobolevn merged commit 4cf41a2 into typeddjango:master Jan 16, 2026
11 checks passed
@bzoracler bzoracler deleted the remove-namespace-cache branch January 16, 2026 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants