reportUninitializedInstanceVariable.md
April 8, 2026 · View on GitHub
Overview
reportUninitializedInstanceVariable is a diagnostic in Pylance and Pyright that warns when an instance variable may be used before it is initialized. This helps catch potential runtime errors and ensures that all instance variables are properly set before use.
Representative Issues
- #2184: When using
Optionalreturn types, ensure functions include explicit return statements for all possible outcomes. - #4163: Ensure consistency in the use of type stubs between Pyright's CLI and Pylance settings.
- #5200: Provide a configuration setting to allow users to customize diagnostic rule severities based on the type checking mode.
- #2721: Use
isinstancefor clearer type narrowing with coroutines. - #4367: Ensure TOML comments use correct line endings.
- #6376: Consider adding diagnostic rules to your configuration file instead of using the "all" default.
- #7192: Avoid redeclaring variables with the same type to prevent shadowing.
- #7193: After an
isinstancecheck, set the variable to the correct type to avoid false positives.
Examples
Error:
class User:
name: str
email: str
def __init__(self, name: str):
self.name = name
# email is declared but never set in __init__
Fix — initialize all declared variables:
class User:
name: str
email: str
def __init__(self, name: str, email: str = ""):
self.name = name
self.email = email
Or use Optional if the variable may not always be set:
class User:
name: str
email: str | None
def __init__(self, name: str):
self.name = name
self.email = None
Common Fixes & Workarounds
- Always initialize instance variables in the class constructor (
__init__). - Use default values for instance variables when possible.
- Review all code paths to ensure variables are set before use.
- Review the Pyright configuration documentation for options to adjust or suppress this diagnostic if needed.
See Also
python.analysis.diagnosticSeverityOverrides— adjust or suppress this diagnosticpython.analysis.typeCheckingMode— controls which diagnostics are enabled by default