Skip to content

Improve type-checker mypy configuration #298

@daniel-zullo-frequenz

Description

@daniel-zullo-frequenz

What's needed?

The type checker mypy is configured to check specific packages within a repository. In practice, there are repositories that contain more than one package. This setup is not ideal because typically only one package is configured to be type-checked by mypy. As a result, parts of the codebase may not be checked, leading to potential type-related issues going unnoticed.

To ensure type-checking, all relevant packages should be included in the mypy configuration. This would help in maintaining type consistency and catching errors across the entire codebase.

Proposed solution

Cookiecutter templates

Replace the setting of packages and use files and mypy_path instead as follows:

Current configuration:

[tool.mypy]
packages = ["your_package_name"]  # Use the actual package name here

to be replaced by:

[tool.mypy]
mypy_path = "src" 
files = ["src", "tests", "docs", "noxfile.py"]

This will ensure the codebase is fully type-checked (at least for any file within src and tests) without the need for the user to set explicitly the packages.

Library code

This also requires a change in the provided nox session, as the mypy session now runs the mypy program twice, one for src and one for the rest. It should be changed to run mypy without any paths.

Notes

There is one downside of going this route. In the current approach we can declare a lot of potential path being used by projects, like examples and benchmarks that are usually not created by default. Then the nox configuration code will check the extra_paths to see if they exist, and use only those that exist. If we pass all of them to tool.mypy.files and some don't exist, mypy will fail to run. There are 2 possible solutions to this:

  1. Only use the set of files that are currently created by the cookiecutter template. The issue is if users create new "standard" directories like examples or benchmarks, they won't by checked by default, and this could go unnoticed for a long time.
  2. Create all directories as part of the templates. This adds a bit of noise to projects that don't use them, but it is the safer approach.

This will also introduce some duplication, as we still need to keep the extra_paths to pass to the pylint session, as pylint can't get the files to check via configuration, only via CLI.

Metadata

Metadata

Assignees

No one assigned

    Labels

    part:templateAffects the cookiecutter template filespart:toolingAffects the development tooling (CI, deployment, dependency management, etc.)priority:highAddress this as soon as possibletype:enhancementNew feature or enhancement visitble to users

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions