typeddjango / django-stubs

PEP-484 stubs for Django
MIT License
1.61k stars 450 forks source link

django-stubs does not appear to run in a pre-commit environment #1645

Closed dakotahorstman closed 1 year ago

dakotahorstman commented 1 year ago

Bug report

What's wrong

It would appear django-stubs is not integrating properly in my pre-commit environment. This is similar to #1087 in that I also encountered the same issues as that ticket, but have since moved beyond. The exact error I'm getting is:

src/project/app/models.py:34: error: Need type annotation for "serial_no"  [var-annotated]
src/project/app/models.py:39: error: Need type annotation for "timestamp"  [var-annotated]
src/project/app/models.py:40: error: Need type annotation for "cavity"  [var-annotated]
src/project/app/models.py:42: error: Need type annotation for "captouch_passed"  [var-annotated]

and the fields in my model:

serial_no = models.CharField(
    _("Serial Number"),
    max_length=16,
    validators=[RegexValidator(regex=r"\A[a-zA-Z]{3}[0-9]{8,13}\Z")],
)
timestamp = models.DateTimeField(_("Timestamp"), auto_now_add=True)
cavity = models.IntegerField(_("Cavity"), choices=CavityPosition.choices)

captouch_passed = models.BooleanField(
    _("Capacitive Touch Pass"),
    default=False,
)

and my pre-commit configuration:

- repo: https://github.com/pre-commit/mirrors-mypy
  rev: v1.4.1
  hooks:
  - id: mypy
    args: ["--config=/app/pyproject.toml"]
    additional_dependencies: [
      "django-stubs[compatible-mypy]",
      "django-stubs-ext",
      "djangorestframework-stubs",
      ...  # other dependencies
    ]

and my pyproject.toml:

[tool.mypy]
allow_redefinition = false
check_untyped_defs = true
ignore_errors = false
ignore_missing_imports = true
implicit_reexport = false
local_partial_types = true
strict_optional = true
strict_equality = true
no_implicit_optional = true
warn_unused_ignores = true
warn_redundant_casts = true
warn_unused_configs = true
warn_unreachable = true
warn_no_return = true
plugins = ["mypy_django_plugin.main", "mypy_drf_plugin.main"]
mypy_path = "src"

[[tool.mypy.overrides]]
# Django migrations should not produce any errors:
module = "*.migrations.*"
ignore_errors = true

[tool.django-stubs]
django_settings_module = "project.settings.test"

When running mypy directly in my environment, I do not get these same errors from mypy. I should also note that I'm running in a Docker container as a normal user, not root.

System information

sobolevn commented 1 year ago

I don't have much experience with pre-commit, so I don't know how to help you :(

dakotahorstman commented 1 year ago

Darn, okay. Maybe pinging @asottile will bring some light?

dakotahorstman commented 1 year ago

Well, this suddenly started working and I have no idea why. I haven't touched anything related to django-stubs and yet it seems like things are working. Will still keep this open until I am confident it works or until I can find out why for sure.

sobolevn commented 1 year ago

Sounds like it might be related to .mypy_cache

adamchainz commented 1 year ago

I have it working in pre-commit environments for my open source projects and clients. I don't think there's anything django-stubs can do here. It really does sound like a .mypy_cache issue, or Docker sync/configuration issue.

dakotahorstman commented 1 year ago

I agree. I deleted the cache directory and it appears to be working now. Will close this issue now that I'm confident that's the root cause.