mypy
code analysis
Links
mypy
is an optional static type checker for Python that aims to combine the benefits of dynamic typing and static typing. By adding type annotations to your Python code, mypy can analyze your programs without executing them, identifying potential errors and improving code quality. It supports features such as type inference, generics, and gradual typing, allowing for a seamless transition from dynamic to static typing.
Usage Example
Here’s a simple example of using mypy to type-check a Python function:
def add_numbers(a: int, b: int) -> int:
return a + b
result = add_numbers(5, 10)
print(result) # Output: 15
To check this code with mypy, save it to a file (e.g., example.py
) and run:
mypy example.py
If the code is correctly typed, mypy will output:
Success: no issues found in 1 source file