-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dataclasses.replace: fall through to typeshed sig #15962
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2106,6 +2106,8 @@ a2 = replace(a, x='42', q=42) # E: Argument "x" to "replace" of "A" has incompa | |
a2 = replace(a, q='42') # E: Argument "q" to "replace" of "A" has incompatible type "str"; expected "int" | ||
reveal_type(a2) # N: Revealed type is "__main__.A" | ||
|
||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testReplaceUnion] | ||
from typing import Generic, Union, TypeVar | ||
from dataclasses import dataclass, replace, InitVar | ||
|
@@ -2135,7 +2137,7 @@ _ = replace(a_or_b, x=42, y=True, z='42', init_var=42) # E: Argument "z" to "re | |
_ = 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> | ||
_ = 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" | ||
|
||
[builtins fixtures/dataclasses.pyi] | ||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testReplaceUnionOfTypeVar] | ||
from typing import Generic, Union, TypeVar | ||
|
@@ -2155,7 +2157,9 @@ TA = TypeVar('TA', bound=A) | |
TB = TypeVar('TB', bound=B) | ||
|
||
def f(b_or_t: Union[TA, TB, int]) -> None: | ||
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 | ||
a2 = replace(b_or_t) # E: Value of type variable "_DataclassT" of "replace" cannot be "Union[TA, TB, int]" | ||
|
||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testReplaceTypeVarBoundNotDataclass] | ||
from dataclasses import dataclass, replace | ||
|
@@ -2167,16 +2171,18 @@ TNone = TypeVar('TNone', bound=None) | |
TUnion = TypeVar('TUnion', bound=Union[str, int]) | ||
|
||
def f1(t: TInt) -> None: | ||
_ = replace(t, x=42) # E: Argument 1 to "replace" has a variable type "TInt" not bound to a dataclass | ||
_ = replace(t, x=42) # E: Value of type variable "_DataclassT" of "replace" cannot be "TInt" | ||
|
||
def f2(t: TAny) -> TAny: | ||
return replace(t, x='spam') # E: Argument 1 to "replace" has a variable type "TAny" not bound to a dataclass | ||
return replace(t, x='spam') # E: Value of type variable "_DataclassT" of "replace" cannot be "TAny" | ||
|
||
def f3(t: TNone) -> TNone: | ||
return replace(t, x='spam') # E: Argument 1 to "replace" has a variable type "TNone" not bound to a dataclass | ||
return replace(t, x='spam') # E: Value of type variable "_DataclassT" of "replace" cannot be "TNone" | ||
|
||
def f4(t: TUnion) -> TUnion: | ||
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 | ||
return replace(t, x='spam') # E: Value of type variable "_DataclassT" of "replace" cannot be "TUnion" | ||
|
||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testReplaceTypeVarBound] | ||
from dataclasses import dataclass, replace | ||
|
@@ -2201,6 +2207,8 @@ def f(t: TA) -> TA: | |
f(A(x=42)) | ||
f(B(x=42)) | ||
|
||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testReplaceAny] | ||
from dataclasses import replace | ||
from typing import Any | ||
|
@@ -2209,17 +2217,33 @@ a: Any | |
a2 = replace(a) | ||
reveal_type(a2) # N: Revealed type is "Any" | ||
|
||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testReplaceNotDataclass] | ||
from dataclasses import replace | ||
|
||
replace(5) # E: Argument 1 to "replace" has incompatible type "int"; expected a dataclass | ||
replace(5) # E: Value of type variable "_DataclassT" of "replace" cannot be "int" | ||
|
||
class C: | ||
pass | ||
|
||
replace(C()) # E: Argument 1 to "replace" has incompatible type "C"; expected a dataclass | ||
replace(C()) # E: Value of type variable "_DataclassT" of "replace" cannot be "C" | ||
|
||
replace(None) # E: Argument 1 to "replace" has incompatible type "None"; expected a dataclass | ||
replace(None) # E: Value of type variable "_DataclassT" of "replace" cannot be "None" | ||
|
||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testReplaceIsDataclass] | ||
from dataclasses import is_dataclass, replace | ||
|
||
def f(x: object) -> None: | ||
# error before type-guard | ||
y = replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "object" | ||
# no error after type-guard | ||
if is_dataclass(x) and not isinstance(x, type): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to test just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's because I'll improve the test to clarify that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
y = replace(x) | ||
|
||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testReplaceGeneric] | ||
from dataclasses import dataclass, replace, InitVar | ||
|
@@ -2238,6 +2262,8 @@ reveal_type(a2) # N: Revealed type is "__main__.A[builtins.int]" | |
a2 = replace(a, x='42') # E: Argument "x" to "replace" of "A[int]" has incompatible type "str"; expected "int" | ||
reveal_type(a2) # N: Revealed type is "__main__.A[builtins.int]" | ||
|
||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testPostInitCorrectSignature] | ||
from typing import Any, Generic, TypeVar, Callable, Self | ||
from dataclasses import dataclass, InitVar | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this can potentially introduce some new weak spots, but on other hand it can eventually work :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a false positive but it's due to #15974.