|
| 1 | +from queue import Queue |
| 2 | +from textwrap import dedent |
| 3 | + |
| 4 | +from bears.python.MypyBear import MypyBear |
| 5 | +from tests.BearTestHelper import generate_skip_decorator |
| 6 | +from tests.LocalBearTestHelper import LocalBearTestHelper |
| 7 | +from coalib.settings.Section import Section |
| 8 | +from coalib.settings.Setting import Setting |
| 9 | +from coalib.results.Result import Result |
| 10 | +from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY |
| 11 | +from coalib.misc.ContextManagers import prepare_file |
| 12 | + |
| 13 | + |
| 14 | +@generate_skip_decorator(MypyBear) |
| 15 | +class MypyBearTest(LocalBearTestHelper): |
| 16 | + |
| 17 | + def setUp(self): |
| 18 | + self.section = Section('name') |
| 19 | + self.queue = Queue() |
| 20 | + self.uut = MypyBear(self.section, self.queue) |
| 21 | + |
| 22 | + def test_variable(self): |
| 23 | + self.check_validity(self.uut, |
| 24 | + ["a = 1 # type: int"], valid=True) |
| 25 | + self.check_validity(self.uut, |
| 26 | + ["a = 'abc' # type: int"], valid=False) |
| 27 | + |
| 28 | + def test_call_sum(self): |
| 29 | + self.check_validity(self.uut, |
| 30 | + ["sum([1, 2, 3])"], valid=True) |
| 31 | + self.check_validity(self.uut, |
| 32 | + ["sum(1, 2, 3)"], valid=False) |
| 33 | + |
| 34 | + def test_py2(self): |
| 35 | + self.check_validity(self.uut, |
| 36 | + ["print(123)"], valid=True) |
| 37 | + self.check_validity(self.uut, |
| 38 | + ["print 123"], valid=False) |
| 39 | + self.section.append(Setting('language', 'Python 2')) |
| 40 | + self.check_validity(self.uut, |
| 41 | + ["print 123"], valid=True) |
| 42 | + |
| 43 | + def test_py2_version(self): |
| 44 | + self.check_validity(self.uut, |
| 45 | + ["print(123)"], valid=True) |
| 46 | + self.check_validity(self.uut, |
| 47 | + ["print 123"], valid=False) |
| 48 | + self.section.append(Setting('python_version', '2.7')) |
| 49 | + self.check_validity(self.uut, |
| 50 | + ["print 123"], valid=True) |
| 51 | + |
| 52 | + def test_bad_language(self): |
| 53 | + self.section.append(Setting('language', 'Piet')) |
| 54 | + self.check_validity(self.uut, |
| 55 | + ["1 + 1"], valid=True) |
| 56 | + while not self.queue.empty(): |
| 57 | + message = self.queue.get() |
| 58 | + msg = ('Language needs to be "Python", "Python 2" or ' |
| 59 | + '"Python 3". Assuming Python 3.') |
| 60 | + if message.message == msg: |
| 61 | + break |
| 62 | + else: |
| 63 | + assert False, 'Message not found' |
| 64 | + |
| 65 | + def test_check_untyped_function_bodies(self): |
| 66 | + source = dedent(""" |
| 67 | + def foo(): |
| 68 | + return 1 + "abc" |
| 69 | + """).splitlines() |
| 70 | + self.check_validity(self.uut, source, valid=True) |
| 71 | + self.section.append(Setting('check_untyped_function_bodies', 'true')) |
| 72 | + self.check_validity(self.uut, source, valid=False) |
| 73 | + |
| 74 | + def test_allow_untyped_functions(self): |
| 75 | + source = dedent(""" |
| 76 | + def foo(): |
| 77 | + pass |
| 78 | + """).splitlines() |
| 79 | + self.check_validity(self.uut, source, valid=True) |
| 80 | + self.section.append(Setting('allow_untyped_functions', 'false')) |
| 81 | + self.check_validity(self.uut, source, valid=False) |
| 82 | + |
| 83 | + def test_allow_untyped_calls(self): |
| 84 | + source = dedent(""" |
| 85 | + def foo(): |
| 86 | + pass |
| 87 | +
|
| 88 | + foo() |
| 89 | + """).splitlines() |
| 90 | + self.check_validity(self.uut, source, valid=True) |
| 91 | + self.section.append(Setting('allow_untyped_calls', 'false')) |
| 92 | + self.check_validity(self.uut, source, valid=False) |
| 93 | + |
| 94 | + def test_strict_optional(self): |
| 95 | + source = dedent(""" |
| 96 | + from typing import Optional, List |
| 97 | +
|
| 98 | + def read_data_file(path: Optional[str]) -> List[str]: |
| 99 | + with open(path) as f: # Error |
| 100 | + return f.read().split(',') |
| 101 | + """).splitlines() |
| 102 | + self.check_validity(self.uut, source, valid=True) |
| 103 | + self.section.append(Setting('strict_optional', 'true')) |
| 104 | + self.check_validity(self.uut, source, valid=False) |
| 105 | + |
| 106 | + def test_discarded_note(self): |
| 107 | + source = dedent(""" |
| 108 | + def f() -> None: |
| 109 | + 1 + "a" |
| 110 | + """).splitlines() |
| 111 | + prepared = prepare_file(source, filename=None, create_tempfile=True) |
| 112 | + with prepared as (file, fname): |
| 113 | + results = [ |
| 114 | + Result.from_values( |
| 115 | + message=( |
| 116 | + 'Unsupported operand types for + ("int" and "str")'), |
| 117 | + file=fname, |
| 118 | + line=3, |
| 119 | + origin=self.uut, |
| 120 | + severity=RESULT_SEVERITY.MAJOR, |
| 121 | + ) |
| 122 | + ] |
| 123 | + self.check_results(self.uut, source, results=results, |
| 124 | + filename=fname, create_tempfile=False) |
0 commit comments