Skip to content

Added check for mutable values of type list, dict, or set when used a…#10557

Merged
erictraut merged 1 commit intomainfrom
issue-10553
Jun 8, 2025
Merged

Added check for mutable values of type list, dict, or set when used a…#10557
erictraut merged 1 commit intomainfrom
issue-10553

Conversation

@erictraut
Copy link
Collaborator

…s default values for a dataclass field. These result in TypeErrors at runtime. This addresses #10553.

…s default values for a dataclass field. These result in TypeErrors at runtime. This addresses #10553.
@github-actions
Copy link
Contributor

github-actions bot commented Jun 8, 2025

Diff from mypy_primer, showing the effect of this PR on open source code:

trio (https://github.com/python-trio/trio)
+   ...:175:25 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:320:41 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:59:25 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:64:47 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:33:41 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:263:51 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:1516:39 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:1797:24 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:19:45 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:35:29 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:14:46 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:28:50 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:220:41 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:221:49 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:37:47 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
- 1491 errors, 13 warnings, 0 informations 
+ 1506 errors, 13 warnings, 0 informations 

ibis (https://github.com/ibis-project/ibis)
+   ...:53:46 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
- 7223 errors, 240 warnings, 0 informations 
+ 7224 errors, 240 warnings, 0 informations 

sympy (https://github.com/sympy/sympy)
-   ...:268:22 - error: Argument of type "Generator[tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Basic | int | None, None, None]" cannot be assigned to parameter "iterable" of type "Iterable[_SupportsSumNoDefaultT@sum]" in function "sum"
+   ...:268:22 - error: Argument of type "Generator[tuple[Unknown, ...] | Unknown | Sum | Expr | Any | Zero | NaN | Piecewise | Basic | int | None, None, None]" cannot be assigned to parameter "iterable" of type "Iterable[_SupportsSumNoDefaultT@sum]" in function "sum"
-     "Generator[tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Basic | int | None, None, None]" is not assignable to "Iterable[_SupportsSumNoDefaultT@sum]"
+     "Generator[tuple[Unknown, ...] | Unknown | Sum | Expr | Any | Zero | NaN | Piecewise | Basic | int | None, None, None]" is not assignable to "Iterable[_SupportsSumNoDefaultT@sum]"
-       Type parameter "_T_co@Iterable" is covariant, but "tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Basic | int | None" is not a subtype of "_SupportsSumNoDefaultT@sum"
+       Type parameter "_T_co@Iterable" is covariant, but "tuple[Unknown, ...] | Unknown | Sum | Expr | Any | Zero | NaN | Piecewise | Basic | int | None" is not a subtype of "_SupportsSumNoDefaultT@sum"
-         Type "tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Basic | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
+         Type "tuple[Unknown, ...] | Unknown | Sum | Expr | Any | Zero | NaN | Piecewise | Basic | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
-           Type "tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Basic | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
+           Type "tuple[Unknown, ...] | Unknown | Sum | Expr | Any | Zero | NaN | Piecewise | Basic | int | None" is not assignable to type "_SupportsSumWithNoDefaultGiven"
-   ...:473:25 - error: Argument of type "Expr | Lambda | Zero | One | Integral | Unknown | Probability | tuple[Unknown, ...] | Sum | NaN | Piecewise | Basic | NegativeOne | Integer | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | int" cannot be assigned to parameter "args" of type "Expr | complex" in function "__new__"
+   ...:473:25 - error: Argument of type "Expr | Lambda | Zero | One | Integral | Unknown | Probability | tuple[Unknown, ...] | Sum | Any | NaN | Piecewise | Basic | NegativeOne | Integer | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | int" cannot be assigned to parameter "args" of type "Expr | complex" in function "__new__"
-     Type "Expr | Lambda | Zero | One | Integral | Unknown | Probability | tuple[Unknown, ...] | Sum | NaN | Piecewise | Basic | NegativeOne | Integer | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | int" is not assignable to type "Expr | complex"
+     Type "Expr | Lambda | Zero | One | Integral | Unknown | Probability | tuple[Unknown, ...] | Sum | Any | NaN | Piecewise | Basic | NegativeOne | Integer | ComplexInfinity | Rational | Infinity | NegativeInfinity | Float | Number | int" is not assignable to type "Expr | complex"
-   ...:1839:25 - error: Argument of type "tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Basic | Integral | Any | int | None" cannot be assigned to parameter "args" of type "Expr | complex" in function "__new__"
+   ...:1839:25 - error: Argument of type "tuple[Unknown, ...] | Unknown | Sum | Expr | Any | Zero | NaN | Piecewise | Basic | Integral | int | None" cannot be assigned to parameter "args" of type "Expr | complex" in function "__new__"
-     Type "tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Basic | Integral | Any | int | None" is not assignable to type "Expr | complex"
+     Type "tuple[Unknown, ...] | Unknown | Sum | Expr | Any | Zero | NaN | Piecewise | Basic | Integral | int | None" is not assignable to type "Expr | complex"
-     Return type mismatch: base method returns type "Self@Basic", override returns type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Integral | Any | Self@Expectation | Literal[0] | None"
+     Return type mismatch: base method returns type "Self@Basic", override returns type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Any | Zero | NaN | Piecewise | Integral | Self@Expectation | Literal[0] | None"
-       Type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Integral | Any | Expectation* | Literal[0] | None" is not assignable to type "Basic"
+       Type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Any | Zero | NaN | Piecewise | Integral | Expectation* | Literal[0] | None" is not assignable to type "Basic"
-     Return type mismatch: base method returns type "Basic", override returns type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Integral | Any | Literal[0] | None"
+     Return type mismatch: base method returns type "Basic", override returns type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Any | Zero | NaN | Piecewise | Integral | Literal[0] | None"
-       Type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Integral | Any | Literal[0] | None" is not assignable to type "Basic"
+       Type "Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Any | Zero | NaN | Piecewise | Integral | Literal[0] | None" is not assignable to type "Basic"
-   ...:366:12 - error: Operator "-" not supported for types "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0] | None" and "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0] | None"
+   ...:366:12 - error: Operator "-" not supported for types "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Any | Zero | NaN | Piecewise | Integral | ExpectationMatrix | Literal[0] | None" and "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Any | Zero | NaN | Piecewise | Integral | ExpectationMatrix | Literal[0] | None"
-   ...:414:21 - error: Argument of type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0] | None" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
+   ...:414:21 - error: Argument of type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Any | Zero | NaN | Piecewise | Integral | ExpectationMatrix | Literal[0] | None" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
-     Type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0] | None" is not assignable to type "Basic"
+     Type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Any | Zero | NaN | Piecewise | Integral | ExpectationMatrix | Literal[0] | None" is not assignable to type "Basic"
-   ...:1004:21 - error: Argument of type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0] | None" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
+   ...:1004:21 - error: Argument of type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Any | Zero | NaN | Piecewise | Integral | ExpectationMatrix | Literal[0] | None" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
-     Type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0] | None" is not assignable to type "Basic"
+     Type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Any | Zero | NaN | Piecewise | Integral | ExpectationMatrix | Literal[0] | None" is not assignable to type "Basic"
-   ...:1059:21 - error: Argument of type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0] | None" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
+   ...:1059:21 - error: Argument of type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Any | Zero | NaN | Piecewise | Integral | ExpectationMatrix | Literal[0] | None" cannot be assigned to parameter "expr" of type "Basic" in function "simplify"
-     Type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0] | None" is not assignable to type "Basic"
+     Type "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Any | Zero | NaN | Piecewise | Integral | ExpectationMatrix | Literal[0] | None" is not assignable to type "Basic"
-   ...:1296:61 - error: Operator "**" not supported for types "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Zero | NaN | Piecewise | Integral | Any | ExpectationMatrix | Literal[0] | None" and "Literal[2]"
+   ...:1296:61 - error: Operator "**" not supported for types "RandomSymbol | Basic | Expectation | Expr | tuple[Unknown, ...] | Unknown | Sum | Any | Zero | NaN | Piecewise | Integral | ExpectationMatrix | Literal[0] | None" and "Literal[2]"

... (truncated 90 lines) ...

pyodide (https://github.com/pyodide/pyodide)
+   ...:13:28 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:14:22 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:25:26 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:26:37 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:91:39 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:94:36 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:124:22 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:125:23 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:126:29 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:127:29 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:132:26 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:145:37 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
- 803 errors, 15 warnings, 0 informations 
+ 815 errors, 15 warnings, 0 informations 

bokeh (https://github.com/bokeh/bokeh)
+ ...
+   ...:50:47 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:51:47 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:71:45 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:72:45 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
- 1047 errors, 51 warnings, 0 informations 
+ 1051 errors, 51 warnings, 0 informations 

scikit-build-core (https://github.com/scikit-build/scikit-build-core)
+ ...
+   ...:28:43 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
- 20 errors, 22 warnings, 0 informations 
+ 21 errors, 22 warnings, 0 informations 

rich (https://github.com/Textualize/rich)
+ ...
+   ...:28:33 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
- 358 errors, 42 warnings, 0 informations 
+ 359 errors, 42 warnings, 0 informations 

mitmproxy (https://github.com/mitmproxy/mitmproxy)
+   ...:66:53 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
- 2595 errors, 345 warnings, 0 informations 
+ 2596 errors, 345 warnings, 0 informations 

prefect (https://github.com/PrefectHQ/prefect)
+   ...:33:41 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:109:41 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:131:41 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:358:17 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:70:41 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:203:41 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:225:41 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
- 5112 errors, 162 warnings, 0 informations 
+ 5119 errors, 162 warnings, 0 informations 

hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
+   ...:216:24 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
- 9 errors, 0 warnings, 183 informations 
+ 10 errors, 0 warnings, 183 informations 

attrs (https://github.com/python-attrs/attrs)
+   ...:66:25 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:110:35 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:410:37 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:413:36 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:414:36 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:420:40 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+   ...:421:40 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
- 924 errors, 43 warnings, 5 informations 
+ 931 errors, 43 warnings, 5 informations 

schemathesis (https://github.com/schemathesis/schemathesis)
+   ...:112:51 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
- 201 errors, 110 warnings, 0 informations 
+ 202 errors, 110 warnings, 0 informations 

materialize (https://github.com/MaterializeInc/materialize)
+   ...:303:28 - error: Default value for dataclass field cannot be a list, dict, or set instance (reportGeneralTypeIssues)
+ ...
- 15 errors, 110 warnings, 0 informations 
+ 16 errors, 110 warnings, 0 informations 

@erictraut erictraut merged commit 9148e3d into main Jun 8, 2025
18 checks passed
@erictraut erictraut deleted the issue-10553 branch June 8, 2025 16:16
erictraut added a commit that referenced this pull request Jun 10, 2025
…n used as default values for a dataclass field. These result in TypeErrors at runtime. This addresses #10553. (#10557)"

This reverts commit 9148e3d.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant