Skip to content

Commit 3ae2925

Browse files
Conformance tests: Allow TD.get(known_key) to be T | None (#1690)
1 parent 1dd6ab1 commit 3ae2925

File tree

5 files changed

+15
-17
lines changed

5 files changed

+15
-17
lines changed

conformance/results/mypy/typeddicts_type_consistency.toml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ typeddicts_type_consistency.py:76: error: Incompatible types in assignment (expr
88
typeddicts_type_consistency.py:77: error: Incompatible types in assignment (expression has type "B3", variable has type "dict[str, object]") [assignment]
99
typeddicts_type_consistency.py:78: error: Incompatible types in assignment (expression has type "B3", variable has type "dict[Any, Any]") [assignment]
1010
typeddicts_type_consistency.py:82: error: Incompatible types in assignment (expression has type "B3", variable has type "Mapping[str, int]") [assignment]
11-
typeddicts_type_consistency.py:99: error: Incompatible types in assignment (expression has type "str | None", variable has type "str") [assignment]
12-
typeddicts_type_consistency.py:105: error: Incompatible types in assignment (expression has type "int | str", variable has type "int") [assignment]
13-
typeddicts_type_consistency.py:124: error: Incompatible types (expression has type "int", TypedDict item "inner_key" has type "str") [typeddict-item]
11+
typeddicts_type_consistency.py:101: error: Incompatible types in assignment (expression has type "str | None", variable has type "str") [assignment]
12+
typeddicts_type_consistency.py:107: error: Incompatible types in assignment (expression has type "int | str", variable has type "int") [assignment]
13+
typeddicts_type_consistency.py:126: error: Incompatible types (expression has type "int", TypedDict item "inner_key" has type "str") [typeddict-item]
1414
"""
15-
conformance_automated = "Fail"
15+
conformance_automated = "Pass"
1616
errors_diff = """
17-
Line 99: Unexpected errors ['typeddicts_type_consistency.py:99: error: Incompatible types in assignment (expression has type "str | None", variable has type "str") [assignment]']
18-
Line 105: Unexpected errors ['typeddicts_type_consistency.py:105: error: Incompatible types in assignment (expression has type "int | str", variable has type "int") [assignment]']
1917
"""

conformance/results/pyre/typeddicts_type_consistency.toml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ typeddicts_type_consistency.py:76:0 Incompatible variable type [9]: d1 is declar
1212
typeddicts_type_consistency.py:77:0 Incompatible variable type [9]: d2 is declared to have type `Dict[str, object]` but is used as type `B3`.
1313
typeddicts_type_consistency.py:78:0 Incompatible variable type [9]: d3 is declared to have type `Dict[typing.Any, typing.Any]` but is used as type `B3`.
1414
typeddicts_type_consistency.py:82:0 Incompatible variable type [9]: m1 is declared to have type `Mapping[str, int]` but is used as type `B3`.
15-
typeddicts_type_consistency.py:99:0 Incompatible variable type [9]: name3 is declared to have type `str` but is used as type `Optional[str]`.
16-
typeddicts_type_consistency.py:105:0 Incompatible variable type [9]: age4 is declared to have type `int` but is used as type `Union[str, int]`.
17-
typeddicts_type_consistency.py:124:41 TypedDict initialization error [55]: Expected type `str` for `Inner1` field `inner_key` but got `int`.
18-
typeddicts_type_consistency.py:150:0 Incompatible variable type [9]: o4 is declared to have type `Outer3` but is used as type `Outer2`.
15+
typeddicts_type_consistency.py:101:0 Incompatible variable type [9]: name3 is declared to have type `str` but is used as type `Optional[str]`.
16+
typeddicts_type_consistency.py:107:0 Incompatible variable type [9]: age4 is declared to have type `int` but is used as type `Union[str, int]`.
17+
typeddicts_type_consistency.py:126:41 TypedDict initialization error [55]: Expected type `str` for `Inner1` field `inner_key` but got `int`.
18+
typeddicts_type_consistency.py:152:0 Incompatible variable type [9]: o4 is declared to have type `Outer3` but is used as type `Outer2`.
1919
"""
2020
conformance_automated = "Fail"
2121
errors_diff = """
2222
Line 65: Expected 1 errors
23-
Line 99: Unexpected errors ['typeddicts_type_consistency.py:99:0 Incompatible variable type [9]: name3 is declared to have type `str` but is used as type `Optional[str]`.']
24-
Line 105: Unexpected errors ['typeddicts_type_consistency.py:105:0 Incompatible variable type [9]: age4 is declared to have type `int` but is used as type `Union[str, int]`.']
25-
Line 150: Unexpected errors ['typeddicts_type_consistency.py:150:0 Incompatible variable type [9]: o4 is declared to have type `Outer3` but is used as type `Outer2`.']
23+
Line 152: Unexpected errors ['typeddicts_type_consistency.py:152:0 Incompatible variable type [9]: o4 is declared to have type `Outer3` but is used as type `Outer2`.']
2624
"""

conformance/results/pyright/typeddicts_type_consistency.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ typeddicts_type_consistency.py:82:25 - error: Expression of type "B3" cannot be
2020
  "B3" is incompatible with "Mapping[str, int]"
2121
    Type parameter "_VT_co@Mapping" is covariant, but "object" is not a subtype of "int"
2222
      "object" is incompatible with "int" (reportAssignmentType)
23-
typeddicts_type_consistency.py:124:56 - error: Expression of type "dict[str, dict[str, dict[str, int]]]" cannot be assigned to declared type "Outer1"
23+
typeddicts_type_consistency.py:126:56 - error: Expression of type "dict[str, dict[str, dict[str, int]]]" cannot be assigned to declared type "Outer1"
2424
  "Literal[1]" is incompatible with "str" (reportAssignmentType)
2525
"""
2626
conformance_automated = "Pass"

conformance/results/pytype/typeddicts_type_consistency.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ output = """
88
File "typeddicts_type_consistency.py", line 62, in <module>: Type annotation for a3 does not match type of assignment [annotation-type-mismatch]
99
File "typeddicts_type_consistency.py", line 65, in <module>: Type annotation for b3 does not match type of assignment [annotation-type-mismatch]
1010
File "typeddicts_type_consistency.py", line 69, in <module>: Type annotation for a3_1 does not match type of assignment [annotation-type-mismatch]
11-
File "typeddicts_type_consistency.py", line 124, in <module>: Type annotation for o2 does not match type of assignment [annotation-type-mismatch]
11+
File "typeddicts_type_consistency.py", line 126, in <module>: Type annotation for o2 does not match type of assignment [annotation-type-mismatch]
1212
"""
1313
conformance_automated = "Fail"
1414
errors_diff = """

conformance/tests/typeddicts_type_consistency.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,15 @@ class B3(TypedDict):
9696

9797
name2: str | None = user2.get("name")
9898

99-
name3: str = user2.get("name")
99+
# The spec does not say whether type checkers should adjust the return type of `.get()`
100+
# to exclude `None` if it is known that the key exists. Either option is acceptable.
101+
name3: str = user2.get("name") # E?
100102

101103
age2: int = user2.get("age", 42)
102104

103105
age3: int | str = user2.get("age", "42")
104106

105-
age4: int = user2.get("age", "42")
107+
age4: int = user2.get("age", "42") # E?
106108

107109
# Test nested TypedDicts.
108110
class Inner1(TypedDict):

0 commit comments

Comments
 (0)