Skip to content

Commit 7bf6646

Browse files
committed
Add unit tests
1 parent 9e9fa14 commit 7bf6646

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

mypy/test/data/check-flags.test

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# test cases for --disallow-untyped-defs
2+
3+
[case testUnannotatedFunction]
4+
# flags: disallow-untyped-defs
5+
def f(x): pass
6+
[out]
7+
main: note: In function "f":
8+
main:2: error: Function is missing a type annotation
9+
10+
[case testUnannotatedArgument]
11+
# flags: disallow-untyped-defs
12+
def f(x) -> int: pass
13+
[out]
14+
main: note: In function "f":
15+
main:2: error: Function is missing a type annotation for one or more arguments
16+
17+
[case testNoArgumentFunction]
18+
# flags: disallow-untyped-defs
19+
def f() -> int: pass
20+
[out]
21+
22+
[case testUnannotatedReturn]
23+
# flags: disallow-untyped-defs
24+
def f(x: int): pass
25+
[out]
26+
main: note: In function "f":
27+
main:2: error: Function is missing a return type annotation
28+
29+
[case testLambda]
30+
# flags: disallow-untyped-defs
31+
lambda x: x
32+
[out]

mypy/test/data/lib-stub/builtins.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
class Any: pass
2+
13
class object:
24
def __init__(self) -> None: pass
35

46
class type:
5-
def __init__(self, x) -> None: pass
7+
def __init__(self, x: Any) -> None: pass
68

79
# These are provided here for convenience.
810
class int: pass

mypy/test/testcheck.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import re
55
import sys
66

7-
from typing import Tuple
7+
from typing import Tuple, List
88

99
from mypy import build
1010
import mypy.myunit # for mutable globals (ick!)
@@ -53,6 +53,7 @@
5353
'check-ignore.test',
5454
'check-type-promotion.test',
5555
'check-semanal-error.test',
56+
'check-flags.test',
5657
]
5758

5859

@@ -69,12 +70,13 @@ def run_test(self, testcase):
6970
pyversion = testcase_pyversion(testcase.file, testcase.name)
7071
program_text = '\n'.join(testcase.input)
7172
module_name, program_name, program_text = self.parse_options(program_text)
73+
flags = self.parse_flags(program_text)
7274
source = BuildSource(program_name, module_name, program_text)
7375
try:
7476
build.build(target=build.TYPE_CHECK,
7577
sources=[source],
7678
pyversion=pyversion,
77-
flags=[build.TEST_BUILTINS],
79+
flags=flags + [build.TEST_BUILTINS],
7880
alt_lib_path=test_temp_dir)
7981
except CompileError as e:
8082
a = normalize_error_messages(e.messages)
@@ -109,3 +111,10 @@ def parse_options(self, program_text: str) -> Tuple[str, str, str]:
109111
return m.group(1), path, program_text
110112
else:
111113
return '__main__', 'main', program_text
114+
115+
def parse_flags(self, program_text: str) -> List[str]:
116+
m = re.search('# flags: (.*)$', program_text, flags=re.MULTILINE)
117+
if m:
118+
return m.group(1).split()
119+
else:
120+
return []

0 commit comments

Comments
 (0)