Skip to content

Commit

Permalink
Conformance tests: Fix automated scoring for dataclasses_usage.toml (p…
Browse files Browse the repository at this point in the history
…ython#1684)

Conformance tests: Mark error for unannotated "field()" as optional, plus pytype changes

The spec doesn't explicitly mention this case. Since it produces a runtime error,
it is reasonable for type checkers to error on it, but it should not be required.

For pytype, mark a few errors as optionally appearing on a different line.
Also, change "x ** 2" to "x * x"; pytype infers "x ** 2" as being of type "float | int"
for whatever reason.
  • Loading branch information
JelleZijlstra committed Apr 11, 2024
1 parent 5207bbc commit 338cedc
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 35 deletions.
6 changes: 4 additions & 2 deletions conformance/results/mypy/dataclasses_usage.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
conformant = "Pass"
notes = """
Does not detect unannotated usage of `dataclasses.field()`.
"""
output = """
dataclasses_usage.py:36: error: Accessing "__init__" on an instance is unsound, since instance.__init__ could be from an incompatible subclass [misc]
dataclasses_usage.py:51: error: Missing positional argument "unit_price" in call to "InventoryItem" [call-arg]
Expand All @@ -13,8 +16,7 @@ dataclasses_usage.py:127: error: Too many arguments for "DC7" [call-arg]
dataclasses_usage.py:130: error: Missing positional argument "y" in call to "DC8" [call-arg]
dataclasses_usage.py:179: error: Too many arguments for "DC13" [call-arg]
"""
conformance_automated = "Fail"
conformance_automated = "Pass"
errors_diff = """
Line 228: Expected 1 errors
"""
ignore_errors = ["Accessing \"__init__\" on an instance is unsound"]
2 changes: 1 addition & 1 deletion conformance/results/mypy/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "mypy 1.9.0"
test_duration = 1.5
test_duration = 2.2
7 changes: 3 additions & 4 deletions conformance/results/pyre/dataclasses_usage.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ dataclasses_usage.py:205:0 Incompatible variable type [9]: v7 is declared to hav
"""
conformance_automated = "Fail"
errors_diff = """
Line 62: Expected 1 errors
Line 68: Expected 1 errors
Line 74: Expected 1 errors
Line 228: Expected 1 errors
Lines 59, 62: Expected error (tag 'DC1')
Lines 65, 68: Expected error (tag 'DC2')
Lines 71, 74: Expected error (tag 'DC3')
Line 73: Unexpected errors ['dataclasses_usage.py:73:4 Incompatible attribute type [8]: Attribute `a` declared in class `DC3` has type `InitVar[int]` but is used as type `int`.']
Line 116: Unexpected errors ['dataclasses_usage.py:116:0 Uninitialized attribute [13]: Attribute `y` is declared in class `DC8` to have type `int` but is never initialized.']
Line 172: Unexpected errors ['dataclasses_usage.py:172:0 Uninitialized attribute [13]: Attribute `x` is declared in class `DC13` to have type `int` but is never initialized.', 'dataclasses_usage.py:172:0 Uninitialized attribute [13]: Attribute `x_squared` is declared in class `DC13` to have type `int` but is never initialized.']
Expand Down
2 changes: 1 addition & 1 deletion conformance/results/pyre/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pyre 0.9.19"
test_duration = 3.0
test_duration = 3.7
13 changes: 1 addition & 12 deletions conformance/results/pytype/dataclasses_usage.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,8 @@ File "dataclasses_usage.py", line 84, in <module>: Function DC4.__init__ expects
File "dataclasses_usage.py", line 89, in DC5: Type annotation for a does not match type of assignment [annotation-type-mismatch]
File "dataclasses_usage.py", line 127, in <module>: Function DC7.__init__ expects 2 arg(s), got 3 [wrong-arg-count]
File "dataclasses_usage.py", line 130, in <module>: Missing parameter 'y' in call to function DC8.__init__ [missing-parameter]
File "dataclasses_usage.py", line 152, in __init__: Type annotation for x_squared does not match type of assignment [annotation-type-mismatch]
File "dataclasses_usage.py", line 165, in __init__: Type annotation for x_squared does not match type of assignment [annotation-type-mismatch]
File "dataclasses_usage.py", line 179, in <module>: Function DC13.__init__ expects 1 arg(s), got 2 [wrong-arg-count]
"""
conformance_automated = "Fail"
conformance_automated = "Pass"
errors_diff = """
Line 62: Expected 1 errors
Line 68: Expected 1 errors
Line 74: Expected 1 errors
Line 228: Expected 1 errors
Line 59: Unexpected errors ['File "dataclasses_usage.py", line 59, in <module>: In method __init__, non-default argument b follows default argument [invalid-function-definition]']
Line 65: Unexpected errors ['File "dataclasses_usage.py", line 65, in <module>: In method __init__, non-default argument b follows default argument [invalid-function-definition]']
Line 71: Unexpected errors ['File "dataclasses_usage.py", line 71, in <module>: In method __init__, non-default argument b follows default argument [invalid-function-definition]']
Line 152: Unexpected errors ['File "dataclasses_usage.py", line 152, in __init__: Type annotation for x_squared does not match type of assignment [annotation-type-mismatch]']
Line 165: Unexpected errors ['File "dataclasses_usage.py", line 165, in __init__: Type annotation for x_squared does not match type of assignment [annotation-type-mismatch]']
"""
2 changes: 1 addition & 1 deletion conformance/results/pytype/version.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = "pytype 2024.03.19"
test_duration = 44.6
test_duration = 44.2
8 changes: 4 additions & 4 deletions conformance/results/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,16 @@ <h3>Python Type System Conformance Test Results</h3>
<div class="table_container"><table><tbody>
<tr><th class="col1">&nbsp;</th>
<th class='tc-header'><div class='tc-name'>mypy 1.9.0</div>
<div class='tc-time'>1.5sec</div>
<div class='tc-time'>2.2sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pyright 1.1.357</div>
<div class='tc-time'>1.6sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pyre 0.9.19</div>
<div class='tc-time'>3.0sec</div>
<div class='tc-time'>3.7sec</div>
</th>
<th class='tc-header'><div class='tc-name'>pytype 2024.03.19</div>
<div class='tc-time'>44.6sec</div>
<div class='tc-time'>44.2sec</div>
</th>
</tr>
<tr><th class="column" colspan="5">
Expand Down Expand Up @@ -703,7 +703,7 @@ <h3>Python Type System Conformance Test Results</h3>
<th class="column col2 not-conformant"><div class="hover-text">Unsupported<span class="tooltip-text" id="bottom"><p>Does not understand @dataclass_transform.</p></span></div></th>
</tr>
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataclasses_usage</th>
<th class="column col2 conformant">Pass</th>
<th class="column col2 conformant"><div class="hover-text">Pass*<span class="tooltip-text" id="bottom"><p>Does not detect unannotated usage of `dataclasses.field()`.</p></span></div></th>
<th class="column col2 conformant">Pass</th>
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not report error when field with no default follows field with default.</p><p>Incorrectly reports error with InitVar that has default value.</p></span></div></th>
<th class="column col2 conformant">Pass</th>
Expand Down
20 changes: 10 additions & 10 deletions conformance/tests/dataclasses_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,22 @@ def __call__(
# > TypeError will be raised if a field without a default value follows a
# > field with a default value. This is true either when this occurs in a
# > single class, or as a result of class inheritance.
@dataclass
@dataclass # E[DC1]
class DC1:
a: int = 0
b: int # E: field with no default cannot follow field with default.
b: int # E[DC1]: field with no default cannot follow field with default.


@dataclass
@dataclass # E[DC2]
class DC2:
a: int = field(default=1)
b: int # E: field with no default cannot follow field with default.
b: int # E[DC2]: field with no default cannot follow field with default.


@dataclass
@dataclass # E[DC3]
class DC3:
a: InitVar[int] = 0
b: int # E: field with no default cannot follow field with default.
b: int # E[DC3]: field with no default cannot follow field with default.


@dataclass
Expand Down Expand Up @@ -149,7 +149,7 @@ class DC11:

def __init__(self, x: int):
self.x = x
self.x_squared = x**2
self.x_squared = x * x


DC11(3)
Expand All @@ -162,7 +162,7 @@ class DC12:

def __init__(self, x: int):
self.x = x
self.x_squared = x**2
self.x_squared = x * x


DC12(3)
Expand Down Expand Up @@ -223,6 +223,6 @@ class DC17(DC16[str]):
@dataclass
class DC18:
x: int = field()
# This should generate an error because an unannotated field
# This may generate a type checker error because an unannotated field
# will result in a runtime exception.
y = field() # E
y = field() # E?

0 comments on commit 338cedc

Please sign in to comment.