Skip to content

Commit 2dc3d22

Browse files
elazargJukkaL
authored andcommitted
Annotate functions in tests (#4434)
Seems like some functions are accidentally unannotated. For example, testInplaceSetitem should have actually failed since the list fixture does not declare addition for int; but it doesn't, since mypy seems to silently infer Any for the attribute.
1 parent c2252da commit 2dc3d22

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

test-data/unit/check-classes.test

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,25 @@ A().f = None # E: Cannot assign to a method
121121

122122
[case testReferToInvalidAttribute]
123123

124+
class A:
125+
def __init__(self) -> None:
126+
self.x = object()
127+
a: A
128+
a.y # E: "A" has no attribute "y"
129+
a.y = object() # E: "A" has no attribute "y"
130+
a.x
131+
a.x = object()
132+
133+
[case testReferToInvalidAttributeUnannotatedInit]
124134
class A:
125135
def __init__(self):
126136
self.x = object()
127-
a = None # type: A
128-
a.y
129-
a.y = object()
137+
138+
a: A
139+
a.y # E: "A" has no attribute "y"
140+
a.y = object() # E: "A" has no attribute "y"
130141
a.x
131142
a.x = object()
132-
[out]
133-
main:6: error: "A" has no attribute "y"
134-
main:7: error: "A" has no attribute "y"
135143

136144
[case testArgumentTypeInference]
137145

test-data/unit/check-inference.test

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,10 +1403,15 @@ b[{}] = 1
14031403

14041404
[case testInferDictInitializedToEmptyAndUpdatedFromMethod]
14051405
map = {}
1406+
def add() -> None:
1407+
map[1] = 2
1408+
[builtins fixtures/dict.pyi]
1409+
1410+
[case testInferDictInitializedToEmptyAndUpdatedFromMethodUnannotated]
1411+
map = {}
14061412
def add():
14071413
map[1] = 2
14081414
[builtins fixtures/dict.pyi]
1409-
[out]
14101415

14111416
[case testSpecialCaseEmptyListInitialization]
14121417
def f(blocks: Any): # E: Name 'Any' is not defined
@@ -1959,7 +1964,7 @@ class C:
19591964

19601965
if bool():
19611966
f()
1962-
1 + '' # E: Unsupported left operand type for + ("int")
1967+
1() # E: "int" not callable
19631968
[builtins fixtures/list.pyi]
19641969
[out]
19651970

@@ -1976,7 +1981,7 @@ class C:
19761981

19771982
if bool():
19781983
f()
1979-
1 + '' # E: Unsupported left operand type for + ("int")
1984+
1() # E: "int" not callable
19801985
[builtins fixtures/list.pyi]
19811986
[out]
19821987

@@ -1992,6 +1997,6 @@ class C:
19921997

19931998
if bool():
19941999
f([])
1995-
1 + '' # E: Unsupported left operand type for + ("int")
2000+
1() # E: "int" not callable
19962001
[builtins fixtures/list.pyi]
19972002
[out]

test-data/unit/check-modules.test

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,9 +1312,14 @@ class A:
13121312
def f(self) -> str: return 'foo'
13131313
class B(A):
13141314
def f(self) -> str: return self.x
1315-
def initialize(self): self.x = 'bar'
1316-
[out]
1315+
def initialize(self) -> None: self.x = 'bar'
13171316

1317+
[case testDeferredClassContextUnannotated]
1318+
class A:
1319+
def f(self) -> str: return 'foo'
1320+
class B(A):
1321+
def f(self) -> str: return self.x
1322+
def initialize(self): self.x = 'bar'
13181323

13191324
-- Scripts and __main__
13201325

test-data/unit/check-statements.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,12 @@ a @= 1 # E: Argument 1 to "__imatmul__" of "A" has incompatible type "int"; exp
353353

354354
[case testInplaceSetitem]
355355
class A(object):
356-
def __init__(self):
357-
self.a = 0
356+
def __init__(self) -> None:
357+
self.a = [1]
358358

359359
def __iadd__(self, a):
360360
# type: (int) -> A
361-
self.a += 1
361+
self.a += [2]
362362
return self
363363

364364
a = A()

0 commit comments

Comments
 (0)