|
| 1 | +-- Tests for mypy.toml / .mypy.toml parsing |
| 2 | +-- ---------------------------------------- |
| 3 | + |
| 4 | +[case testNonArrayOverridesMypyTOML] |
| 5 | +# cmd: mypy x.py |
| 6 | +[file mypy.toml] |
| 7 | +\[mypy.overrides] |
| 8 | +module = "x" |
| 9 | +disallow_untyped_defs = false |
| 10 | +[file x.py] |
| 11 | +def f(a): |
| 12 | + pass |
| 13 | +def g(a: int) -> int: |
| 14 | + return f(a) |
| 15 | +[out] |
| 16 | +mypy.toml: mypy.overrides sections must be an array. Please make sure you are using double brackets like so: [[mypy.overrides]] |
| 17 | +== Return code: 0 |
| 18 | + |
| 19 | +[case testNoModuleInOverrideMypyTOML] |
| 20 | +# cmd: mypy x.py |
| 21 | +[file mypy.toml] |
| 22 | +\[[mypy.overrides]] |
| 23 | +disallow_untyped_defs = false |
| 24 | +[file x.py] |
| 25 | +def f(a): |
| 26 | + pass |
| 27 | +def g(a: int) -> int: |
| 28 | + return f(a) |
| 29 | +[out] |
| 30 | +mypy.toml: toml config file contains a [[mypy.overrides]] section, but no module to override was specified. |
| 31 | +== Return code: 0 |
| 32 | + |
| 33 | +[case testInvalidModuleInOverrideMypyTOML] |
| 34 | +# cmd: mypy x.py |
| 35 | +[file mypy.toml] |
| 36 | +\[[mypy.overrides]] |
| 37 | +module = 0 |
| 38 | +disallow_untyped_defs = false |
| 39 | +[file x.py] |
| 40 | +def f(a): |
| 41 | + pass |
| 42 | +def g(a: int) -> int: |
| 43 | + return f(a) |
| 44 | +[out] |
| 45 | +mypy.toml: toml config file contains a [[mypy.overrides]] section with a module value that is not a string or a list of strings |
| 46 | +== Return code: 0 |
| 47 | + |
| 48 | +[case testConflictingModuleInOverridesMypyTOML] |
| 49 | +# cmd: mypy x.py |
| 50 | +[file mypy.toml] |
| 51 | +\[[mypy.overrides]] |
| 52 | +module = 'x' |
| 53 | +disallow_untyped_defs = false |
| 54 | +\[[mypy.overrides]] |
| 55 | +module = ['x'] |
| 56 | +disallow_untyped_defs = true |
| 57 | +[file x.py] |
| 58 | +def f(a): |
| 59 | + pass |
| 60 | +def g(a: int) -> int: |
| 61 | + return f(a) |
| 62 | +[out] |
| 63 | +mypy.toml: toml config file contains [[mypy.overrides]] sections with conflicting values. Module 'x' has two different values for 'disallow_untyped_defs' |
| 64 | +== Return code: 0 |
| 65 | + |
| 66 | +[case testMultilineLiteralExcludeMypyTOML] |
| 67 | +# cmd: mypy x |
| 68 | +[file mypy.toml] |
| 69 | +exclude = '''(?x)( |
| 70 | + (^|/)[^/]*skipme_\.py$ |
| 71 | + |(^|/)_skipme[^/]*\.py$ |
| 72 | +)''' |
| 73 | +[file x/__init__.py] |
| 74 | +i: int = 0 |
| 75 | +[file x/_skipme_please.py] |
| 76 | +This isn't even syntactically valid! |
| 77 | +[file x/please_skipme_.py] |
| 78 | +Neither is this! |
| 79 | + |
| 80 | +[case testMultilineBasicExcludeMypyTOML] |
| 81 | +# cmd: mypy x |
| 82 | +[file mypy.toml] |
| 83 | +exclude = """(?x)( |
| 84 | + (^|/)[^/]*skipme_\\.py$ |
| 85 | + |(^|/)_skipme[^/]*\\.py$ |
| 86 | +)""" |
| 87 | +[file x/__init__.py] |
| 88 | +i: int = 0 |
| 89 | +[file x/_skipme_please.py] |
| 90 | +This isn't even syntactically valid! |
| 91 | +[file x/please_skipme_.py] |
| 92 | +Neither is this! |
| 93 | + |
| 94 | +[case testSequenceExcludeMypyTOML] |
| 95 | +# cmd: mypy x |
| 96 | +[file mypy.toml] |
| 97 | +exclude = [ |
| 98 | + '(^|/)[^/]*skipme_\.py$', # literal (no escaping) |
| 99 | + "(^|/)_skipme[^/]*\\.py$", # basic (backslash needs escaping) |
| 100 | +] |
| 101 | +[file x/__init__.py] |
| 102 | +i: int = 0 |
| 103 | +[file x/_skipme_please.py] |
| 104 | +This isn't even syntactically valid! |
| 105 | +[file x/please_skipme_.py] |
| 106 | +Neither is this! |
| 107 | + |
| 108 | +[case testMypyTOMLFilesTrailingComma] |
| 109 | +# cmd: mypy |
| 110 | +[file mypy.toml] |
| 111 | +# We combine multiple tests in a single one here, because these tests are slow. |
| 112 | +files = """ |
| 113 | + a.py, |
| 114 | + b.py, |
| 115 | +""" |
| 116 | +always_true = """ |
| 117 | + FLAG_A1, |
| 118 | + FLAG_B1, |
| 119 | +""" |
| 120 | +always_false = """ |
| 121 | + FLAG_A2, |
| 122 | + FLAG_B2, |
| 123 | +""" |
| 124 | +[file a.py] |
| 125 | +x: str = 'x' # ok' |
| 126 | + |
| 127 | +# --always-true |
| 128 | +FLAG_A1 = False |
| 129 | +FLAG_B1 = False |
| 130 | +if not FLAG_A1: # unreachable |
| 131 | + x: int = 'x' |
| 132 | +if not FLAG_B1: # unreachable |
| 133 | + y: int = 'y' |
| 134 | +[file b.py] |
| 135 | +y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 136 | +[file c.py] |
| 137 | +# This should not trigger any errors, because it is not included: |
| 138 | +z: int = 'z' |
| 139 | +[out] |
| 140 | + |
| 141 | +[case testMypyTOMLModulesTrailingComma] |
| 142 | +# cmd: mypy |
| 143 | +[file mypy.toml] |
| 144 | +# We combine multiple tests in a single one here, because these tests are slow. |
| 145 | +modules = """ |
| 146 | + a, |
| 147 | + b, |
| 148 | +""" |
| 149 | +disable_error_code = """ |
| 150 | + operator, |
| 151 | + import, |
| 152 | +""" |
| 153 | +enable_error_code = """ |
| 154 | + redundant-expr, |
| 155 | + ignore-without-code, |
| 156 | +""" |
| 157 | +[file a.py] |
| 158 | +x: str = 'x' # ok |
| 159 | + |
| 160 | +# --enable-error-code |
| 161 | +a: int = 'a' # type: ignore |
| 162 | + |
| 163 | +# --disable-error-code |
| 164 | +'a' + 1 |
| 165 | +[file b.py] |
| 166 | +y: int = 'y' |
| 167 | +[file c.py] |
| 168 | +# This should not trigger any errors, because it is not included: |
| 169 | +z: int = 'z' |
| 170 | +[out] |
| 171 | +b.py:1: error: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 172 | +a.py:4: error: "type: ignore" comment without error code (consider "type: ignore[assignment]" instead) |
| 173 | + |
| 174 | +[case testMypyTOMLPackagesTrailingComma] |
| 175 | +# cmd: mypy |
| 176 | +[file mypy.toml] |
| 177 | +packages = """ |
| 178 | + a, |
| 179 | + b, |
| 180 | +""" |
| 181 | +[file a/__init__.py] |
| 182 | +x: str = 'x' # ok |
| 183 | +[file b/__init__.py] |
| 184 | +y: int = 'y' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 185 | +[file c/__init__.py] |
| 186 | +# This should not trigger any errors, because it is not included: |
| 187 | +z: int = 'z' |
| 188 | +[out] |
| 189 | + |
| 190 | +[case testMypyTOMLSettingOfWrongType] |
| 191 | +# cmd: mypy a.py |
| 192 | +[file mypy.toml] |
| 193 | +enable_error_code = true |
| 194 | +[file a.py] |
| 195 | +x: int = 1 |
| 196 | +[out] |
| 197 | +mypy.toml: [mypy]: enable_error_code: Expected a list or a stringified version thereof, but got: 'True', of type bool. |
| 198 | +== Return code: 0 |
0 commit comments