Skip to content
Latchkey

mypy --exclude: Skip Files by Regex

mypy --exclude takes a regular expression and skips files whose path matches it.

Generated code, fixtures, and vendored trees usually should not be checked. --exclude keeps them out, but its regex (not glob) semantics trip people up.

What it does

mypy --exclude matches a regular expression against each candidate file path and skips the matches. It is a regex, not a glob, and it is matched against the path; in config the exclude key can be a single regex or a list of regexes.

Common usage

Terminal
mypy --exclude '(^build/|/migrations/|_pb2\.py$)' src/
# config form (list of regexes)
# [tool.mypy]
# exclude = ["^build/", "/migrations/", "_pb2\\.py$"]

Options

Flag or keyWhat it does
--exclude <regex>Skip files matching the regular expression
exclude (config)String or list of regexes in config
^ / $Anchor to start/end of the path within the regex
ignore_errors (override)Alternative: check but suppress errors per module

In CI

Remember --exclude only applies to files mypy discovers itself; a path passed explicitly on the command line is still checked even if it matches the exclude. So mypy src/ honors the exclude, but mypy src/build/x.py does not. In config use a list of anchored regexes for readability.

Common errors in CI

Treating --exclude as a glob is the classic mistake: build/* does not mean what you think, and a leading dot must be escaped (\.). If excluded files are still checked, they were probably named explicitly on the command line; exclude only filters auto-discovered paths.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →