You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
dataclasses.replace: fall through to typeshed sig (#15962)
If the dataclasses plugin cannot determine a signature for
`dataclasses.replace`, it should not report an error. The underlying
typeshed signature will get a shot at verifying the type and reporting
an error, and it would enable the following pattern (without typing
`replace`'s kwargs, though)
_ = replace(a_or_b, x=42, y=True, w={}, init_var=42) # E: Argument "w" to "replace" of "Union[A[int], B]" has incompatible type "Dict[<nothing>, <nothing>]"; expected <nothing>
2152
2154
_ = replace(a_or_b, y=42, init_var=42) # E: Argument "y" to "replace" of "Union[A[int], B]" has incompatible type "int"; expected "bool"
2153
2155
2154
-
[builtins fixtures/dataclasses.pyi]
2156
+
[builtins fixtures/tuple.pyi]
2155
2157
2156
2158
[case testReplaceUnionOfTypeVar]
2157
2159
from typing import Generic, Union, TypeVar
@@ -2171,7 +2173,9 @@ TA = TypeVar('TA', bound=A)
2171
2173
TB = TypeVar('TB', bound=B)
2172
2174
2173
2175
def f(b_or_t: Union[TA, TB, int]) -> None:
2174
-
a2 = replace(b_or_t) # E: Argument 1 to "replace" has type "Union[TA, TB, int]" whose item "TB" is not bound to a dataclass # E: Argument 1 to "replace" has incompatible type "Union[TA, TB, int]" whose item "int" is not a dataclass
2176
+
a2 = replace(b_or_t) # E: Value of type variable "_DataclassT" of "replace" cannot be "Union[TA, TB, int]"
_ = replace(t, x=42) # E: Argument 1 to "replace" has a variable type "TInt" not bound to a dataclass
2190
+
_ = replace(t, x=42) # E: Value of type variable "_DataclassT" of "replace" cannot be "TInt"
2187
2191
2188
2192
def f2(t: TAny) -> TAny:
2189
-
return replace(t, x='spam') # E: Argument 1 to "replace" has a variable type "TAny" not bound to a dataclass
2193
+
return replace(t, x='spam') # E: Value of type variable "_DataclassT" of "replace" cannot be "TAny"
2190
2194
2191
2195
def f3(t: TNone) -> TNone:
2192
-
return replace(t, x='spam') # E: Argument 1 to "replace" has a variable type "TNone" not bound to a dataclass
2196
+
return replace(t, x='spam') # E: Value of type variable "_DataclassT" of "replace" cannot be "TNone"
2193
2197
2194
2198
def f4(t: TUnion) -> TUnion:
2195
-
return replace(t, x='spam') # E: Argument 1 to "replace" has incompatible type "TUnion" whose item "str" is not a dataclass # E: Argument 1 to "replace" has incompatible type "TUnion" whose item "int" is not a dataclass
2199
+
return replace(t, x='spam') # E: Value of type variable "_DataclassT" of "replace" cannot be "TUnion"
2200
+
2201
+
[builtins fixtures/tuple.pyi]
2196
2202
2197
2203
[case testReplaceTypeVarBound]
2198
2204
from dataclasses import dataclass, replace
@@ -2217,6 +2223,8 @@ def f(t: TA) -> TA:
2217
2223
f(A(x=42))
2218
2224
f(B(x=42))
2219
2225
2226
+
[builtins fixtures/tuple.pyi]
2227
+
2220
2228
[case testReplaceAny]
2221
2229
from dataclasses import replace
2222
2230
from typing import Any
@@ -2225,17 +2233,33 @@ a: Any
2225
2233
a2 = replace(a)
2226
2234
reveal_type(a2) # N: Revealed type is "Any"
2227
2235
2236
+
[builtins fixtures/tuple.pyi]
2237
+
2228
2238
[case testReplaceNotDataclass]
2229
2239
from dataclasses import replace
2230
2240
2231
-
replace(5) # E: Argument 1 to "replace" has incompatible type "int"; expected a dataclass
2241
+
replace(5) # E: Value of type variable "_DataclassT" of "replace" cannot be "int"
2232
2242
2233
2243
class C:
2234
2244
pass
2235
2245
2236
-
replace(C()) # E: Argument 1 to "replace" has incompatible type "C"; expected a dataclass
2246
+
replace(C()) # E: Value of type variable "_DataclassT" of "replace" cannot be "C"
2237
2247
2238
-
replace(None) # E: Argument 1 to "replace" has incompatible type "None"; expected a dataclass
2248
+
replace(None) # E: Value of type variable "_DataclassT" of "replace" cannot be "None"
2249
+
2250
+
[builtins fixtures/tuple.pyi]
2251
+
2252
+
[case testReplaceIsDataclass]
2253
+
from dataclasses import is_dataclass, replace
2254
+
2255
+
def f(x: object) -> None:
2256
+
_ = replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "object"
2257
+
if is_dataclass(x):
2258
+
_ = replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "Union[DataclassInstance, Type[DataclassInstance]]"
2259
+
if not isinstance(x, type):
2260
+
_ = replace(x)
2261
+
2262
+
[builtins fixtures/tuple.pyi]
2239
2263
2240
2264
[case testReplaceGeneric]
2241
2265
from dataclasses import dataclass, replace, InitVar
@@ -2254,6 +2278,8 @@ reveal_type(a2) # N: Revealed type is "__main__.A[builtins.int]"
2254
2278
a2 = replace(a, x='42') # E: Argument "x" to "replace" of "A[int]" has incompatible type "str"; expected "int"
2255
2279
reveal_type(a2) # N: Revealed type is "__main__.A[builtins.int]"
2256
2280
2281
+
[builtins fixtures/tuple.pyi]
2282
+
2257
2283
[case testPostInitCorrectSignature]
2258
2284
from typing import Any, Generic, TypeVar, Callable, Self
0 commit comments