reportInvalidStubStatement.md
April 8, 2026 · View on GitHub
Overview
reportInvalidStubStatement flags statements in Python type stub (.pyi) files that are not valid according to the stub file specification. This diagnostic helps ensure that stubs are syntactically correct and compatible with static type checkers.
Representative Issues
- #3102: Ensure that default argument types in functions match the annotated parameter types to avoid runtime errors and type checking issues.
- #4163: Ensure consistency in the use of type stubs between Pyright's CLI and Pylance settings, especially with
useLibraryCodeForTypes. - #5200: Provide a configuration setting to allow users to customize diagnostic rule severities based on the type checking mode.
- #3332: Always specify the module path when referencing submodules within larger modules to avoid syntax and import errors.
Examples
Error:
# my_module.pyi (stub file)
print("loading") # Executable statement not allowed in stubs
def greet(name: str) -> str: ...
Fix — keep only declarations in stubs:
# my_module.pyi
def greet(name: str) -> str: ...
Stubs should contain only declarations (functions, classes, variables with type annotations) and ... as placeholders.
Common Fixes & Workarounds
- Only use valid stub syntax in
.pyifiles (e.g., type annotations, function/class definitions, and docstrings). - Avoid executable statements or runtime logic in stub files.
- Ensure all imports and references in stubs use the correct module paths.
- Match default argument types to their annotated parameter types.
- Refer to the Pyright configuration documentation for details on configuring this diagnostic.
- Suppress this diagnostic with
# pyright: reportInvalidStubStatement=falseif you have a special case.
See Also
python.analysis.diagnosticSeverityOverrides— adjust or suppress this diagnosticpython.analysis.typeCheckingMode— controls which diagnostics are enabled by default