Search code examples
pythonpython-typingmypy

How to get `mypy` to raise errors/warnings about using the `typing` package instead of built in types


Currently I have a bunch of code that does this:

from typing import Dict
foo: Dict[str, str] = []

In Python 3.9+, it is preferable to use the built-in types (source):

foo: dict[str, str] = []

Is there a way to configure mypy to raise an error/warning when my code uses Dict instead of dict?


Solution

  • According to the mypy maintainers, this fuctionality will not be implemented because it has already been implemented by formatters like ruff:

    This is already implemented by linters such as Ruff (https://docs.astral.sh/ruff/rules/non-pep585-annotation/). That doesn't mean mypy can't also implement support for checks like this, but it does mean the return on investment for mypy to add this feature is lower.

    Source