pytorch
01441af7 - Use mypy internals instead of fnmatch for mypy wrapper (#55702)

Commit
3 years ago
Use mypy internals instead of fnmatch for mypy wrapper (#55702) Summary: I noticed that https://github.com/pytorch/pytorch/issues/53296 added these two lines to the `files` list in `mypy-strict.ini`: ``` benchmarks/instruction_counts/*.py, benchmarks/instruction_counts/*/*.py, ``` I opened https://github.com/pytorch/pytorch/issues/55700 to simplify them into one line, but I was also curious whether `tools/mypy_wrapper.py` correctly handles those patterns, so I added the `test_glob_wildcards_dont_expand_or_collapse` case shown in this PR. Turns out, it doesn't! I believe this is because [`mypy` uses `glob`](https://github.com/python/mypy/blob/v0.770/mypy/config_parser.py#L45-L63) to parse these patterns, and for some reason, [`fnmatch`](https://docs.python.org/3/library/fnmatch.html) and [`glob`](https://docs.python.org/3/library/glob.html) don't agree with each other on what `*` means: - according to `fnmatch`, `*` seems to mean `.*` - according to `glob`, `*` seems to mean `[^/]*` [This SO answer](https://stackoverflow.com/a/60174071) suggests using the [`glob.globmatch` function from the `wcmatch` library](https://facelessuser.github.io/wcmatch/glob/#globmatch) to solve the issue, but [we didn't want to add another external dependency](https://github.com/pytorch/pytorch/pull/55702#discussion_r610868623), so instead I simply modified our matching function to just directly call `mypy`'s own internal function that does the globbing (linked above). One possible downside of this approach is that now the tests in `tools/test/test_mypy_wrapper.py` could break if the directory structure of PyTorch is changed. Pull Request resolved: https://github.com/pytorch/pytorch/pull/55702 Test Plan: ``` python tools/test/test_mypy_wrapper.py ``` Reviewed By: malfet, seemethere Differential Revision: D27684499 Pulled By: samestep fbshipit-source-id: d99387a579c21eee73d1714e3e815ab7155f9646
Author
Parents
Loading