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 Optional return 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 isinstance for 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 isinstance check, 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

  1. Always initialize instance variables in the class constructor (__init__).
  2. Use default values for instance variables when possible.
  3. Review all code paths to ensure variables are set before use.
  4. Review the Pyright configuration documentation for options to adjust or suppress this diagnostic if needed.

See Also