{"id":445,"library":"mypy","title":"Mypy: Optional Static Typing for Python","description":"Mypy is a static type checker for Python, enabling optional static typing to improve code quality and maintainability. As of version 1.19.1, it supports Python 3.9 and later, with regular updates and a stable release cadence.","status":"active","version":"1.19.1","language":"python","source_language":"en","source_url":"https://github.com/python/mypy","tags":["static typing","type checker","Python","mypy"],"install":[{"cmd":"pip install mypy","lang":"bash","label":"Install Mypy"}],"dependencies":[{"reason":"Provides backports of typing features for older Python versions","package":"typing-extensions","optional":true}],"imports":[{"note":"Use 'Final' from 'typing' to indicate constants; 'typing_extensions' is needed for Python versions prior to 3.8.","symbol":"Final","correct":"from typing import Final"},{"note":"Use 'TypedDict' from 'typing' for defining typed dictionaries; 'typing_extensions' is needed for Python versions prior to 3.8.","symbol":"TypedDict","correct":"from typing import TypedDict"}],"quickstart":{"code":"import sys\nfrom mypy import api\n\nresult = api.run(sys.argv[1:])\n\nif result[0]:\n    print('\\nType checking report:\\n')\n    print(result[0])  # stdout\n\nif result[1]:\n    print('\\nError report:\\n')\n    print(result[1])  # stderr\n\nsys.exit(result[2])  # exit status","lang":"python","description":"Integrate Mypy into your Python application by importing 'mypy.api' and calling the 'run' function with command-line arguments. This allows programmatic access to Mypy's type checking capabilities."},"warnings":[{"fix":"Upgrade to Python 3.8 or later to maintain compatibility.","message":"Mypy 1.9 introduced breaking changes due to an updated typeshed version, dropping support for Python 3.7.","severity":"breaking","affected_versions":"1.9"},{"fix":"Install 'typing_extensions' to use these features on older Python versions.","message":"Using 'Final' and 'TypedDict' from 'typing' requires Python 3.8 or later; for earlier versions, install 'typing_extensions'.","severity":"gotcha","affected_versions":"1.19.1"},{"fix":"Ensure that the 'mypy' command is followed by the path to the files, modules, or packages you intend to type-check, for example: 'mypy my_script.py' or 'mypy -m my_package'.","message":"Mypy reported a 'Missing target' error, indicating that it was called without specifying any files, modules, or packages to type-check. This is a usage error.","severity":"gotcha","affected_versions":"all"},{"fix":"Ensure the 'mypy' command is invoked with a target, such as 'mypy your_module.py', 'mypy --package your_package', or 'mypy --module your_module'.","message":"Mypy failed because no target module, package, or files were specified. The 'mypy' command requires at least one argument indicating what to type-check.","severity":"breaking","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-05-12T13:52:07.455Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Install the module (e.g., `pip install foo`), install its type stubs (e.g., `pip install types-foo` or `mypy --install-types`), add the module's parent directory to `MYPYPATH`, or configure `mypy.ini` with `ignore_missing_imports = True` for the specific module (e.g., `[mypy-foo.*] ignore_missing_imports = True`).","cause":"Mypy cannot locate the Python module 'foo' or its associated type stubs (.pyi files), often because it's a third-party library without stubs, a local module not on Mypy's search path, or not installed in the environment Mypy is checking.","error":"Cannot find implementation or library stub for module named 'foo'"},{"fix":"Ensure the assigned value's type matches the variable's expected type, or explicitly annotate the variable with a `Union` type if it can legitimately hold different types, or use `cast()` if you are certain about the type and want to override Mypy.","cause":"You are attempting to assign a value of one type (expression type 'X') to a variable that has been explicitly or implicitly typed as another incompatible type ('Y').","error":"Incompatible types in assignment (expression has type \"X\", variable has type \"Y\")"},{"fix":"Add an explicit type annotation to the variable, especially for empty containers (e.g., `my_list: list[str] = []` or `my_dict: dict[str, int] = {}`).","cause":"Mypy is unable to infer the precise type of a variable, typically occurring with empty collections (e.g., `[]`, `{}`) or when `--disallow-untyped-defs` or `warn_incomplete_defs` is enabled, requiring an explicit type hint.","error":"Need type annotation for 'variable_name'"},{"fix":"Correct any typos in the name, ensure the item is imported from the correct module, or define the name before its first use.","cause":"Mypy detects a reference to a name (variable, function, class, or module) that has not been defined or properly imported within the current scope. This often indicates a typo or a missing import.","error":"Name 'name' is not defined"},{"fix":"Ensure all code paths within the function explicitly return a value consistent with its return type annotation, or change the return type annotation to `None` if the function is not meant to return anything.","cause":"A function or method is annotated with a return type other than `None` (or implicitly expected to return a value), but there is a code path where it implicitly returns `None` (e.g., by reaching the end of the function without an explicit `return` statement).","error":"Function does not return a value"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":0,"quickstart_tag":"stale","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.5,"disk_size":"76.7M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0.5,"disk_size":"76M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"81.6M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"81M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"73.5M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.6,"disk_size":"73M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.7,"disk_size":"73.3M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.5,"disk_size":"72M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.4,"disk_size":"72.6M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.01,"mem_mb":0.4,"disk_size":"71M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","exit_code":2},{"runtime":"python:3.10-slim","exit_code":2},{"runtime":"python:3.11-alpine","exit_code":2},{"runtime":"python:3.11-slim","exit_code":2},{"runtime":"python:3.12-alpine","exit_code":2},{"runtime":"python:3.12-slim","exit_code":2},{"runtime":"python:3.13-alpine","exit_code":2},{"runtime":"python:3.13-slim","exit_code":2},{"runtime":"python:3.9-alpine","exit_code":2},{"runtime":"python:3.9-slim","exit_code":2}]}}