| 1 |
-- Checks for 'mypy: option' directives inside files |
| 2 |
|
| 3 |
[case testInlineSimple1] |
| 4 |
|
| 5 |
# mypy: disallow-any-generics, no-warn-no-return |
| 6 |
|
| 7 |
from typing import List, Optional |
| 8 |
def foo() -> Optional[List]: # E: Missing type arguments for generic type "List" |
| 9 |
20 |
| 10 |
|
| 11 |
[builtins fixtures/list.pyi] |
| 12 |
|
| 13 |
[case testInlineSimple2] |
| 14 |
|
| 15 |
# mypy: disallow-any-generics |
| 16 |
# mypy: no-warn-no-return |
| 17 |
|
| 18 |
from typing import List, Optional |
| 19 |
def foo() -> Optional[List]: # E: Missing type arguments for generic type "List" |
| 20 |
20 |
| 21 |
|
| 22 |
[builtins fixtures/list.pyi] |
| 23 |
|
| 24 |
[case testInlineSimple3] |
| 25 |
|
| 26 |
# mypy: disallow-any-generics=true, warn-no-return=0 |
| 27 |
|
| 28 |
from typing import List, Optional |
| 29 |
def foo() -> Optional[List]: # E: Missing type arguments for generic type "List" |
| 30 |
20 |
| 31 |
|
| 32 |
[builtins fixtures/list.pyi] |
| 33 |
|
| 34 |
|
| 35 |
[case testInlineSimple4] |
| 36 |
|
| 37 |
# mypy: disallow-any-generics = true, warn-no-return = 0 |
| 38 |
|
| 39 |
from typing import List, Optional |
| 40 |
def foo() -> Optional[List]: # E: Missing type arguments for generic type "List" |
| 41 |
20 |
| 42 |
|
| 43 |
[builtins fixtures/list.pyi] |
| 44 |
|
| 45 |
[case testInlineList] |
| 46 |
|
| 47 |
# mypy: disallow-any-generics,always-false="FOO,BAR" |
| 48 |
|
| 49 |
from typing import List |
| 50 |
|
| 51 |
def foo(FOO: bool, BAR: bool) -> List: # E: Missing type arguments for generic type "List" |
| 52 |
if FOO or BAR: |
| 53 |
1+'lol' |
| 54 |
return [] |
| 55 |
|
| 56 |
[builtins fixtures/list.pyi] |
| 57 |
|
| 58 |
[case testInlineInvert1] |
| 59 |
# flags: --disallow-any-generics --allow-untyped-globals |
| 60 |
import a |
| 61 |
[file a.py] |
| 62 |
# mypy: allow-any-generics, disallow-untyped-globals |
| 63 |
|
| 64 |
x = [] # E: Need type annotation for "x" (hint: "x: list[<type>] = ...") |
| 65 |
|
| 66 |
from typing import List |
| 67 |
def foo() -> List: |
| 68 |
... |
| 69 |
|
| 70 |
[builtins fixtures/list.pyi] |
| 71 |
|
| 72 |
[case testInlineInvert2] |
| 73 |
|
| 74 |
import a |
| 75 |
[file a.py] |
| 76 |
# mypy: no-always-true |
| 77 |
|
| 78 |
[out] |
| 79 |
tmp/a.py:1: error: Can not invert non-boolean key always_true |
| 80 |
|
| 81 |
[case testInlineIncremental1] |
| 82 |
|
| 83 |
import a |
| 84 |
[file a.py] |
| 85 |
# mypy: disallow-any-generics, no-warn-no-return |
| 86 |
|
| 87 |
from typing import List, Optional |
| 88 |
def foo() -> Optional[List]: |
| 89 |
20 |
| 90 |
|
| 91 |
[file a.py.2] |
| 92 |
# mypy: no-warn-no-return |
| 93 |
|
| 94 |
from typing import List, Optional |
| 95 |
def foo() -> Optional[List]: |
| 96 |
20 |
| 97 |
|
| 98 |
[file a.py.3] |
| 99 |
from typing import List, Optional |
| 100 |
def foo() -> Optional[List]: |
| 101 |
20 |
| 102 |
[out] |
| 103 |
tmp/a.py:4: error: Missing type arguments for generic type "List" |
| 104 |
[out2] |
| 105 |
[out3] |
| 106 |
tmp/a.py:2: error: Missing return statement |
| 107 |
|
| 108 |
[builtins fixtures/list.pyi] |
| 109 |
|
| 110 |
[case testInlineIncremental2] |
| 111 |
|
| 112 |
# flags2: --disallow-any-generics |
| 113 |
import a |
| 114 |
[file a.py] |
| 115 |
# mypy: no-warn-no-return |
| 116 |
|
| 117 |
from typing import Optional, List |
| 118 |
def foo() -> Optional[List]: |
| 119 |
20 |
| 120 |
|
| 121 |
[file b.py.2] |
| 122 |
# no changes to a.py, but flag change should cause recheck |
| 123 |
|
| 124 |
[out] |
| 125 |
[out2] |
| 126 |
tmp/a.py:4: error: Missing type arguments for generic type "List" |
| 127 |
|
| 128 |
[builtins fixtures/list.pyi] |
| 129 |
|
| 130 |
[case testInlineIncremental3] |
| 131 |
import a, b |
| 132 |
[file a.py] |
| 133 |
# mypy: no-warn-no-return |
| 134 |
from typing import Optional |
| 135 |
|
| 136 |
def foo() -> Optional[int]: |
| 137 |
20 |
| 138 |
|
| 139 |
[file b.py] |
| 140 |
[file b.py.2] |
| 141 |
# no changes to a.py and we want to make sure it isn't rechecked |
| 142 |
[out] |
| 143 |
[out2] |
| 144 |
[rechecked b] |
| 145 |
|
| 146 |
[case testInlineError1] |
| 147 |
# mypy: invalid-whatever |
| 148 |
# mypy: no-warn-no-return; no-strict-optional |
| 149 |
# mypy: always-true=FOO,BAR |
| 150 |
# mypy: always-true="FOO,BAR |
| 151 |
[out] |
| 152 |
main:1: error: Unrecognized option: invalid_whatever = True |
| 153 |
main:2: error: Unrecognized option: no_warn_no_return; no_strict_optional = True |
| 154 |
main:3: error: Unrecognized option: bar = True |
| 155 |
main:4: error: Unterminated quote in configuration comment |
| 156 |
|
| 157 |
[case testInlineError2] |
| 158 |
# mypy: skip-file |
| 159 |
[out] |
| 160 |
main:1: error: Unrecognized option: skip_file = True |
| 161 |
|
| 162 |
[case testInlineStrict] |
| 163 |
# mypy: strict |
| 164 |
[out] |
| 165 |
main:1: error: Setting "strict" not supported in inline configuration: specify it in a configuration file instead, or set individual inline flags (see "mypy -h" for the list of flags enabled in strict mode) |
| 166 |
|
| 167 |
[case testInlineErrorCodes] |
| 168 |
# mypy: enable-error-code="ignore-without-code,truthy-bool" |
| 169 |
class Foo: |
| 170 |
pass |
| 171 |
|
| 172 |
foo = Foo() |
| 173 |
if foo: ... # E: "__main__.foo" has type "Foo" which does not implement __bool__ or __len__ so it could always be true in boolean context |
| 174 |
42 + "no" # type: ignore # E: "type: ignore" comment without error code (consider "type: ignore[operator]" instead) |
| 175 |
|
| 176 |
[case testInlineErrorCodesOverrideConfig] |
| 177 |
# flags: --config-file tmp/mypy.ini |
| 178 |
import foo |
| 179 |
import tests.bar |
| 180 |
import tests.baz |
| 181 |
[file foo.py] |
| 182 |
# mypy: disable-error-code="truthy-bool" |
| 183 |
class Foo: |
| 184 |
pass |
| 185 |
|
| 186 |
foo = Foo() |
| 187 |
if foo: ... |
| 188 |
42 + "no" # type: ignore # E: "type: ignore" comment without error code (consider "type: ignore[operator]" instead) |
| 189 |
|
| 190 |
[file tests/__init__.py] |
| 191 |
[file tests/bar.py] |
| 192 |
# mypy: enable-error-code="ignore-without-code" |
| 193 |
|
| 194 |
def foo() -> int: ... |
| 195 |
if foo: ... # E: Function "foo" could always be true in boolean context |
| 196 |
42 + "no" # type: ignore # E: "type: ignore" comment without error code (consider "type: ignore[operator]" instead) |
| 197 |
|
| 198 |
[file tests/baz.py] |
| 199 |
# mypy: disable-error-code="truthy-bool" |
| 200 |
class Foo: |
| 201 |
pass |
| 202 |
|
| 203 |
foo = Foo() |
| 204 |
if foo: ... |
| 205 |
42 + "no" # type: ignore |
| 206 |
|
| 207 |
[file mypy.ini] |
| 208 |
\[mypy] |
| 209 |
enable_error_code = ignore-without-code, truthy-bool |
| 210 |
|
| 211 |
\[mypy-tests.*] |
| 212 |
disable_error_code = ignore-without-code |
| 213 |
|
| 214 |
[case testInlineErrorCodesOverrideConfigSmall] |
| 215 |
# flags: --config-file tmp/mypy.ini |
| 216 |
import tests.baz |
| 217 |
[file tests/__init__.py] |
| 218 |
[file tests/baz.py] |
| 219 |
42 + "no" # type: ignore |
| 220 |
|
| 221 |
[file mypy.ini] |
| 222 |
\[mypy] |
| 223 |
enable_error_code = ignore-without-code, truthy-bool |
| 224 |
|
| 225 |
\[mypy-tests.*] |
| 226 |
disable_error_code = ignore-without-code |
| 227 |
|
| 228 |
[case testInlineErrorCodesOverrideConfigSmall2] |
| 229 |
# flags: --config-file tmp/mypy.ini |
| 230 |
import tests.bar |
| 231 |
import tests.baz |
| 232 |
[file tests/__init__.py] |
| 233 |
[file tests/baz.py] |
| 234 |
42 + "no" # type: ignore |
| 235 |
[file tests/bar.py] |
| 236 |
# mypy: enable-error-code="ignore-without-code" |
| 237 |
|
| 238 |
def foo() -> int: ... |
| 239 |
if foo: ... # E: Function "foo" could always be true in boolean context |
| 240 |
42 + "no" # type: ignore # E: "type: ignore" comment without error code (consider "type: ignore[operator]" instead) |
| 241 |
|
| 242 |
[file mypy.ini] |
| 243 |
\[mypy] |
| 244 |
enable_error_code = ignore-without-code, truthy-bool |
| 245 |
|
| 246 |
\[mypy-tests.*] |
| 247 |
disable_error_code = ignore-without-code |
| 248 |
|
| 249 |
|
| 250 |
[case testInlineErrorCodesOverrideConfigSmallBackward] |
| 251 |
# flags: --config-file tmp/mypy.ini |
| 252 |
import tests.bar |
| 253 |
import tests.baz |
| 254 |
[file tests/__init__.py] |
| 255 |
[file tests/baz.py] |
| 256 |
42 + "no" # type: ignore # E: "type: ignore" comment without error code (consider "type: ignore[operator]" instead) |
| 257 |
[file tests/bar.py] |
| 258 |
# mypy: disable-error-code="ignore-without-code" |
| 259 |
42 + "no" # type: ignore |
| 260 |
|
| 261 |
[file mypy.ini] |
| 262 |
\[mypy] |
| 263 |
enable_error_code = ignore-without-code, truthy-bool |
| 264 |
|
| 265 |
\[mypy-tests.*] |
| 266 |
enable_error_code = ignore-without-code |
| 267 |
|
| 268 |
[case testInlineOverrideConfig] |
| 269 |
# flags: --config-file tmp/mypy.ini |
| 270 |
import foo |
| 271 |
import tests.bar |
| 272 |
import tests.baz |
| 273 |
[file foo.py] |
| 274 |
# mypy: disable-error-code="truthy-bool" |
| 275 |
class Foo: |
| 276 |
pass |
| 277 |
|
| 278 |
foo = Foo() |
| 279 |
if foo: ... |
| 280 |
42 # type: ignore # E: Unused "type: ignore" comment |
| 281 |
|
| 282 |
[file tests/__init__.py] |
| 283 |
[file tests/bar.py] |
| 284 |
# mypy: warn_unused_ignores |
| 285 |
|
| 286 |
def foo() -> int: ... |
| 287 |
if foo: ... # E: Function "foo" could always be true in boolean context |
| 288 |
42 # type: ignore # E: Unused "type: ignore" comment |
| 289 |
|
| 290 |
[file tests/baz.py] |
| 291 |
# mypy: disable-error-code="truthy-bool" |
| 292 |
class Foo: |
| 293 |
pass |
| 294 |
|
| 295 |
foo = Foo() |
| 296 |
if foo: ... |
| 297 |
42 # type: ignore |
| 298 |
|
| 299 |
[file mypy.ini] |
| 300 |
\[mypy] |
| 301 |
warn_unused_ignores = True |
| 302 |
|
| 303 |
\[mypy-tests.*] |
| 304 |
warn_unused_ignores = False |
| 305 |
|
| 306 |
|
| 307 |
[case testIgnoreErrorsSimple] |
| 308 |
# mypy: ignore-errors=True |
| 309 |
|
| 310 |
def f() -> None: |
| 311 |
while 1(): |
| 312 |
pass |
| 313 |
|
| 314 |
[case testIgnoreErrorsInImportedModule] |
| 315 |
from m import C |
| 316 |
c = C() |
| 317 |
reveal_type(c.x) # N: Revealed type is "builtins.int" |
| 318 |
|
| 319 |
[file m.py] |
| 320 |
# mypy: ignore-errors=True |
| 321 |
|
| 322 |
class C: |
| 323 |
def f(self) -> None: |
| 324 |
self.x = 1 |
| 325 |
|
| 326 |
[case testIgnoreErrorsWithLambda] |
| 327 |
# mypy: ignore-errors=True |
| 328 |
|
| 329 |
def f(self, x=lambda: 1) -> None: |
| 330 |
pass |
| 331 |
|
| 332 |
class C: |
| 333 |
def f(self) -> None: |
| 334 |
l = lambda: 1 |
| 335 |
self.x = 1 |
| 336 |
|
| 337 |
[case testIgnoreErrorsWithUnsafeSuperCall_no_empty] |
| 338 |
|
| 339 |
from m import C |
| 340 |
|
| 341 |
class D(C): |
| 342 |
def m(self) -> None: |
| 343 |
super().m1() |
| 344 |
super().m2() \ |
| 345 |
# E: Call to abstract method "m2" of "C" with trivial body via super() is unsafe |
| 346 |
super().m3() \ |
| 347 |
# E: Call to abstract method "m3" of "C" with trivial body via super() is unsafe |
| 348 |
super().m4() \ |
| 349 |
# E: Call to abstract method "m4" of "C" with trivial body via super() is unsafe |
| 350 |
super().m5() \ |
| 351 |
# E: Call to abstract method "m5" of "C" with trivial body via super() is unsafe |
| 352 |
super().m6() \ |
| 353 |
# E: Call to abstract method "m6" of "C" with trivial body via super() is unsafe |
| 354 |
super().m7() |
| 355 |
|
| 356 |
def m1(self) -> int: |
| 357 |
return 0 |
| 358 |
|
| 359 |
def m2(self) -> int: |
| 360 |
return 0 |
| 361 |
|
| 362 |
def m3(self) -> int: |
| 363 |
return 0 |
| 364 |
|
| 365 |
def m4(self) -> int: |
| 366 |
return 0 |
| 367 |
|
| 368 |
def m5(self) -> int: |
| 369 |
return 0 |
| 370 |
|
| 371 |
def m6(self) -> int: |
| 372 |
return 0 |
| 373 |
|
| 374 |
[file m.py] |
| 375 |
# mypy: ignore-errors=True |
| 376 |
import abc |
| 377 |
|
| 378 |
class C: |
| 379 |
@abc.abstractmethod |
| 380 |
def m1(self) -> int: |
| 381 |
"""x""" |
| 382 |
return 0 |
| 383 |
|
| 384 |
@abc.abstractmethod |
| 385 |
def m2(self) -> int: |
| 386 |
"""doc""" |
| 387 |
|
| 388 |
@abc.abstractmethod |
| 389 |
def m3(self) -> int: |
| 390 |
pass |
| 391 |
|
| 392 |
@abc.abstractmethod |
| 393 |
def m4(self) -> int: ... |
| 394 |
|
| 395 |
@abc.abstractmethod |
| 396 |
def m5(self) -> int: |
| 397 |
"""doc""" |
| 398 |
... |
| 399 |
|
| 400 |
@abc.abstractmethod |
| 401 |
def m6(self) -> int: |
| 402 |
raise NotImplementedError() |
| 403 |
|
| 404 |
@abc.abstractmethod |
| 405 |
def m7(self) -> int: |
| 406 |
raise NotImplementedError() |
| 407 |
pass |
| 408 |
|
| 409 |
[builtins fixtures/exception.pyi] |
| 410 |
|
| 411 |
[case testInlineErrorCodesMultipleCodes] |
| 412 |
# mypy: disable-error-code="truthy-bool, ignore-without-code" |
| 413 |
class Foo: |
| 414 |
pass |
| 415 |
|
| 416 |
foo = Foo() |
| 417 |
if foo: ... |
| 418 |
42 + "no" # type: ignore |
| 419 |
|
| 420 |
[case testInlinePythonVersion] |
| 421 |
# mypy: python-version=3.10 # E: python_version not supported in inline configuration |
| 422 |
|
| 423 |
[case testInlineErrorCodesArentRuinedByOthersBaseCase] |
| 424 |
# mypy: disable-error-code=name-defined |
| 425 |
a |
| 426 |
|
| 427 |
[case testInlineErrorCodesArentRuinedByOthersInvalid] |
| 428 |
# mypy: disable-error-code=name-defined |
| 429 |
# mypy: AMONGUS |
| 430 |
a |
| 431 |
[out] |
| 432 |
main:2: error: Unrecognized option: amongus = True |
| 433 |
|
| 434 |
[case testInlineErrorCodesArentRuinedByOthersInvalidBefore] |
| 435 |
# mypy: AMONGUS |
| 436 |
# mypy: disable-error-code=name-defined |
| 437 |
a |
| 438 |
[out] |
| 439 |
main:1: error: Unrecognized option: amongus = True |
| 440 |
|
| 441 |
[case testInlineErrorCodesArentRuinedByOthersSe] |
| 442 |
# mypy: disable-error-code=name-defined |
| 443 |
# mypy: strict-equality |
| 444 |
def is_magic(x: bytes) -> bool: |
| 445 |
y |
| 446 |
return x == 'magic' # E: Unsupported left operand type for == ("bytes") |
| 447 |
|
| 448 |
[case testInlineConfigErrorCodesOffAndOn] |
| 449 |
# mypy: disable-error-code=name-defined |
| 450 |
# mypy: enable-error-code=name-defined |
| 451 |
a # E: Name "a" is not defined |
| 452 |
|
| 453 |
[case testInlineConfigErrorCodesOnAndOff] |
| 454 |
# mypy: enable-error-code=name-defined |
| 455 |
# mypy: disable-error-code=name-defined |
| 456 |
a # E: Name "a" is not defined |
| 457 |
|
| 458 |
[case testConfigFileErrorCodesOnAndOff] |
| 459 |
# flags: --config-file tmp/mypy.ini |
| 460 |
import foo |
| 461 |
[file foo.py] |
| 462 |
42 + "no" # type: ignore # E: "type: ignore" comment without error code (consider "type: ignore[operator]" instead) |
| 463 |
[file mypy.ini] |
| 464 |
\[mypy] |
| 465 |
enable_error_code = ignore-without-code |
| 466 |
disable_error_code = ignore-without-code |
| 467 |
|
| 468 |
[case testInlineConfigBaseCaseWui] |
| 469 |
# mypy: warn_unused_ignores |
| 470 |
x = 1 # type: ignore # E: Unused "type: ignore" comment |
| 471 |
|
| 472 |
[case testInlineConfigIsntRuinedByOthersInvalidWui] |
| 473 |
# mypy: warn_unused_ignores |
| 474 |
# mypy: AMONGUS |
| 475 |
x = 1 # type: ignore # E: Unused "type: ignore" comment |
| 476 |
[out] |
| 477 |
main:2: error: Unrecognized option: amongus = True |