-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
Hey all, thanks for the great product, my code quality has significantly improved since I started using mypy.
I've, however, been struggling all day trying to get mypy to ignore the automatically generated protobuf files - ending with <filename>_pb2.py and <filename>_pb2_grpc.py due to the fact that these files are auto-generated and that we shouldn't be editing them as per: # Generated by the protocol buffer compiler. DO NOT EDIT!
I would like to exclude them. This in and of itself is difficult, let alone trying to get it to happen when running mpy in your CLI as well as in you pre-commit... I've read everything I can find:
- GitHub
python/mypy- Is there a way to totally ignore all of the MyPy errors in specific project packages? Is there a way to totally ignore all of the MyPy errors in specific project packages? #6155
- Exclude multiple directories on mypy commandline: improve documentation Exclude multiple directories on mypy commandline: improve documentation #10250
- Using mypy via pre-commit Using mypy via pre-commit #13916
- better doc on excluding files in pyproject.toml better doc on excluding files in pyproject.toml #13883
- StackOverflow
- Random Blog - Excluding files with Mypy hook for pre-commit
- mypy documentation - config_file
My current config:
Python 3.11.4
pre-commit-config.yaml
exclude: ^proto/zkp_auth_pb2_grpc.py$|^proto/zkp_auth_pb2.py$
repos:
- repo: https://github.com/PyCQA/bandit
- repo: https://github.com/psf/black
- repo: https://github.com/pycqa/flake8
- repo: https://github.com/timothycrosley/isort
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.6.1
hooks:
- id: mypy
args: [--config-file=pyproject.toml]
- repo: https://github.com/asottile/pyupgradepyproject.toml
[tool.mypy]
python_version = "3.11"
check_untyped_defs = true
disallow_untyped_defs = true
incremental = false
ignore_errors = false
pretty = true
show_error_context = true
show_traceback = true
strict_optional = true
warn_incomplete_stub = true
warn_no_return = true
warn_redundant_casts = true
warn_return_any = true
warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true
[[tool.mypy.overrides]]
module = [
"google.protobuf.*",
"grpc.*",
]
ignore_missing_imports = trueThe structure of my project is:
.
├── ci
│ ├── Dockerfile-client
│ └── Dockerfile-server
├── docker-compose.yml
├── poetry.lock
├── proto
│ ├── __init__.py
│ ├── zkp_auth_pb2_grpc.py
│ ├── zkp_auth_pb2.py
│ ├── zkp_auth_pb2.pyi
│ └── zkp_auth.proto
├── pyproject.toml
├── README.md
├── scripts
│ └── setup.sh
├── src
│ ├── client.py
│ ├── __init__.py
│ ├── __main__.py
│ └── server.py
└── testsA few of the things I tried (excuse my use of regex I'm still very new to it) in my pyproject.toml are:
exclude = '(^|/)proto/)'
exclude = "(^|/)proto/)"
exclude = "(proto/zkp_auth_pb2_grpc.py)"
exclude = "(proto/zkp_auth_pb2_grpc.py|proto/zkp_auth_pb2.py)"
exclude = "(zkp_auth_pb2_grpc.py|zkp_auth_pb2.py)/$"
exclude = """
(?x)(
zkp_auth_pb2_grpc.py$
)
"""
exclude = "./proto"
exclude = '^proto/'
exclude = [
'^proto/',
]
exclude = [
'^proto/zkp_auth_pb2_grpc.py$',
'^proto/zkp_auth_pb2.py$'
]
exclude = [
"^proto/zkp_auth_pb2_grpc.py$",
"^proto/zkp_auth_pb2.py$"
]
[tool.mypy.exclude]
files = [
'^proto/zkp_auth_pb2_grpc.py$',
'^proto/zkp_auth_pb2.py$'
]
[tool.mypy.exclude]
files = [
"^proto/zkp_auth_pb2_grpc.py$",
"^proto/zkp_auth_pb2.py$"
]
[tool.mypy.files]
exclude = '^(?:proto/|/proto/)'
[mypy.myproject.generated.*]
[tool.mypy.ignore_errors]
module = [
"proto.*",
]Furthermore I tried:
- Using a
mypy.inifile instead - Fiddling with the
pre-commit-config.yamlwith regards to the arguments being parsed to it- Adding
pass_filenames: false
- Adding
- Running pypy manually
The part I also don't get is that I was able to commit the auto-generated files even if they had issues, think I had exclude: proto/ in my pre-commit that time, but then that the other files importing these auto-generated protobuf files would result in issues when committing, example:
src/client.py:5: note: In module imported here:
proto/zkp_auth_pb2_grpc.py:If anyone has any ideas/tips/insights in how I can exclude these files from mypy that would be great appreciated?
Do I potentially need to use: https://github.com/nipunn1313/mypy-protobuf?
@posita I hope you don't mind me tagging you as p[er your post here