Mutable default argument in generic function
Detects when a generic function or method accepts a mutable object as a default parameter value, which can lead to runtime type errors.
When a generic function uses a mutable default value (like a list, dict, or set), that default is shared across all invocations of the function. This creates a scenario where the mutable object can accumulate values of different types from different calls.
What gets flagged
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 | |
Unsoundness Checker Output
| Text Only | |
|---|---|
1 2 3 4 5 6 7 8 9 | |
Mypy: No Diagnostic Emitted
Pyright: No Diagnostic Emitted
Ty: No Diagnostic Emitted
In this example, both calls to append_and_return share the same default list. If code appends to this list in different calls with different type parameters, the list ends up containing mixed types, breaking the type soundness that list[T] promises.