forked from microsoft/pyright
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support PEP 712's new attribute assignment conversion (microsoft#5343)
- Loading branch information
Showing
3 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/pyright-internal/src/tests/samples/dataclassConverter2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# This sample tests assignment of dataclass fields that use | ||
# the coverter parameter described in PEP 712. | ||
|
||
from dataclasses import dataclass, field | ||
|
||
|
||
def converter_simple(s: str) -> int: ... | ||
def converter_passThru(x: str | int) -> str | int: ... | ||
|
||
@dataclass | ||
class Foo: | ||
# This should generate an error because "converter" is not an official property yet. | ||
asymmetric: int = field(converter=converter_simple) | ||
# This should generate an error because "converter" is not an official property yet. | ||
symmetric: str | int = field(converter=converter_passThru) | ||
|
||
foo = Foo("1", 1) | ||
|
||
reveal_type(foo.asymmetric, expected_text="int") | ||
foo.asymmetric = "2" | ||
reveal_type(foo.asymmetric, expected_text="int") # Asymmetric -- type narrowing should not occur | ||
# This should generate an error because only strs can be assigned to field0. | ||
foo.asymmetric = 2 | ||
|
||
reveal_type(foo.symmetric, expected_text="str | int") | ||
foo.symmetric = "1" | ||
reveal_type(foo.symmetric, expected_text="Literal['1']") # Symmetric -- type narrowing should occur | ||
|
||
|
||
reveal_type(Foo.asymmetric, expected_text="int") | ||
Foo.asymmetric = "2" | ||
reveal_type(Foo.asymmetric, expected_text="int") | ||
# This should generate an error because only strs can be assigned to field0. | ||
Foo.asymmetric = 2 | ||
|
||
reveal_type(Foo.symmetric, expected_text="str | int") | ||
Foo.symmetric = "1" | ||
reveal_type(Foo.symmetric, expected_text="Literal['1']") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters