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:
- 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.
- 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.
What's needed?
The type checker
mypyis 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 bymypy. 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
mypyconfiguration. 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
filesandmypy_pathinstead as follows:Current configuration:
to be replaced by:
This will ensure the codebase is fully type-checked (at least for any file within
srcandtests) without the need for the user to set explicitly the packages.Library code
This also requires a change in the provided
noxsession, as themypysession now runs themypyprogram twice, one forsrcand one for the rest. It should be changed to runmypywithout 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
examplesandbenchmarksthat are usually not created by default. Then thenoxconfiguration code will check theextra_pathsto see if they exist, and use only those that exist. If we pass all of them totool.mypy.filesand some don't exist,mypywill fail to run. There are 2 possible solutions to this:examplesorbenchmarks, they won't by checked by default, and this could go unnoticed for a long time.This will also introduce some duplication, as we still need to keep the
extra_pathsto pass to thepylintsession, aspylintcan't get the files to check via configuration, only via CLI.