Skip to content

Excluding a protobuf directory from mypy in pyproject.toml & pre-commit-config.yaml #16403

@BenWolfaardt

Description

@BenWolfaardt

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:

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/pyupgrade

pyproject.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 = true

The 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
└── tests

A 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.ini file instead
  • Fiddling with the pre-commit-config.yaml with regards to the arguments being parsed to it
    • Adding pass_filenames: false
  • 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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions