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
_ = replace(a_or_b, x=42, y=True) # E: Missing named argument "a" for "replace" of "Union[A[int], B]"
2088
+
_ = replace(a_or_b, x=42, y=True, z='42', a=42) # E: Argument "z" to "replace" of "Union[A[int], B]" has incompatible type "str"; expected <nothing>
2089
+
_ = replace(a_or_b, x=42, y=True, w={}, a=42) # E: Argument "w" to "replace" of "Union[A[int], B]" has incompatible type "Dict[<nothing>, <nothing>]"; expected <nothing>
2090
+
2062
2091
[builtins fixtures/dataclasses.pyi]
2063
2092
2064
-
[case testReplaceTypeVar]
2093
+
[case testReplaceUnionOfTypeVar]
2094
+
# flags: --strict-optional
2095
+
from typing import Generic, Union, TypeVar
2065
2096
from dataclasses import dataclass, replace
2066
-
from typing import TypeVar
2067
2097
2068
2098
@dataclass
2069
2099
class A:
2070
2100
x: int
2101
+
y: int
2102
+
z: str
2103
+
w: dict
2104
+
2105
+
class B:
2106
+
pass
2071
2107
2072
2108
TA = TypeVar('TA', bound=A)
2109
+
TB = TypeVar('TB', bound=B)
2110
+
2111
+
def f(b_or_t: Union[TA, TB, int]) -> None:
2112
+
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
2113
+
2114
+
[case testReplaceTypeVarBoundNotDataclass]
2115
+
from dataclasses import dataclass, replace
2116
+
from typing import Union, TypeVar
2117
+
2073
2118
TInt = TypeVar('TInt', bound=int)
2074
2119
TAny = TypeVar('TAny')
2075
2120
TNone = TypeVar('TNone', bound=None)
2121
+
TUnion = TypeVar('TUnion', bound=Union[str, int])
2076
2122
2123
+
def f1(t: TInt) -> None:
2124
+
_ = replace(t, x=42) # E: Argument 1 to "replace" has a variable type "TInt" not bound to a dataclass
2077
2125
2078
-
def f(t: TA) -> TA:
2079
-
_ = replace(t, x='spam') # E: Argument "x" to "replace" of "TA" has incompatible type "str"; expected "int"
2080
-
return replace(t, x=42)
2126
+
def f2(t: TAny) -> TAny:
2127
+
return replace(t, x='spam') # E: Argument 1 to "replace" has a variable type "TAny" not bound to a dataclass
2081
2128
2129
+
def f3(t: TNone) -> TNone:
2130
+
return replace(t, x='spam') # E: Argument 1 to "replace" has a variable type "TNone" not bound to a dataclass
2082
2131
2083
-
def g(t: TInt) -> None:
2084
-
_ = replace(t, x=42) # E: Argument 1 to "replace" has variable type "TInt" not bound to a dataclass
2132
+
def f4(t: TUnion) -> TUnion:
2133
+
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
2085
2134
2135
+
[case testReplaceTypeVarBound]
2136
+
from dataclasses import dataclass, replace
2137
+
from typing import TypeVar
2086
2138
2087
-
def h(t: TAny) -> TAny:
2088
-
return replace(t, x='spam') # E: Argument 1 to "replace" has variable type "TAny" not bound to a dataclass
2139
+
@dataclass
2140
+
class A:
2141
+
x: int
2089
2142
2143
+
@dataclass
2144
+
class B(A):
2145
+
pass
2090
2146
2091
-
def q(t: TNone) -> TNone:
2092
-
return replace(t, x='spam') # E: Argument 1 to "replace" has variable type "TNone" not bound to a dataclass
2147
+
TA = TypeVar('TA', bound=A)
2093
2148
2094
-
[builtins fixtures/dataclasses.pyi]
2149
+
def f(t: TA) -> TA:
2150
+
t2 = replace(t, x=42)
2151
+
reveal_type(t2) # N: Revealed type is "TA`-1"
2152
+
_ = replace(t, x='42') # E: Argument "x" to "replace" of "TA" has incompatible type "str"; expected "int"
2153
+
return t2
2154
+
2155
+
f(A(x=42))
2156
+
f(B(x=42))
2095
2157
2096
2158
[case testReplaceAny]
2097
2159
from dataclasses import replace
@@ -2101,8 +2163,6 @@ a: Any
2101
2163
a2 = replace(a)
2102
2164
reveal_type(a2) # N: Revealed type is "Any"
2103
2165
2104
-
[builtins fixtures/dataclasses.pyi]
2105
-
2106
2166
[case testReplaceNotDataclass]
2107
2167
from dataclasses import replace
2108
2168
@@ -2125,7 +2185,6 @@ T = TypeVar('T')
2125
2185
class A(Generic[T]):
2126
2186
x: T
2127
2187
2128
-
2129
2188
a = A(x=42)
2130
2189
reveal_type(a) # N: Revealed type is "__main__.A[builtins.int]"
0 commit comments