reportCallInDefaultInitializer.md
April 8, 2026 · View on GitHub
Overview
reportCallInDefaultInitializer is a Pylance and Pyright diagnostic that warns when a function call is used as a default value for a function parameter. This can lead to unexpected behavior because the call is evaluated only once, not each time the function is called.
Representative 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, improving the granularity of error reporting.
- #5887: Ensure that classes inheriting from abstract base classes implement all abstract methods.
- #715: When using generic types like dictionaries in Python, prefer the
typing.Dictsyntax over the olderdict[t, t]syntax to ensure compatibility across different Python versions. - #2721: When using
iscoroutineor similar type guards to conditionally check and assign types, consider usingisinstancefor clearer control over the type narrowing process. - #4367: Ensure that comments in TOML files use the correct line endings and do not contain unsupported control characters to avoid parse errors.
- #5411: Always use the explicit
AsyncGeneratortype annotation for async generator functions to avoid inconsistencies with inferred types. - #5412: When using
type(self)as a constructor in a generic class, ensure to dynamically generate the correct specialized type for subclasses. - #7192: Avoid redeclaring variables with the same type to prevent shadowing and ensure clear variable identification.
- #7193: After an
isinstancecheck with a specific type, the variable should be explicitly set to that type or a more specific type to avoid false positive errors in static analysis tools.
Examples
def func(
b=dict(), # Warning: Call expression not allowed in default value
c=max(3, 4), # Warning: Call expression not allowed in default value
d=[], # Warning: Mutable default value
):
pass
Fix — use None and initialize inside the function:
def func(
b: dict | None = None,
c: int | None = None,
d: list | None = None,
):
b = b if b is not None else {}
c = c if c is not None else max(3, 4)
d = d if d is not None else []
Common Fixes & Workarounds
- Avoid using function calls as default parameter values; use
Noneand set the value inside the function if needed. - Use explicit type annotations for clarity and to help static analysis tools.
- Ensure all abstract methods are implemented in subclasses.
- Prefer
typing.Dictoverdict[t, t]for type compatibility. - Use
isinstancefor type checks and type narrowing. - Use explicit type annotations for async generators and other advanced types.
- Refer to the Pyright configuration documentation for details on configuring or disabling this diagnostic.
See Also
python.analysis.diagnosticSeverityOverrides— adjust or suppress this diagnosticpython.analysis.typeCheckingMode— controls which diagnostics are enabled by default