-
-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for stubs and address a few issues.
- Loading branch information
Showing
7 changed files
with
119 additions
and
15 deletions.
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
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
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
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
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 |
---|---|---|
@@ -1,10 +1,8 @@ | ||
from typing import Container, List, Union | ||
from typing import Container, Iterable, List, Union | ||
from . import ValidatorType | ||
|
||
def instance_of(type: type) -> ValidatorType: ... | ||
|
||
def provides(interface) -> ValidatorType: ... | ||
|
||
def optional(validator: Union[ValidatorType, List[ValidatorType]]) -> ValidatorType: ... | ||
|
||
def in_(options: Container) -> ValidatorType: ... | ||
def and_(*validators: Iterable[ValidatorType]) -> ValidatorType: ... |
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,78 @@ | ||
# this file is adapted from mypy.test.testcmdline | ||
|
||
import os | ||
import re | ||
import subprocess | ||
import sys | ||
|
||
from typing import Tuple, List, Dict, Set | ||
|
||
from mypy.test.data import parse_test_cases, DataDrivenTestCase, DataSuite | ||
from mypy.test.helpers import (assert_string_arrays_equal, | ||
normalize_error_messages) | ||
|
||
# Path to Python 3 interpreter | ||
python3_path = sys.executable | ||
test_temp_dir = 'tmp' | ||
test_file = os.path.splitext(os.path.realpath(__file__))[0] + '.test' | ||
prefix_dir = os.path.join(os.path.dirname(os.path.dirname(test_file)), 'src') | ||
|
||
|
||
class PythonEvaluationSuite(DataSuite): | ||
|
||
@classmethod | ||
def cases(cls) -> List[DataDrivenTestCase]: | ||
return parse_test_cases(test_file, | ||
_test_python_evaluation, | ||
base_path=test_temp_dir, | ||
optional_out=True, | ||
native_sep=True) | ||
|
||
def run_case(self, testcase: DataDrivenTestCase): | ||
_test_python_evaluation(testcase) | ||
|
||
|
||
def _test_python_evaluation(testcase: DataDrivenTestCase) -> None: | ||
assert testcase.old_cwd is not None, "test was not properly set up" | ||
# Write the program to a file. | ||
program = '_program.py' | ||
program_path = os.path.join(test_temp_dir, program) | ||
with open(program_path, 'w') as file: | ||
for s in testcase.input: | ||
file.write('{}\n'.format(s)) | ||
args = parse_args(testcase.input[0]) | ||
args.append('--show-traceback') | ||
# Type check the program. | ||
fixed = [python3_path, '-m', 'mypy'] | ||
process = subprocess.Popen(fixed + args, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
env={'MYPYPATH': prefix_dir}, | ||
cwd=test_temp_dir) | ||
outb = process.stdout.read() | ||
# Split output into lines. | ||
out = [s.rstrip('\n\r') for s in str(outb, 'utf8').splitlines()] | ||
# Remove temp file. | ||
os.remove(program_path) | ||
# Compare actual output to expected. | ||
out = normalize_error_messages(out) | ||
assert_string_arrays_equal(testcase.output, out, | ||
'Invalid output ({}, line {})'.format( | ||
testcase.file, testcase.line)) | ||
|
||
|
||
def parse_args(line: str) -> List[str]: | ||
"""Parse the first line of the program for the command line. | ||
This should have the form | ||
# cmd: mypy <options> | ||
For example: | ||
# cmd: mypy pkg/ | ||
""" | ||
m = re.match('# cmd: mypy (.*)$', line) | ||
if not m: | ||
return [] # No args; mypy will spit out an error. | ||
return m.group(1).split() |
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,20 @@ | ||
[case test_type_annotations] | ||
# cmd: mypy a.py | ||
[file a.py] | ||
import attr | ||
|
||
@attr.s | ||
class C(object): | ||
a : int = attr.ib() | ||
b = attr.ib(type=int) | ||
|
||
c = C() | ||
reveal_type(c.a) | ||
reveal_type(c.b) | ||
reveal_type(C.a) | ||
reveal_type(C.b) | ||
[out] | ||
a.py:9: error: Revealed type is 'builtins.int' | ||
a.py:10: error: Revealed type is 'builtins.int' | ||
a.py:11: error: Revealed type is 'builtins.int' | ||
a.py:12: error: Revealed type is 'builtins.int' |