2

I am building a cookiecutter template for a python package, and I want to run a bunch of checks for the template repo itself with pre-commit.

A skeleton of the repo looks like this:

my_cookiecutter_template
|   .pre-commit-config.yaml
|   cookiecutter.json
|
|___{{cookiecutter.project_slug}}
    |    pyproject.toml
    | 
    |____{{cookiecutter.project_slug}}
         |   __init__.py

One of the pre-commit hooks I am using is mypy, but it is failing with this error:

{{cookiecutter.project_slug}} is not a valid Python package name

because there is an __init__.py file in the directory called {{cookiecutter.project_slug}} (which will obviously be renamed with a valid name when the template is instantiated).

My question is, how can I suppress this mypy error? The mypy docs have details of lots of exceptions with a specific error code which gives you a way to suppress it. But, unless I am mistaken, there is nothing there that pertains to this specific error

Ben Jeffrey
  • 714
  • 9
  • 18

1 Answers1

0

I had to add the following to the pre-commit-config.yaml:

# See https://pre-commit.com for more information
 # See https://pre-commit.com/hooks.html for more hooks
 repos:
    -   id: mypy
         # See https://github.com/pre-commit/mirrors-mypy/issues/1
         # This seems like the simplest way to avoid the mypy error
         # {{cookiecutter.project_slug}} is not a valid Python package name
         exclude: '.*__init__\.py'

Normally, the way to exclude certain files from mypy would be to add the following to any mypy config file (source):

[mypy]
exclude = __init__.py
#alternative
#exclude = (?x)(
#    __init__.py$
#    )

Given how pre-commit works, it's not so easy to use this setting. Please note that this solution is using the exclude from pre-commit itself, and not the mypy exclude.

mbestipa
  • 9
  • 2