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