jazzband / django-model-utils

Django model mixins and utilities.
https://django-model-utils.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.66k stars 365 forks source link

Enable static type checks using mypy in CI #601

Closed mthuurne closed 6 months ago

mthuurne commented 6 months ago

This is a first step in adding type annotations. See #558 for discussion.

Some things to pay attention to during review:

I'm using from __future__ import annotations to force postponed evaluation of annotations. This has several advantages:

I added typing_extensions as a runtime dependency. This makes it possible to use both runtime and static features from newer versions of Python in older versions as well. In particular, typing.Self simplifies a lot of annotations, but was only added to the standard library in Python 3.11.

It is possible to only use static features of typing_extensions and put the import within a if TYPE_CHECKING: guard. It makes maintenance a bit more cumbersome, but it would eliminate the runtime dependency. As a lot of other libraries are using typing_extensions already, I'm not sure if it avoiding the dependency in django-model-utils would actually make a difference in any real-world application. But if you want me to make that change, let me know.

I've used Python 3.8 in the tox configuration to run mypy, because that is the one that also runs flake8 and isort. I'm not sure that's a good reason though. Another option would be to use Python 3.12, as that is the fastest. Note that mypy's results do not depend on the Python version it runs under: it always has access to all typing features it supports.

The type checking of the unit tests requires all imported libraries to exist, which is why I added time_machine as a dependency of the mypy env in tox. If we want to avoid that duplication, maybe it would make sense to create a requirements-testlib.txt or add a testlib extra to setup.py.

It would also be possible to move time_machine into requirements-test.txt, but installing the test tools and runtime dependencies like psycopg2-binary isn't necessary for mypy to work and I wanted to avoid slowing down CI.

codecov[bot] commented 6 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 98.94%. Comparing base (4c9d6ee) to head (c75e54a).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #601 +/- ## ======================================= Coverage 98.94% 98.94% ======================================= Files 6 6 Lines 757 757 ======================================= Hits 749 749 Misses 8 8 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

mthuurne commented 6 months ago

In the summary, I wrote:

As a lot of other libraries are using typing_extensions already, I'm not sure if it avoiding the dependency in django-model-utils would actually make a difference in any real-world application.

In our own Django application, it turns out only scipy depends on typing_extensions, and it has it as an optional dev dependency rather than a runtime dependency. So perhaps I overestimated how many libraries are using it.

foarsitter commented 6 months ago

Thans for your throughout description of this PR.

Is the typing_extensions thing something we can solve in another way?

mthuurne commented 6 months ago

Is the typing_extensions thing something we can solve in another way?

I used it in several places in earlier versions of the annotations, but in the latest version there are no uses left. I'll drop it from this PR and we can see whether or not to add it later if/when there is a concrete use case to discuss.