Type Checking with MyPy and Pyright¶
Type checking is an essential part of maintaining a robust and error-free codebase. MyPy and Pyright are both static type checkers for Python that help you ensure your code adheres to specified type annotations.
Setting Up MyPy¶
To set up MyPy for your project, follow these steps:
Install MyPy:
uv pip install mypy
Configure MyPy: check in the
myproject.tomlfile the [tool.mypy] sectionRun MyPy: To check your code with MyPy, run the following command:
uvx --with-editable . mypy py_launch_blueprint/
or
just typecheck
Setting Up Pyright¶
To set up Pyright for your project, follow these steps:
Install Pyright:
uv pip install pyright
Configure Pyright: check
pyrightconfig.jsonfile in the root of the projectRun Pyright: To check your code with Pyright, run the following command:
uvx --with-editable . pyright py_launch_blueprint/
Best Practices for Type Checking¶
Annotate All Functions: Ensure all functions have type annotations for their parameters and return types.
Use Type Hints: Utilize Python’s built-in type hints (e.g.,
List,Dict,Optional) to specify the expected types.Avoid
Any: Minimize the use of theAnytype to maintain strict type checking.Leverage
TypedDict: UseTypedDictfor dictionaries with a fixed set of keys and value types.Check Third-Party Libraries: Ensure third-party libraries used in your project have type stubs available.
Common Issues and Solutions¶
Missing Type Annotations:
Issue: MyPy/Pyright reports missing type annotations for functions.
Solution: Add type annotations to all function parameters and return types.
Incompatible Types:
Issue: MyPy/Pyright reports incompatible types in assignments or function calls.
Solution: Ensure the types of variables and function arguments match the expected types.
Ignoring Errors:
Issue: MyPy/Pyright reports errors that you want to ignore.
Solution: Use
# type: ignorecomments to suppress specific errors, but use them sparingly.
# mypy reveal_type(x) # type: ignore # pyright x = something() # pyright: ignore
Third-Party Libraries:
Issue: MyPy/Pyright reports missing type stubs for third-party libraries.
Solution: Install type stubs for the libraries using
uv pip install types-<library>.
Type checking only specific files:
Issue: You want to run MyPy/Pyright on specific files or directories.
Solution: Specify the files or directories to check as arguments to the MyPy/Pyright command.
# mypy mypy src/main.py src/utils.py # pyright pyright src/main.py src/utils.py
By following these best practices and addressing common issues, you can effectively use MyPy and Pyright to maintain a type-safe and reliable codebase. Read more about mypy