| 1 |
-- Tests for error codes and ignoring errors using error codes |
| 2 |
-- |
| 3 |
-- These implicitly use --show-error-codes. |
| 4 |
|
| 5 |
[case testErrorCodeNoAttribute] |
| 6 |
import m |
| 7 |
m.x # E: Module has no attribute "x" [attr-defined] |
| 8 |
'x'.foobar # E: "str" has no attribute "foobar" [attr-defined] |
| 9 |
from m import xx # E: Module "m" has no attribute "xx" [attr-defined] |
| 10 |
from m import think # E: Module "m" has no attribute "think"; maybe "thing"? [attr-defined] |
| 11 |
for x in 1: # E: "int" has no attribute "__iter__" (not iterable) [attr-defined] |
| 12 |
pass |
| 13 |
[file m.py] |
| 14 |
thing = 0 |
| 15 |
[builtins fixtures/module.pyi] |
| 16 |
|
| 17 |
[case testErrorCodeUndefinedName] |
| 18 |
x # E: Name "x" is not defined [name-defined] |
| 19 |
def f() -> None: |
| 20 |
y # E: Name "y" is not defined [name-defined] |
| 21 |
[file m.py] |
| 22 |
[builtins fixtures/module.pyi] |
| 23 |
|
| 24 |
[case testErrorCodeUndefinedNameSuggestion] |
| 25 |
my_variable = 42 |
| 26 |
my_constant = 100 |
| 27 |
|
| 28 |
x = my_variabel # E: Name "my_variabel" is not defined; did you mean "my_variable"? [name-defined] |
| 29 |
|
| 30 |
def calculate_sum(items: int) -> int: |
| 31 |
return items |
| 32 |
|
| 33 |
calculate_summ(1) # E: Name "calculate_summ" is not defined; did you mean "calculate_sum"? [name-defined] |
| 34 |
|
| 35 |
class MyClass: |
| 36 |
pass |
| 37 |
|
| 38 |
y = MyClas() # E: Name "MyClas" is not defined; did you mean "MyClass"? [name-defined] |
| 39 |
|
| 40 |
unknown_xyz # E: Name "unknown_xyz" is not defined [name-defined] |
| 41 |
[builtins fixtures/module.pyi] |
| 42 |
|
| 43 |
[case testErrorCodeUndefinedNameSuggestionLocal] |
| 44 |
def foo() -> None: |
| 45 |
local_value = 10 |
| 46 |
x = local_valeu # E: Name "local_valeu" is not defined; did you mean "local_value"? [name-defined] |
| 47 |
[builtins fixtures/module.pyi] |
| 48 |
|
| 49 |
[case testErrorCodeUndefinedNameSuggestionOuterScope] |
| 50 |
top_level_var = 42 |
| 51 |
|
| 52 |
def foo() -> None: |
| 53 |
x = top_level_vr # E: Name "top_level_vr" is not defined; did you mean "top_level_var"? [name-defined] |
| 54 |
[builtins fixtures/module.pyi] |
| 55 |
|
| 56 |
[case testErrorCodeUndefinedNameSuggestionClassBody] |
| 57 |
class Foo: |
| 58 |
class_attr = 10 |
| 59 |
x = class_atr # E: Name "class_atr" is not defined; did you mean "class_attr"? [name-defined] |
| 60 |
[builtins fixtures/module.pyi] |
| 61 |
|
| 62 |
[case testErrorCodeUndefinedNameSuggestionFromImport] |
| 63 |
from m import some_function |
| 64 |
|
| 65 |
some_functon(1) # E: Name "some_functon" is not defined; did you mean "some_function"? [name-defined] |
| 66 |
|
| 67 |
[file m.py] |
| 68 |
def some_function(x: int) -> int: |
| 69 |
return x |
| 70 |
[builtins fixtures/module.pyi] |
| 71 |
|
| 72 |
[case testErrorCodeUndefinedNameSuggestionBuiltin] |
| 73 |
x = lenn # E: Name "lenn" is not defined; did you mean "len"? [name-defined] |
| 74 |
[builtins fixtures/len.pyi] |
| 75 |
|
| 76 |
[case testErrorCodeUndefinedNameSuggestionClassMethod] |
| 77 |
class Foo: |
| 78 |
class_attr = 10 |
| 79 |
def method(self) -> None: |
| 80 |
x = class_atr # E: Name "class_atr" is not defined [name-defined] |
| 81 |
[builtins fixtures/module.pyi] |
| 82 |
|
| 83 |
[case testErrorCodeUndefinedNameSuggestionMultiple] |
| 84 |
total_count = 1 |
| 85 |
total_counts = 2 |
| 86 |
x = total_countt # E: Name "total_countt" is not defined; did you mean "total_count" or "total_counts"? [name-defined] |
| 87 |
[builtins fixtures/module.pyi] |
| 88 |
|
| 89 |
[case testErrorCodeUndefinedNameSuggestionIgnored] |
| 90 |
my_variable = 42 |
| 91 |
x = my_variabel # type: ignore[name-defined] |
| 92 |
y = my_variabel # type: ignore |
| 93 |
[builtins fixtures/module.pyi] |
| 94 |
|
| 95 |
[case testErrorCodeUnclassifiedError] |
| 96 |
class A: |
| 97 |
def __init__(self) -> int: \ |
| 98 |
# E: The return type of "__init__" must be None [misc] |
| 99 |
pass |
| 100 |
|
| 101 |
[case testErrorCodeNoteHasNoCode] |
| 102 |
reveal_type(1) # N: Revealed type is "Literal[1]?" |
| 103 |
|
| 104 |
[case testErrorCodeSyntaxError_no_native_parse] |
| 105 |
1 '' |
| 106 |
[out] |
| 107 |
main:1: error: Invalid syntax [syntax] |
| 108 |
[out version==3.10.0] |
| 109 |
main:1: error: Invalid syntax. Perhaps you forgot a comma? [syntax] |
| 110 |
|
| 111 |
[case testErrorCodeSyntaxError2_no_native_parse] |
| 112 |
def f(): # E: Type signature has too many parameters [syntax] |
| 113 |
# type: (int) -> None |
| 114 |
1 |
| 115 |
|
| 116 |
x = 0 # type: x y # E: Syntax error in type comment "x y" [syntax] |
| 117 |
|
| 118 |
[case testErrorCodeSyntaxError3_no_native_parse] |
| 119 |
# This is a bit inconsistent -- syntax error would be more logical? |
| 120 |
x: 'a b' # E: Invalid type comment or annotation [valid-type] |
| 121 |
for v in x: # type: int, int # E: Syntax error in type annotation [syntax] \ |
| 122 |
# N: Suggestion: Use Tuple[T1, ..., Tn] instead of (T1, ..., Tn) |
| 123 |
pass |
| 124 |
|
| 125 |
[case testErrorCodeSyntaxErrorIgnoreNote] |
| 126 |
# This is a bit inconsistent -- syntax error would be more logical? |
| 127 |
x: 'a b' # type: ignore[valid-type] |
| 128 |
for v in x: # type: int, int # type: ignore[syntax] |
| 129 |
pass |
| 130 |
|
| 131 |
[case testErrorCodeIgnore1] |
| 132 |
'x'.foobar # type: ignore[attr-defined] |
| 133 |
'x'.foobar # type: ignore[xyz] # E: "str" has no attribute "foobar" [attr-defined] \ |
| 134 |
# N: Error code "attr-defined" not covered by "type: ignore[xyz]" comment |
| 135 |
'x'.foobar # type: ignore |
| 136 |
|
| 137 |
[case testErrorCodeIgnore2] |
| 138 |
a = 'x'.foobar # type: int # type: ignore[attr-defined] |
| 139 |
b = 'x'.foobar # type: int # type: ignore[xyz] # E: "str" has no attribute "foobar" [attr-defined] \ |
| 140 |
# N: Error code "attr-defined" not covered by "type: ignore[xyz]" comment |
| 141 |
c = 'x'.foobar # type: int # type: ignore |
| 142 |
|
| 143 |
[case testErrorCodeIgnoreMultiple1] |
| 144 |
a = 'x'.foobar(b) # type: ignore[name-defined, attr-defined] |
| 145 |
a = 'x'.foobar(b) # type: ignore[name-defined, xyz] # E: "str" has no attribute "foobar" [attr-defined] \ |
| 146 |
# N: Error code "attr-defined" not covered by "type: ignore[name-defined, xyz]" comment |
| 147 |
a = 'x'.foobar(b) # type: ignore[xyz, w, attr-defined] # E: Name "b" is not defined [name-defined] \ |
| 148 |
# N: Error code "name-defined" not covered by "type: ignore[xyz, w, attr-defined]" comment |
| 149 |
a = 'x'.foobar(b) # type: ignore[ w, xyz, attr-defined] # E: Name "b" is not defined [name-defined] \ |
| 150 |
# N: Error code "name-defined" not covered by "type: ignore[w, xyz, attr-defined]" comment |
| 151 |
|
| 152 |
[case testErrorCodeIgnoreMultiple2] |
| 153 |
a = 'x'.foobar(c) # type: int # type: ignore[name-defined, attr-defined] |
| 154 |
b = 'x'.foobar(c) # type: int # type: ignore[name-defined, xyz] # E: "str" has no attribute "foobar" [attr-defined] \ |
| 155 |
# N: Error code "attr-defined" not covered by "type: ignore[name-defined, xyz]" comment |
| 156 |
|
| 157 |
[case testErrorCodeWarnUnusedIgnores1] |
| 158 |
# flags: --warn-unused-ignores |
| 159 |
x # type: ignore[name-defined, attr-defined] # E: Unused "type: ignore[attr-defined]" comment [unused-ignore] |
| 160 |
|
| 161 |
[case testErrorCodeWarnUnusedIgnores2] |
| 162 |
# flags: --warn-unused-ignores |
| 163 |
"x".foobar(y) # type: ignore[name-defined, attr-defined] |
| 164 |
|
| 165 |
[case testErrorCodeWarnUnusedIgnores3] |
| 166 |
# flags: --warn-unused-ignores |
| 167 |
"x".foobar(y) # type: ignore[name-defined, attr-defined, xyz] # E: Unused "type: ignore[xyz]" comment [unused-ignore] |
| 168 |
|
| 169 |
[case testErrorCodeWarnUnusedIgnores4] |
| 170 |
# flags: --warn-unused-ignores |
| 171 |
"x".foobar(y) # type: ignore[name-defined, attr-defined, valid-type] # E: Unused "type: ignore[valid-type]" comment [unused-ignore] |
| 172 |
|
| 173 |
[case testErrorCodeWarnUnusedIgnores5] |
| 174 |
# flags: --warn-unused-ignores |
| 175 |
"x".foobar(y) # type: ignore[name-defined, attr-defined, valid-type, xyz] # E: Unused "type: ignore[valid-type, xyz]" comment [unused-ignore] |
| 176 |
|
| 177 |
[case testErrorCodeWarnUnusedIgnores6_NoDetailWhenSingleErrorCode] |
| 178 |
# flags: --warn-unused-ignores |
| 179 |
"x" # type: ignore[name-defined] # E: Unused "type: ignore" comment [unused-ignore] |
| 180 |
|
| 181 |
[case testErrorCodeWarnUnusedIgnores7_WarnWhenErrorCodeDisabled] |
| 182 |
# flags: --warn-unused-ignores --disable-error-code name-defined |
| 183 |
x # type: ignore # E: Unused "type: ignore" comment [unused-ignore] |
| 184 |
x # type: ignore[name-defined] # E: Unused "type: ignore" comment [unused-ignore] |
| 185 |
"x".foobar(y) # type: ignore[name-defined, attr-defined] # E: Unused "type: ignore[name-defined]" comment [unused-ignore] |
| 186 |
|
| 187 |
[case testErrorCodeWarnUnusedIgnores8_IgnoreUnusedIgnore] |
| 188 |
# flags: --warn-unused-ignores --disable-error-code name-defined |
| 189 |
"x" # type: ignore[unused-ignore] |
| 190 |
"x" # type: ignore[name-defined, unused-ignore] |
| 191 |
"x" # type: ignore[xyz, unused-ignore] |
| 192 |
x # type: ignore[name-defined, unused-ignore] |
| 193 |
|
| 194 |
[case testErrorCodeWarnUnusedIgnores9_SloppyFormattingNotEmptyBrackets] |
| 195 |
# flags: --warn-unused-ignores |
| 196 |
1 + "ok" + "ok".foo # type: ignore[ operator,attr-defined] |
| 197 |
1 + "ok" + "ok".foo # type: ignore[ ,operator,attr-defined] |
| 198 |
1 + "ok" + "ok".foo # type: ignore[ operator,attr-defined,] |
| 199 |
1 + "ok" + "ok".foo # type: ignore[ operator,attr-defined,,] |
| 200 |
|
| 201 |
[case testErrorCodeWarnUnusedIgnores10_SloppyFormattingEmptyBrackets] |
| 202 |
# flags: --warn-unused-ignores |
| 203 |
1+1 # type: ignore # E: Unused "type: ignore" comment [unused-ignore] |
| 204 |
1+1 # type: ignore[] # E: Unused "type: ignore" comment [unused-ignore] |
| 205 |
1+1 # type: ignore[ ] # E: Unused "type: ignore" comment [unused-ignore] |
| 206 |
1+1 # type: ignore[,,, ] # E: Unused "type: ignore" comment [unused-ignore] |
| 207 |
|
| 208 |
[case testErrorCodeMissingWhenRequired] |
| 209 |
# flags: --enable-error-code ignore-without-code |
| 210 |
"x" # type: ignore # E: "type: ignore" comment without error code [ignore-without-code] |
| 211 |
y # type: ignore # E: "type: ignore" comment without error code (consider "type: ignore[name-defined]" instead) [ignore-without-code] |
| 212 |
z # type: ignore[name-defined] |
| 213 |
"a" # type: ignore[ignore-without-code] |
| 214 |
|
| 215 |
[case testErrorCodeMissingDoesntTrampleUnusedIgnoresWarning] |
| 216 |
# flags: --enable-error-code ignore-without-code --warn-unused-ignores |
| 217 |
"x" # type: ignore # E: Unused "type: ignore" comment [unused-ignore] |
| 218 |
"y" # type: ignore[ignore-without-code] # E: Unused "type: ignore" comment [unused-ignore] |
| 219 |
z # type: ignore[ignore-without-code] # E: Unused "type: ignore" comment [unused-ignore] \ |
| 220 |
# E: Name "z" is not defined [name-defined] \ |
| 221 |
# N: Error code "name-defined" not covered by "type: ignore[ignore-without-code]" comment |
| 222 |
|
| 223 |
[case testErrorCodeMissingWholeFileIgnores] |
| 224 |
# flags: --enable-error-code ignore-without-code |
| 225 |
# type: ignore # whole file ignore |
| 226 |
x |
| 227 |
y # type: ignore # ignore the lack of error code since we ignore the whole file |
| 228 |
|
| 229 |
[case testErrorCodeMissingMultiple] |
| 230 |
# flags: --enable-error-code ignore-without-code |
| 231 |
from __future__ import annotations |
| 232 |
class A: |
| 233 |
attr: int |
| 234 |
def func(self, var: int) -> A | None: ... |
| 235 |
|
| 236 |
a: A | None |
| 237 |
# 'union-attr' should only be listed once (instead of twice) and list should be sorted |
| 238 |
a.func("invalid string").attr # type: ignore # E: "type: ignore" comment without error code (consider "type: ignore[arg-type, union-attr]" instead) [ignore-without-code] |
| 239 |
[builtins fixtures/tuple.pyi] |
| 240 |
|
| 241 |
[case testErrorCodeIgnoreWithExtraSpace] |
| 242 |
x # type: ignore [name-defined] |
| 243 |
x2 # type: ignore [ name-defined ] |
| 244 |
x3 # type: ignore [ xyz , name-defined ] |
| 245 |
x4 # type: ignore[xyz,name-defined] |
| 246 |
y # type: ignore [xyz] # E: Name "y" is not defined [name-defined] \ |
| 247 |
# N: Error code "name-defined" not covered by "type: ignore[xyz]" comment |
| 248 |
y # type: ignore[ xyz ] # E: Name "y" is not defined [name-defined] \ |
| 249 |
# N: Error code "name-defined" not covered by "type: ignore[xyz]" comment |
| 250 |
y # type: ignore[ xyz , foo ] # E: Name "y" is not defined [name-defined] \ |
| 251 |
# N: Error code "name-defined" not covered by "type: ignore[xyz, foo]" comment |
| 252 |
|
| 253 |
a = z # type: int # type: ignore [name-defined] |
| 254 |
b = z2 # type: int # type: ignore [ name-defined ] |
| 255 |
c = z2 # type: int # type: ignore [ name-defined , xyz ] |
| 256 |
d = zz # type: int # type: ignore [xyz] # E: Name "zz" is not defined [name-defined] \ |
| 257 |
# N: Error code "name-defined" not covered by "type: ignore[xyz]" comment |
| 258 |
e = zz # type: int # type: ignore [ xyz ] # E: Name "zz" is not defined [name-defined] \ |
| 259 |
# N: Error code "name-defined" not covered by "type: ignore[xyz]" comment |
| 260 |
f = zz # type: int # type: ignore [ xyz,foo ] # E: Name "zz" is not defined [name-defined] \ |
| 261 |
# N: Error code "name-defined" not covered by "type: ignore[xyz, foo]" comment |
| 262 |
|
| 263 |
[case testErrorCodeIgnoreAfterArgComment] |
| 264 |
def f(x # type: xyz # type: ignore[name-defined] # Comment |
| 265 |
): |
| 266 |
# type () -> None |
| 267 |
pass |
| 268 |
|
| 269 |
def g(x # type: xyz # type: ignore # Comment |
| 270 |
): |
| 271 |
# type () -> None |
| 272 |
pass |
| 273 |
|
| 274 |
def h(x # type: xyz # type: ignore[foo] # E: Name "xyz" is not defined [name-defined] \ |
| 275 |
# N: Error code "name-defined" not covered by "type: ignore[foo]" comment |
| 276 |
): |
| 277 |
# type () -> None |
| 278 |
pass |
| 279 |
|
| 280 |
[case testErrorCodeIgnoreWithNote] |
| 281 |
import nostub # type: ignore[import] |
| 282 |
from defusedxml import xyz # type: ignore[import] |
| 283 |
|
| 284 |
[case testErrorCodeBadIgnore_no_native_parse] |
| 285 |
import nostub # type: ignore xyz # E: Invalid "type: ignore" comment [syntax] \ |
| 286 |
# E: Cannot find implementation or library stub for module named "nostub" [import-not-found] \ |
| 287 |
# N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports |
| 288 |
import nostub # type: ignore[ # E: Invalid "type: ignore" comment [syntax] |
| 289 |
import nostub # type: ignore[foo # E: Invalid "type: ignore" comment [syntax] |
| 290 |
import nostub # type: ignore[foo, # E: Invalid "type: ignore" comment [syntax] |
| 291 |
import nostub # type: ignore[foo]] # E: Invalid "type: ignore" comment [syntax] |
| 292 |
import nostub # type: ignore[foo][bar] # E: Invalid "type: ignore" comment [syntax] |
| 293 |
import nostub # type: ignore[foo] [bar] # E: Invalid "type: ignore" comment [syntax] |
| 294 |
|
| 295 |
x = 0 # type: ignore[ # E: Invalid "type: ignore" comment [syntax] |
| 296 |
|
| 297 |
def f(x, # type: int # type: ignore[ # E: Invalid "type: ignore" comment [syntax] |
| 298 |
): |
| 299 |
# type: (...) -> None |
| 300 |
pass |
| 301 |
|
| 302 |
[case testErrorCodeBadIgnoreNoExtraComment_no_native_parse] |
| 303 |
# Omit the E: ... comments, as they affect parsing |
| 304 |
import nostub # type: ignore xyz |
| 305 |
import nostub # type: ignore[xyz |
| 306 |
import nostub # type: ignore[xyz][xyz] |
| 307 |
x = 0 # type: ignore[ |
| 308 |
def f(x, # type: int # type: ignore[ |
| 309 |
): |
| 310 |
# type: (...) -> None |
| 311 |
pass |
| 312 |
[out] |
| 313 |
main:2: error: Invalid "type: ignore" comment [syntax] |
| 314 |
main:2: error: Cannot find implementation or library stub for module named "nostub" [import-not-found] |
| 315 |
main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports |
| 316 |
main:3: error: Invalid "type: ignore" comment [syntax] |
| 317 |
main:4: error: Invalid "type: ignore" comment [syntax] |
| 318 |
main:5: error: Invalid "type: ignore" comment [syntax] |
| 319 |
main:6: error: Invalid "type: ignore" comment [syntax] |
| 320 |
|
| 321 |
[case testErrorCodeArgKindAndCount] |
| 322 |
def f(x: int) -> None: pass |
| 323 |
f() # E: Missing positional argument "x" in call to "f" [call-arg] |
| 324 |
f(1, 2) # E: Too many arguments for "f" [call-arg] |
| 325 |
f(y=1) # E: Unexpected keyword argument "y" for "f" [call-arg] |
| 326 |
|
| 327 |
def g(*, x: int) -> None: pass |
| 328 |
g() # E: Missing named argument "x" for "g" [call-arg] |
| 329 |
|
| 330 |
def h(x: int, y: int, z: int) -> None: pass |
| 331 |
h(y=1, z=1) # E: Missing positional argument "x" in call to "h" [call-arg] |
| 332 |
h(y=1) # E: Missing positional arguments "x", "z" in call to "h" [call-arg] |
| 333 |
|
| 334 |
[case testTooManyPositionalArgumentsErrorCode] |
| 335 |
def f(a: int, *, b: int) -> None: pass |
| 336 |
f(1, 2) # E: Too many positional arguments for "f" [call-arg] |
| 337 |
f(1, 2) # type: ignore[call-arg] |
| 338 |
f(1, 2) # type: ignore[misc] |
| 339 |
|
| 340 |
[case testTooManyPositionalArgumentsCoveredByMiscUnused] |
| 341 |
# flags: --warn-unused-ignores |
| 342 |
def f(a: int, *, b: int) -> None: pass |
| 343 |
f(1, 2) # type: ignore[misc] # E: Unused "type: ignore" comment, use narrower [call-arg] instead of [misc] code [unused-ignore] |
| 344 |
|
| 345 |
[case testErrorCodeArgType] |
| 346 |
def f(x: int) -> None: pass |
| 347 |
f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int" [arg-type] |
| 348 |
|
| 349 |
class A: |
| 350 |
def g(self, *, x: int) -> None: pass |
| 351 |
|
| 352 |
A().g(x='') # E: Argument "x" to "g" of "A" has incompatible type "str"; expected "int" [arg-type] |
| 353 |
|
| 354 |
[case testErrorCodeInvalidType] |
| 355 |
def f(): pass |
| 356 |
|
| 357 |
x: f # E: Function "__main__.f" is not valid as a type [valid-type] \ |
| 358 |
# N: Perhaps you need "Callable[...]" or a callback protocol? |
| 359 |
|
| 360 |
import sys |
| 361 |
y: sys # E: Module "sys" is not valid as a type [valid-type] \ |
| 362 |
# N: Perhaps you meant to use a protocol matching the module structure? |
| 363 |
z: y # E: Variable "__main__.y" is not valid as a type [valid-type] \ |
| 364 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 365 |
[builtins fixtures/tuple.pyi] |
| 366 |
|
| 367 |
[case testErrorCodeNeedTypeAnnotation] |
| 368 |
from typing import TypeVar |
| 369 |
|
| 370 |
T = TypeVar('T') |
| 371 |
def f() -> T: pass # E: A function returning TypeVar should receive at least one argument containing the same TypeVar [type-var] |
| 372 |
x = f() # E: Need type annotation for "x" [var-annotated] |
| 373 |
y = [] # E: Need type annotation for "y" (hint: "y: list[<type>] = ...") [var-annotated] |
| 374 |
[builtins fixtures/list.pyi] |
| 375 |
|
| 376 |
[case testErrorCodeBadOverride] |
| 377 |
from typing import overload |
| 378 |
|
| 379 |
class A: |
| 380 |
def f(self) -> int: |
| 381 |
return 0 |
| 382 |
class B(A): |
| 383 |
def f(self) -> str: # E: Return type "str" of "f" incompatible with return type "int" in supertype "A" [override] |
| 384 |
return '' |
| 385 |
class C(A): |
| 386 |
def f(self, x: int) -> int: # E: Signature of "f" incompatible with supertype "A" [override] \ |
| 387 |
# N: Superclass: \ |
| 388 |
# N: def f(self) -> int \ |
| 389 |
# N: Subclass: \ |
| 390 |
# N: def f(self, x: int) -> int |
| 391 |
return 0 |
| 392 |
class D: |
| 393 |
def f(self, x: int) -> int: |
| 394 |
return 0 |
| 395 |
class E(D): |
| 396 |
def f(self, x: str) -> int: # E: Argument 1 of "f" is incompatible with supertype "D"; supertype defines the argument type as "int" [override] \ |
| 397 |
# N: This violates the Liskov substitution principle \ |
| 398 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 399 |
return 0 |
| 400 |
|
| 401 |
class O: |
| 402 |
@overload |
| 403 |
def f(self, x: int) -> None: pass |
| 404 |
@overload |
| 405 |
def f(self, x: str) -> None: pass |
| 406 |
def f(self, x): |
| 407 |
pass |
| 408 |
|
| 409 |
class OO(O): |
| 410 |
@overload # E: Signature of "f" incompatible with supertype "O" [override] \ |
| 411 |
# N: Overload variants must be defined in the same order as they are in "O" |
| 412 |
def f(self, x: str) -> None: pass |
| 413 |
@overload |
| 414 |
def f(self, x: int) -> None: pass |
| 415 |
def f(self, x): |
| 416 |
pass |
| 417 |
|
| 418 |
[case testErrorCodeReturnValue] |
| 419 |
def f() -> int: |
| 420 |
return '' # E: Incompatible return value type (got "str", expected "int") [return-value] |
| 421 |
|
| 422 |
[case testErrorCodeMissingReturnValueInReturnStatement] |
| 423 |
def f() -> int: |
| 424 |
return # E: Return value expected [return-value] |
| 425 |
|
| 426 |
[case testErrorCodeAssignment] |
| 427 |
x: str = 0 # E: Incompatible types in assignment (expression has type "int", variable has type "str") [assignment] |
| 428 |
|
| 429 |
def f(x: str = 0) -> None: # E: Incompatible default for parameter "x" (default has type "int", parameter has type "str") [assignment] |
| 430 |
pass |
| 431 |
|
| 432 |
class A: |
| 433 |
x = 0 |
| 434 |
|
| 435 |
class B(A): |
| 436 |
x = '' # E: Incompatible types in assignment (expression has type "str", base class "A" defined the type as "int") [assignment] |
| 437 |
|
| 438 |
a: A |
| 439 |
a.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment] |
| 440 |
|
| 441 |
[case testErrorCodeMissingTypeArg] |
| 442 |
# flags: --disallow-any-generics |
| 443 |
from typing import List, TypeVar |
| 444 |
x: List # E: Missing type arguments for generic type "List" [type-arg] |
| 445 |
y: list # E: Missing type arguments for generic type "list" [type-arg] |
| 446 |
T = TypeVar('T') |
| 447 |
L = List[List[T]] |
| 448 |
z: L # E: Missing type arguments for generic type "L" [type-arg] |
| 449 |
[builtins fixtures/list.pyi] |
| 450 |
|
| 451 |
[case testErrorCodeUnionAttribute] |
| 452 |
from typing import Union |
| 453 |
class A: |
| 454 |
x: int |
| 455 |
class B: |
| 456 |
y: str |
| 457 |
a: Union[A, B] |
| 458 |
a.x # E: Item "B" of "A | B" has no attribute "x" [union-attr] |
| 459 |
|
| 460 |
[case testErrorCodeFunctionHasNoAnnotation] |
| 461 |
# flags: --disallow-untyped-defs |
| 462 |
|
| 463 |
def f(x): # E: Function is missing a type annotation [no-untyped-def] |
| 464 |
pass |
| 465 |
|
| 466 |
def g(x: int): # E: Function is missing a return type annotation [no-untyped-def] |
| 467 |
pass |
| 468 |
|
| 469 |
def h(x) -> None: # E: Function is missing a type annotation for one or more parameters [no-untyped-def] |
| 470 |
pass |
| 471 |
|
| 472 |
def gen(): # E: Function is missing a return type annotation [no-untyped-def] |
| 473 |
yield 1 |
| 474 |
|
| 475 |
def gen2(x: int): # E: Function is missing a return type annotation [no-untyped-def] |
| 476 |
yield 1 |
| 477 |
|
| 478 |
async def asyncf(): # E: Function is missing a return type annotation [no-untyped-def] |
| 479 |
return 0 |
| 480 |
|
| 481 |
async def asyncf2(x: int): # E: Function is missing a return type annotation [no-untyped-def] |
| 482 |
return 0 |
| 483 |
[typing fixtures/typing-async.pyi] |
| 484 |
[builtins fixtures/tuple.pyi] |
| 485 |
|
| 486 |
[case testErrorCodeCallUntypedFunction] |
| 487 |
# flags: --disallow-untyped-calls |
| 488 |
|
| 489 |
def f() -> None: |
| 490 |
g() # E: Call to untyped function "g" in typed context [no-untyped-call] |
| 491 |
|
| 492 |
def g(): |
| 493 |
pass |
| 494 |
|
| 495 |
[case testErrorCodeUntypedDecorator] |
| 496 |
# flags: --disallow-untyped-decorators --warn-unused-ignores |
| 497 |
def d(f): return f |
| 498 |
|
| 499 |
@d # E: Untyped decorator makes function "x" untyped [untyped-decorator] |
| 500 |
def x() -> int: return 1 |
| 501 |
@d # type: ignore |
| 502 |
def y() -> int: return 2 |
| 503 |
@d # type: ignore[untyped-decorator] |
| 504 |
def best() -> int: return 3 |
| 505 |
@d # type: ignore[misc] # E: Unused "type: ignore" comment [unused-ignore] \ |
| 506 |
# E: Untyped decorator makes function "z" untyped [untyped-decorator] \ |
| 507 |
# N: Error code "untyped-decorator" not covered by "type: ignore[misc]" comment |
| 508 |
def z() -> int: return 4 |
| 509 |
|
| 510 |
[case testErrorCodeIndexing] |
| 511 |
from typing import Dict |
| 512 |
x: Dict[int, int] |
| 513 |
x[''] # E: Invalid index type "str" for "dict[int, int]"; expected type "int" [index] |
| 514 |
1[''] # E: Value of type "int" is not indexable [index] |
| 515 |
1[''] = 1 # E: Unsupported target for indexed assignment ("int") [index] |
| 516 |
[builtins fixtures/dict.pyi] |
| 517 |
|
| 518 |
[case testErrorCodeInvalidTypeArg] |
| 519 |
from typing import TypeVar, Generic |
| 520 |
|
| 521 |
T = TypeVar('T', int, str) |
| 522 |
TT = TypeVar('TT', int, None) |
| 523 |
S = TypeVar('S', bound=str) |
| 524 |
|
| 525 |
def f(x: T) -> T: |
| 526 |
return x |
| 527 |
|
| 528 |
f(object()) # E: Value of type variable "T" of "f" cannot be "object" [type-var] |
| 529 |
|
| 530 |
def g(x: S) -> S: |
| 531 |
return x |
| 532 |
|
| 533 |
g(1) # E: Value of type variable "S" of "g" cannot be "int" [type-var] |
| 534 |
|
| 535 |
class C(Generic[T]): pass |
| 536 |
class D(Generic[S]): pass |
| 537 |
class E(Generic[S, T]): pass |
| 538 |
|
| 539 |
x: C[object] # E: Value of type variable "T" of "C" cannot be "object" [type-var] |
| 540 |
y: D[int] # E: Type argument "int" of "D" must be a subtype of "str" [type-var] |
| 541 |
z: D[int, int] # E: "D" expects 1 type argument, but 2 given [type-arg] |
| 542 |
|
| 543 |
def h(a: TT, s: S) -> None: |
| 544 |
b: C[TT] # E: Invalid type argument value for "C" [type-var] |
| 545 |
c: C[S] # E: Type variable "S" not valid as type argument value for "C" [type-var] |
| 546 |
|
| 547 |
[case testErrorCodeOperators] |
| 548 |
class A: pass |
| 549 |
A() + 1 # E: Unsupported left operand type for + ("A") [operator] |
| 550 |
1 in A() # E: Unsupported right operand type for in ("A") [operator] |
| 551 |
A() < 1 # E: Unsupported left operand type for < ("A") [operator] |
| 552 |
-A() # E: Unsupported operand type for unary - ("A") [operator] |
| 553 |
+A() # E: Unsupported operand type for unary + ("A") [operator] |
| 554 |
~A() # E: Unsupported operand type for ~ ("A") [operator] |
| 555 |
|
| 556 |
class B: |
| 557 |
def __add__(self, other: int) -> 'B': |
| 558 |
return self |
| 559 |
def __radd__(self, other: int) -> 'B': |
| 560 |
return self |
| 561 |
def __contains__(self, other: int) -> int: |
| 562 |
return 0 |
| 563 |
|
| 564 |
B() + '' # E: Unsupported operand types for + ("B" and "str") [operator] |
| 565 |
'' + B() # E: Unsupported operand types for + ("str" and "B") [operator] |
| 566 |
'' in B() # E: Unsupported operand types for in ("str" and "B") [operator] |
| 567 |
|
| 568 |
1() # E: "int" not callable [operator] |
| 569 |
[builtins fixtures/tuple.pyi] |
| 570 |
|
| 571 |
[case testErrorCodeListOrDictItem] |
| 572 |
from typing import List, Dict |
| 573 |
x: List[int] = [''] # E: List item 0 has incompatible type "str"; expected "int" [list-item] |
| 574 |
y: Dict[int, int] = {1: ''} # E: Dict entry 0 has incompatible type "int": "str"; expected "int": "int" [dict-item] |
| 575 |
[builtins fixtures/dict.pyi] |
| 576 |
|
| 577 |
[case testErrorCodeTypedDict] |
| 578 |
from typing import TypedDict |
| 579 |
class D(TypedDict): |
| 580 |
x: int |
| 581 |
class E(TypedDict): |
| 582 |
x: int |
| 583 |
y: int |
| 584 |
|
| 585 |
a: D = {'x': ''} # E: Incompatible types (expression has type "str", TypedDict item "x" has type "int") [typeddict-item] |
| 586 |
b: D = {'y': ''} # E: Missing key "x" for TypedDict "D" [typeddict-item] \ |
| 587 |
# E: Extra key "y" for TypedDict "D" [typeddict-unknown-key] |
| 588 |
c = D(x=0) if int() else E(x=0, y=0) |
| 589 |
c = {} # E: Missing key "x" for TypedDict "D" [typeddict-item] |
| 590 |
d: D = {'x': '', 'y': 1} # E: Extra key "y" for TypedDict "D" [typeddict-unknown-key] \ |
| 591 |
# E: Incompatible types (expression has type "str", TypedDict item "x" has type "int") [typeddict-item] |
| 592 |
|
| 593 |
|
| 594 |
a['y'] = 1 # E: TypedDict "D" has no key "y" [typeddict-unknown-key] |
| 595 |
a['x'] = 'x' # E: Value of "x" has incompatible type "str"; expected "int" [typeddict-item] |
| 596 |
a['y'] # E: TypedDict "D" has no key "y" [typeddict-item] |
| 597 |
[builtins fixtures/dict.pyi] |
| 598 |
[typing fixtures/typing-typeddict.pyi] |
| 599 |
|
| 600 |
[case testErrorCodeTypedDictNoteIgnore] |
| 601 |
from typing import TypedDict |
| 602 |
class A(TypedDict): |
| 603 |
one_commonpart: int |
| 604 |
two_commonparts: int |
| 605 |
|
| 606 |
a: A = {'one_commonpart': 1, 'two_commonparts': 2} |
| 607 |
a['other_commonpart'] = 3 # type: ignore[typeddict-unknown-key] |
| 608 |
not_exist = a['not_exist'] # type: ignore[typeddict-item] |
| 609 |
[builtins fixtures/dict.pyi] |
| 610 |
[typing fixtures/typing-typeddict.pyi] |
| 611 |
|
| 612 |
[case testErrorCodeTypedDictSubCodeIgnore] |
| 613 |
from typing import TypedDict |
| 614 |
class D(TypedDict): |
| 615 |
x: int |
| 616 |
d: D = {'x': 1, 'y': 2} # type: ignore[typeddict-item] |
| 617 |
[builtins fixtures/dict.pyi] |
| 618 |
[typing fixtures/typing-typeddict.pyi] |
| 619 |
|
| 620 |
[case testErrorCodeCannotDetermineType] |
| 621 |
y = x # E: Cannot determine type of "x" [has-type] \ |
| 622 |
# E: Name "x" is used before definition [used-before-def] |
| 623 |
reveal_type(y) # N: Revealed type is "Any" |
| 624 |
x = None |
| 625 |
|
| 626 |
[case testErrorCodeRedundantCast] |
| 627 |
# flags: --warn-redundant-casts |
| 628 |
from typing import cast |
| 629 |
|
| 630 |
x = cast(int, int()) # E: Redundant cast to "int" [redundant-cast] |
| 631 |
|
| 632 |
[case testErrorCodeInvalidCommentSignature] |
| 633 |
def f(x): # E: Type signature has too few parameters [syntax] |
| 634 |
# type: () -> None |
| 635 |
pass |
| 636 |
|
| 637 |
def g(x): # E: Type signature has too many parameters [syntax] |
| 638 |
# type: (int, int) -> None |
| 639 |
pass |
| 640 |
|
| 641 |
[case testErrorCodeNonOverlappingEquality] |
| 642 |
# flags: --strict-equality |
| 643 |
if int() == str(): # E: Non-overlapping equality check (left operand type: "int", right operand type: "str") [comparison-overlap] |
| 644 |
pass |
| 645 |
if int() != str(): # E: Non-overlapping equality check (left operand type: "int", right operand type: "str") [comparison-overlap] |
| 646 |
pass |
| 647 |
if int() is str(): # E: Non-overlapping identity check (left operand type: "int", right operand type: "str") [comparison-overlap] |
| 648 |
pass |
| 649 |
[builtins fixtures/primitives.pyi] |
| 650 |
|
| 651 |
[case testErrorCodeMissingModule] |
| 652 |
from defusedxml import xyz # E: Library stubs not installed for "defusedxml" [import-untyped] \ |
| 653 |
# N: Hint: "python3 -m pip install types-defusedxml" \ |
| 654 |
# N: (or run "mypy --install-types" to install all missing stub packages) |
| 655 |
from nonexistent import foobar # E: Cannot find implementation or library stub for module named "nonexistent" [import-not-found] |
| 656 |
import nonexistent2 # E: Cannot find implementation or library stub for module named "nonexistent2" [import-not-found] |
| 657 |
from nonexistent3 import * # E: Cannot find implementation or library stub for module named "nonexistent3" [import-not-found] |
| 658 |
from pkg import bad # E: Module "pkg" has no attribute "bad" [attr-defined] |
| 659 |
from pkg.bad2 import bad3 # E: Cannot find implementation or library stub for module named "pkg.bad2" [import-not-found] \ |
| 660 |
# N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports |
| 661 |
|
| 662 |
[file pkg/__init__.py] |
| 663 |
|
| 664 |
[case testErrorCodeAlreadyDefined] |
| 665 |
x: int |
| 666 |
x: str # E: Name "x" already defined on line 1 [no-redef] |
| 667 |
|
| 668 |
def f(): |
| 669 |
pass |
| 670 |
def f(): # E: Name "f" already defined on line 4 [no-redef] |
| 671 |
pass |
| 672 |
|
| 673 |
[case testErrorCodeMissingReturn] |
| 674 |
def f() -> int: # E: Missing return statement [return] |
| 675 |
x = 0 |
| 676 |
|
| 677 |
[case testErrorCodeReturnValueNotExpected] |
| 678 |
def f() -> None: |
| 679 |
return 1 # E: No return value expected [return-value] |
| 680 |
|
| 681 |
[case testErrorCodeFunctionDoesNotReturnValue] |
| 682 |
from typing import Callable |
| 683 |
|
| 684 |
def f() -> None: pass |
| 685 |
|
| 686 |
class A: |
| 687 |
def g(self) -> None: pass |
| 688 |
|
| 689 |
c: Callable[[], None] |
| 690 |
|
| 691 |
def main() -> None: |
| 692 |
x = f() # E: "f" does not return a value (it only ever returns None) [func-returns-value] |
| 693 |
y = A().g() # E: "g" of "A" does not return a value (it only ever returns None) [func-returns-value] |
| 694 |
z = c() # E: Function does not return a value (it only ever returns None) [func-returns-value] |
| 695 |
|
| 696 |
[case testErrorCodeInstantiateAbstract] |
| 697 |
from abc import abstractmethod |
| 698 |
|
| 699 |
class A: |
| 700 |
@abstractmethod |
| 701 |
def f(self): pass |
| 702 |
|
| 703 |
class B(A): |
| 704 |
pass |
| 705 |
|
| 706 |
B() # E: Cannot instantiate abstract class "B" with abstract attribute "f" [abstract] |
| 707 |
|
| 708 |
[case testErrorCodeNewTypeNotSubclassable] |
| 709 |
from typing import Union, NewType |
| 710 |
|
| 711 |
X = NewType('X', Union[int, str]) # E: Argument 2 to NewType(...) must be subclassable (got "int | str") [valid-newtype] |
| 712 |
|
| 713 |
[case testErrorCodeOverloadVariant] |
| 714 |
from typing import overload |
| 715 |
|
| 716 |
@overload |
| 717 |
def f(x: int) -> int: ... |
| 718 |
@overload |
| 719 |
def f(x: str) -> str: ... |
| 720 |
def f(x): |
| 721 |
return x |
| 722 |
|
| 723 |
f(object()) # E: No overload variant of "f" matches argument type "object" [call-overload] \ |
| 724 |
# N: Possible overload variants: \ |
| 725 |
# N: def f(x: int) -> int \ |
| 726 |
# N: def f(x: str) -> str |
| 727 |
|
| 728 |
f() # E: All overload variants of "f" require at least one argument [call-overload] \ |
| 729 |
# N: Possible overload variants: \ |
| 730 |
# N: def f(x: int) -> int \ |
| 731 |
# N: def f(x: str) -> str |
| 732 |
|
| 733 |
f(1, 1) # E: No overload variant of "f" matches argument types "int", "int" [call-overload] \ |
| 734 |
# N: Possible overload variants: \ |
| 735 |
# N: def f(x: int) -> int \ |
| 736 |
# N: def f(x: str) -> str |
| 737 |
|
| 738 |
[case testErrorCodeOverloadVariantIgnore] |
| 739 |
from typing import overload |
| 740 |
|
| 741 |
@overload |
| 742 |
def f(x: int) -> int: ... |
| 743 |
@overload |
| 744 |
def f(x: str) -> str: ... |
| 745 |
def f(x): |
| 746 |
return x |
| 747 |
|
| 748 |
f(object()) # type: ignore[call-overload] |
| 749 |
|
| 750 |
[case testErrorCodeAnyFromUnfollowedImport] |
| 751 |
# flags: --disallow-any-unimported |
| 752 |
from m import C # type: ignore |
| 753 |
def f(x: C) -> None: # E: Argument 1 to "f" becomes "Any" due to an unfollowed import [no-any-unimported] |
| 754 |
pass |
| 755 |
|
| 756 |
def g() -> C: ... # E: Return type becomes "Any" due to an unfollowed import [no-any-unimported] |
| 757 |
|
| 758 |
[case testErrorCodeReturnAny] |
| 759 |
# flags: --warn-return-any |
| 760 |
def f(): pass |
| 761 |
|
| 762 |
def g() -> int: |
| 763 |
return f() # E: Returning Any from function declared to return "int" [no-any-return] |
| 764 |
|
| 765 |
[case testErrorCodeFormatCall] |
| 766 |
'{:d}'.format('no') # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int") [str-format] |
| 767 |
'{!x}'.format('Hm...') # E: Invalid conversion type "x", must be one of "r", "s" or "a" [str-format] |
| 768 |
'}{'.format() # E: Invalid conversion specifier in format string: unexpected } [str-format] |
| 769 |
|
| 770 |
'%d' % 'no' # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "int | float | SupportsInt") [str-format] |
| 771 |
'%d + %d' % (1, 2, 3) # E: Not all arguments converted during string formatting [str-format] |
| 772 |
|
| 773 |
'{}'.format(b'abc') # E: If x = b'abc' then f"{x}" or "{}".format(x) produces "b'abc'", not "abc". If this is desired behavior, use f"{x!r}" or "{!r}".format(x). Otherwise, decode the bytes [str-bytes-safe] |
| 774 |
'%s' % b'abc' # E: If x = b'abc' then "%s" % x produces "b'abc'", not "abc". If this is desired behavior use "%r" % x. Otherwise, decode the bytes [str-bytes-safe] |
| 775 |
[builtins fixtures/primitives.pyi] |
| 776 |
[typing fixtures/typing-medium.pyi] |
| 777 |
|
| 778 |
[case testErrorCodeIgnoreNamedDefinedNote] |
| 779 |
x: List[int] # type: ignore[name-defined] |
| 780 |
|
| 781 |
[case testErrorCodeProtocolProblemsIgnore] |
| 782 |
from typing import Protocol |
| 783 |
|
| 784 |
class P(Protocol): |
| 785 |
def f(self, x: str) -> None: ... |
| 786 |
|
| 787 |
class A: |
| 788 |
def f(self, x: int) -> None: ... |
| 789 |
|
| 790 |
def g(p: P) -> None: pass |
| 791 |
|
| 792 |
p: A |
| 793 |
g(p) # type: ignore[arg-type] |
| 794 |
[builtins fixtures/tuple.pyi] |
| 795 |
|
| 796 |
[case testErrorCodeNoneReturnNoteIgnore] |
| 797 |
# flags: --disallow-untyped-defs |
| 798 |
|
| 799 |
def f(): # type: ignore[no-untyped-def] |
| 800 |
pass |
| 801 |
|
| 802 |
[case testErrorCodeVarianceNoteIgnore] |
| 803 |
from typing import List |
| 804 |
def f(x: List[object]) -> None: pass |
| 805 |
a = [1] |
| 806 |
f(a) # type: ignore[arg-type] |
| 807 |
[builtins fixtures/list.pyi] |
| 808 |
|
| 809 |
[case testErrorCodeAssignToMethod] |
| 810 |
class A: |
| 811 |
def f(self) -> None: pass |
| 812 |
|
| 813 |
def g(self: A) -> None: pass |
| 814 |
|
| 815 |
A.f = g # E: Cannot assign to a method [method-assign] |
| 816 |
|
| 817 |
[case testErrorCodeDefinedHereNoteIgnore] |
| 818 |
import m |
| 819 |
m.f(kw=1) # type: ignore[call-arg] |
| 820 |
[file m.py] |
| 821 |
def f() -> None: pass |
| 822 |
|
| 823 |
[case testErrorCodeUnionNoteIgnore] |
| 824 |
from typing import Union |
| 825 |
|
| 826 |
class Foo: |
| 827 |
def __add__(self, x: Foo) -> Foo: pass |
| 828 |
def __radd__(self, x: Foo) -> Foo: pass |
| 829 |
|
| 830 |
class Bar: |
| 831 |
def __add__(self, x: Bar) -> Bar: pass |
| 832 |
def __radd__(self, x: Bar) -> Bar: pass |
| 833 |
|
| 834 |
a: Union[Foo, Bar] |
| 835 |
|
| 836 |
a + a # type: ignore[operator] |
| 837 |
a + Foo() # type: ignore[operator] |
| 838 |
Foo() + a # type: ignore[operator] |
| 839 |
|
| 840 |
[case testErrorCodeTypeIgnoreMisspelled1] |
| 841 |
x = y # type: ignored[foo] |
| 842 |
xx = y # type: ignored [foo] |
| 843 |
[out] |
| 844 |
main:1: error: Name "ignored" is not defined [name-defined] |
| 845 |
main:1: error: Name "y" is not defined [name-defined] |
| 846 |
main:2: error: Name "ignored" is not defined [name-defined] |
| 847 |
main:2: error: Name "y" is not defined [name-defined] |
| 848 |
|
| 849 |
[case testErrorCodeTypeIgnoreMisspelled2_no_native_parse] |
| 850 |
x = y # type: int # type: ignored[foo] |
| 851 |
x = y # type: int # type: ignored [foo] |
| 852 |
[out] |
| 853 |
main:1: error: Syntax error in type comment "int" [syntax] |
| 854 |
main:2: error: Syntax error in type comment "int" [syntax] |
| 855 |
|
| 856 |
[case testErrorCode__exit__Return] |
| 857 |
class InvalidReturn: |
| 858 |
def __exit__(self, x, y, z) -> bool: # E: "bool" is invalid as return type for "__exit__" that always returns False [exit-return] \ |
| 859 |
# N: Use "typing.Literal[False]" as the return type or change it to "None" \ |
| 860 |
# N: If return type of "__exit__" implies that it may return True, the context manager may swallow exceptions |
| 861 |
return False |
| 862 |
[builtins fixtures/bool.pyi] |
| 863 |
|
| 864 |
[case testErrorCodeOverloadedOperatorMethod] |
| 865 |
from typing import Optional, overload |
| 866 |
|
| 867 |
class A: |
| 868 |
@overload |
| 869 |
def __add__(self, x: int) -> A: ... |
| 870 |
@overload |
| 871 |
def __add__(self, x: str) -> str: ... |
| 872 |
def __add__(self, x): pass |
| 873 |
|
| 874 |
class B: |
| 875 |
pass |
| 876 |
|
| 877 |
x: Optional[B] |
| 878 |
A() + x # type: ignore[operator] |
| 879 |
|
| 880 |
class C: |
| 881 |
@overload |
| 882 |
def __rsub__(self, x: int) -> A: ... |
| 883 |
@overload |
| 884 |
def __rsub__(self, x: str) -> str: ... |
| 885 |
def __rsub__(self, x): pass |
| 886 |
|
| 887 |
x - C() # type: ignore[operator] |
| 888 |
|
| 889 |
[case testErrorCodeMultiLineBinaryOperatorOperand] |
| 890 |
from typing import Optional |
| 891 |
|
| 892 |
class C: pass |
| 893 |
|
| 894 |
def f() -> Optional[C]: |
| 895 |
return None |
| 896 |
|
| 897 |
f( # type: ignore[operator] |
| 898 |
) + C() |
| 899 |
|
| 900 |
[case testErrorCodeSpecialArgTypeErrors] |
| 901 |
from typing import TypedDict |
| 902 |
|
| 903 |
class C(TypedDict): |
| 904 |
x: int |
| 905 |
|
| 906 |
c: C |
| 907 |
c.setdefault('x', '1') # type: ignore[typeddict-item] |
| 908 |
|
| 909 |
class A: |
| 910 |
pass |
| 911 |
|
| 912 |
class B(A): |
| 913 |
def f(self) -> None: |
| 914 |
super(1, self).foo() # type: ignore[arg-type] |
| 915 |
|
| 916 |
def f(**x: int) -> None: |
| 917 |
pass |
| 918 |
|
| 919 |
f(**1) # type: ignore[arg-type] |
| 920 |
[builtins fixtures/dict.pyi] |
| 921 |
[typing fixtures/typing-typeddict.pyi] |
| 922 |
|
| 923 |
[case testRedundantExpressions] |
| 924 |
# flags: --enable-error-code redundant-expr |
| 925 |
def foo() -> bool: ... |
| 926 |
|
| 927 |
lst = [1, 2, 3, 4] |
| 928 |
|
| 929 |
b = False or foo() # E: Left operand of "or" is always false [redundant-expr] |
| 930 |
c = True and foo() # E: Left operand of "and" is always true [redundant-expr] |
| 931 |
g = 3 if True else 4 # E: If condition is always true [redundant-expr] |
| 932 |
h = 3 if False else 4 # E: If condition is always false [redundant-expr] |
| 933 |
i = [x for x in lst if True] # E: If condition in comprehension is always true [redundant-expr] |
| 934 |
j = [x for x in lst if False] # E: If condition in comprehension is always false [redundant-expr] |
| 935 |
k = [x for x in lst if isinstance(x, int) or foo()] # E: If condition in comprehension is always true [redundant-expr] |
| 936 |
[builtins fixtures/isinstancelist.pyi] |
| 937 |
|
| 938 |
[case testRedundantExprTruthiness] |
| 939 |
# flags: --enable-error-code redundant-expr |
| 940 |
from typing import List |
| 941 |
|
| 942 |
def maybe() -> bool: ... |
| 943 |
|
| 944 |
class Foo: |
| 945 |
def __init__(self, x: List[int]) -> None: |
| 946 |
self.x = x or [] |
| 947 |
|
| 948 |
def method(self) -> int: |
| 949 |
if not self.x or maybe(): |
| 950 |
return 1 |
| 951 |
return 2 |
| 952 |
[builtins fixtures/list.pyi] |
| 953 |
|
| 954 |
[case testRedundantExpressionsUnionWithAny] |
| 955 |
# flags: --enable-error-code redundant-expr |
| 956 |
from typing import Any, Awaitable, Union |
| 957 |
ok_list: list[Union[Any, Awaitable]] |
| 958 |
for y in ok_list: |
| 959 |
z = 1 if isinstance(y, Awaitable) else 2 |
| 960 |
|
| 961 |
bad_list: list[Awaitable] |
| 962 |
for y in bad_list: |
| 963 |
z = 1 if isinstance(y, Awaitable) else 2 # E: If condition is always true [redundant-expr] |
| 964 |
[builtins fixtures/isinstancelist.pyi] |
| 965 |
[typing fixtures/typing-full.pyi] |
| 966 |
|
| 967 |
|
| 968 |
[case testNamedTupleNameMismatch] |
| 969 |
from typing import NamedTuple |
| 970 |
|
| 971 |
Foo = NamedTuple("Bar", []) # E: First argument to namedtuple() should be "Foo", not "Bar" [name-match] |
| 972 |
[builtins fixtures/tuple.pyi] |
| 973 |
|
| 974 |
[case testTypedDictNameMismatch] |
| 975 |
from typing import TypedDict |
| 976 |
|
| 977 |
Foo = TypedDict("Bar", {}) # E: First argument "Bar" to TypedDict() does not match variable name "Foo" [name-match] |
| 978 |
[builtins fixtures/dict.pyi] |
| 979 |
[typing fixtures/typing-typeddict.pyi] |
| 980 |
|
| 981 |
[case testTruthyBool] |
| 982 |
# flags: --enable-error-code truthy-bool --no-local-partial-types |
| 983 |
from typing import List, Union, Any |
| 984 |
|
| 985 |
class Foo: |
| 986 |
pass |
| 987 |
class Bar: |
| 988 |
pass |
| 989 |
|
| 990 |
foo = Foo() |
| 991 |
if foo: # E: "__main__.foo" has type "Foo" which does not implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool] |
| 992 |
pass |
| 993 |
|
| 994 |
not foo # E: "__main__.foo" has type "Foo" which does not implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool] |
| 995 |
|
| 996 |
zero = 0 |
| 997 |
if zero: |
| 998 |
pass |
| 999 |
|
| 1000 |
not zero |
| 1001 |
|
| 1002 |
false = False |
| 1003 |
if false: |
| 1004 |
pass |
| 1005 |
|
| 1006 |
not false |
| 1007 |
|
| 1008 |
null = None |
| 1009 |
if null: |
| 1010 |
pass |
| 1011 |
|
| 1012 |
not null |
| 1013 |
|
| 1014 |
s = '' |
| 1015 |
if s: |
| 1016 |
pass |
| 1017 |
|
| 1018 |
not s |
| 1019 |
|
| 1020 |
good_union: Union[str, int] = 5 |
| 1021 |
if good_union: |
| 1022 |
pass |
| 1023 |
if not good_union: |
| 1024 |
pass |
| 1025 |
|
| 1026 |
not good_union |
| 1027 |
|
| 1028 |
bad_union: Union[Foo, Bar] = Foo() |
| 1029 |
if bad_union: # E: "__main__.bad_union" has type "Foo | Bar" of which no members implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool] |
| 1030 |
pass |
| 1031 |
if not bad_union: # E: "__main__.bad_union" has type "Foo | Bar" of which no members implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool] |
| 1032 |
pass |
| 1033 |
|
| 1034 |
not bad_union # E: "__main__.bad_union" has type "Foo | Bar" of which no members implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool] |
| 1035 |
|
| 1036 |
# 'object' is special and is treated as potentially falsy |
| 1037 |
obj: object = Foo() |
| 1038 |
if obj: |
| 1039 |
pass |
| 1040 |
if not obj: |
| 1041 |
pass |
| 1042 |
|
| 1043 |
not obj |
| 1044 |
|
| 1045 |
lst: List[int] = [] |
| 1046 |
if lst: |
| 1047 |
pass |
| 1048 |
|
| 1049 |
not lst |
| 1050 |
|
| 1051 |
a: Any |
| 1052 |
if a: |
| 1053 |
pass |
| 1054 |
|
| 1055 |
not a |
| 1056 |
|
| 1057 |
any_or_object: Union[object, Any] |
| 1058 |
if any_or_object: |
| 1059 |
pass |
| 1060 |
|
| 1061 |
not any_or_object |
| 1062 |
|
| 1063 |
if (my_foo := Foo()): # E: "__main__.my_foo" has type "Foo" which does not implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool] |
| 1064 |
pass |
| 1065 |
|
| 1066 |
if my_a := (a or Foo()): # E: "__main__.Foo" returns "Foo" which does not implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool] |
| 1067 |
pass |
| 1068 |
[builtins fixtures/list.pyi] |
| 1069 |
|
| 1070 |
[case testTruthyFunctions] |
| 1071 |
def f(): |
| 1072 |
pass |
| 1073 |
if f: # E: Function "f" could always be true in boolean context [truthy-function] |
| 1074 |
pass |
| 1075 |
if not f: # E: Function "f" could always be true in boolean context [truthy-function] |
| 1076 |
pass |
| 1077 |
conditional_result = 'foo' if f else 'bar' # E: Function "f" could always be true in boolean context [truthy-function] |
| 1078 |
|
| 1079 |
not f # E: Function "f" could always be true in boolean context [truthy-function] |
| 1080 |
|
| 1081 |
[case testTruthyIterable] |
| 1082 |
# flags: --enable-error-code truthy-iterable |
| 1083 |
from typing import Iterable |
| 1084 |
def func(var: Iterable[str]) -> None: |
| 1085 |
if var: # E: "var" has type "Iterable[str]" which can always be true in boolean context. Consider using "Collection[str]" instead. [truthy-iterable] |
| 1086 |
... |
| 1087 |
|
| 1088 |
not var # E: "var" has type "Iterable[str]" which can always be true in boolean context. Consider using "Collection[str]" instead. [truthy-iterable] |
| 1089 |
|
| 1090 |
[case testNoOverloadImplementation] |
| 1091 |
from typing import overload |
| 1092 |
|
| 1093 |
@overload # E: An overloaded function outside a stub file must have an implementation [no-overload-impl] |
| 1094 |
def f(arg: int) -> int: |
| 1095 |
... |
| 1096 |
|
| 1097 |
@overload |
| 1098 |
def f(arg: str) -> str: |
| 1099 |
... |
| 1100 |
|
| 1101 |
[case testSliceInDictBuiltin_no_native_parse] |
| 1102 |
# flags: --show-column-numbers |
| 1103 |
b: dict[int, x:y] |
| 1104 |
c: dict[x:y] |
| 1105 |
|
| 1106 |
[builtins fixtures/dict.pyi] |
| 1107 |
[out] |
| 1108 |
main:2:14: error: Invalid type comment or annotation [valid-type] |
| 1109 |
main:2:14: note: did you mean to use ',' instead of ':' ? |
| 1110 |
main:3:4: error: "dict" expects 2 type arguments, but 1 given [type-arg] |
| 1111 |
main:3:9: error: Invalid type comment or annotation [valid-type] |
| 1112 |
main:3:9: note: did you mean to use ',' instead of ':' ? |
| 1113 |
|
| 1114 |
[case testSliceInDictTyping_no_native_parse] |
| 1115 |
# flags: --show-column-numbers |
| 1116 |
from typing import Dict |
| 1117 |
b: Dict[int, x:y] |
| 1118 |
c: Dict[x:y] |
| 1119 |
|
| 1120 |
[builtins fixtures/dict.pyi] |
| 1121 |
[out] |
| 1122 |
main:3:14: error: Invalid type comment or annotation [valid-type] |
| 1123 |
main:3:14: note: did you mean to use ',' instead of ':' ? |
| 1124 |
main:4:4: error: "dict" expects 2 type arguments, but 1 given [type-arg] |
| 1125 |
main:4:9: error: Invalid type comment or annotation [valid-type] |
| 1126 |
main:4:9: note: did you mean to use ',' instead of ':' ? |
| 1127 |
|
| 1128 |
|
| 1129 |
[case testSliceInCustomTensorType] |
| 1130 |
# syntactically mimics torchtyping.TensorType |
| 1131 |
class TensorType: ... |
| 1132 |
t: TensorType["batch":..., float] # type: ignore |
| 1133 |
reveal_type(t) # N: Revealed type is "__main__.TensorType" |
| 1134 |
[builtins fixtures/tuple.pyi] |
| 1135 |
|
| 1136 |
[case testNoteAboutChangedTypedDictErrorCode] |
| 1137 |
from typing import TypedDict |
| 1138 |
class D(TypedDict): |
| 1139 |
x: int |
| 1140 |
|
| 1141 |
def f(d: D, s: str) -> None: |
| 1142 |
d[s] # type: ignore[xyz] \ |
| 1143 |
# E: TypedDict key must be a string literal; expected one of ("x") [literal-required] \ |
| 1144 |
# N: Error code "literal-required" not covered by "type: ignore[xyz]" comment |
| 1145 |
d[s] # E: TypedDict key must be a string literal; expected one of ("x") [literal-required] |
| 1146 |
d[s] # type: ignore[misc] \ |
| 1147 |
# E: TypedDict key must be a string literal; expected one of ("x") [literal-required] \ |
| 1148 |
# N: Error code changed to literal-required; "type: ignore" comment may be out of date |
| 1149 |
d[s] # type: ignore[literal-required] |
| 1150 |
[builtins fixtures/dict.pyi] |
| 1151 |
[typing fixtures/typing-typeddict.pyi] |
| 1152 |
|
| 1153 |
[case testRecommendErrorCode] |
| 1154 |
# type: ignore[whatever] # E: Type ignore with error code is not supported for modules; use `# mypy: disable-error-code="whatever"` [syntax] |
| 1155 |
1 + "asdf" |
| 1156 |
|
| 1157 |
[case testRecommendErrorCode2] |
| 1158 |
# type: ignore[whatever, other] # E: Type ignore with error code is not supported for modules; use `# mypy: disable-error-code="whatever, other"` [syntax] |
| 1159 |
1 + "asdf" |
| 1160 |
|
| 1161 |
[case testShowErrorCodesInConfig] |
| 1162 |
# flags: --config-file tmp/mypy.ini |
| 1163 |
# Test 'show_error_codes = True' in config doesn't raise an exception |
| 1164 |
var: int = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment] |
| 1165 |
|
| 1166 |
[file mypy.ini] |
| 1167 |
\[mypy] |
| 1168 |
show_error_codes = True |
| 1169 |
|
| 1170 |
[case testErrorCodeUnsafeSuper_no_empty] |
| 1171 |
from abc import abstractmethod |
| 1172 |
|
| 1173 |
class Base: |
| 1174 |
@abstractmethod |
| 1175 |
def meth(self) -> int: |
| 1176 |
raise NotImplementedError() |
| 1177 |
class Sub(Base): |
| 1178 |
def meth(self) -> int: |
| 1179 |
return super().meth() # E: Call to abstract method "meth" of "Base" with trivial body via super() is unsafe [safe-super] |
| 1180 |
[builtins fixtures/exception.pyi] |
| 1181 |
|
| 1182 |
[case testDedicatedErrorCodeForEmpty_no_empty] |
| 1183 |
from typing import Optional |
| 1184 |
def foo() -> int: ... # E: Missing return statement [empty-body] |
| 1185 |
def bar() -> None: ... |
| 1186 |
# This is inconsistent with how --warn-no-return behaves in general |
| 1187 |
# but we want to minimize fallout of finally handling empty bodies. |
| 1188 |
def baz() -> Optional[int]: ... # OK |
| 1189 |
|
| 1190 |
[case testDedicatedErrorCodeTypeAbstract] |
| 1191 |
import abc |
| 1192 |
from typing import TypeVar, Type |
| 1193 |
|
| 1194 |
class C(abc.ABC): |
| 1195 |
@abc.abstractmethod |
| 1196 |
def foo(self) -> None: ... |
| 1197 |
|
| 1198 |
T = TypeVar("T") |
| 1199 |
def test(tp: Type[T]) -> T: ... |
| 1200 |
test(C) # E: Only concrete class can be given where "type[C]" is expected [type-abstract] |
| 1201 |
|
| 1202 |
class D(C): |
| 1203 |
@abc.abstractmethod |
| 1204 |
def bar(self) -> None: ... |
| 1205 |
cls: Type[C] = D # E: Can only assign concrete classes to a variable of type "type[C]" [type-abstract] |
| 1206 |
|
| 1207 |
[case testUncheckedAnnotationCodeShown] |
| 1208 |
def f(): |
| 1209 |
x: int = "no" # N: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked] |
| 1210 |
|
| 1211 |
[case testUncheckedAnnotationSuppressed] |
| 1212 |
# flags: --disable-error-code=annotation-unchecked |
| 1213 |
def f(): |
| 1214 |
x: int = "no" # No warning here |
| 1215 |
|
| 1216 |
[case testMethodAssignmentSuppressed] |
| 1217 |
# flags: --disable-error-code=method-assign |
| 1218 |
class A: |
| 1219 |
def f(self) -> None: pass |
| 1220 |
def g(self) -> None: pass |
| 1221 |
|
| 1222 |
def h(self: A) -> None: pass |
| 1223 |
|
| 1224 |
A.f = h |
| 1225 |
# This actually works at runtime, but there is no way to express this in current type system |
| 1226 |
A.f = A().g # E: Incompatible types in assignment (expression has type "Callable[[], None]", variable has type "Callable[[A], None]") [assignment] |
| 1227 |
|
| 1228 |
[case testMethodAssignCoveredByAssignmentIgnore] |
| 1229 |
class A: |
| 1230 |
def f(self) -> None: pass |
| 1231 |
def h(self: A) -> None: pass |
| 1232 |
A.f = h # type: ignore[assignment] |
| 1233 |
|
| 1234 |
[case testMethodAssignCoveredByAssignmentFlag] |
| 1235 |
# flags: --disable-error-code=assignment |
| 1236 |
class A: |
| 1237 |
def f(self) -> None: pass |
| 1238 |
def h(self: A) -> None: pass |
| 1239 |
A.f = h # OK |
| 1240 |
|
| 1241 |
[case testMethodAssignCoveredByAssignmentUnused] |
| 1242 |
# flags: --warn-unused-ignores |
| 1243 |
class A: |
| 1244 |
def f(self) -> None: pass |
| 1245 |
def h(self: A) -> None: pass |
| 1246 |
A.f = h # type: ignore[assignment] # E: Unused "type: ignore" comment, use narrower [method-assign] instead of [assignment] code [unused-ignore] |
| 1247 |
|
| 1248 |
[case testUnusedIgnoreEnableCode] |
| 1249 |
# flags: --enable-error-code=unused-ignore |
| 1250 |
x = 1 # type: ignore # E: Unused "type: ignore" comment [unused-ignore] |
| 1251 |
|
| 1252 |
[case testErrorCodeUnsafeOverloadError] |
| 1253 |
from typing import overload, Union |
| 1254 |
|
| 1255 |
@overload |
| 1256 |
def unsafe_func(x: int) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types [overload-overlap] |
| 1257 |
@overload |
| 1258 |
def unsafe_func(x: object) -> str: ... |
| 1259 |
def unsafe_func(x: object) -> Union[int, str]: |
| 1260 |
if isinstance(x, int): |
| 1261 |
return 42 |
| 1262 |
else: |
| 1263 |
return "some string" |
| 1264 |
[builtins fixtures/isinstancelist.pyi] |
| 1265 |
|
| 1266 |
|
| 1267 |
### |
| 1268 |
# unimported-reveal |
| 1269 |
### |
| 1270 |
|
| 1271 |
[case testUnimportedRevealType] |
| 1272 |
# flags: --enable-error-code=unimported-reveal |
| 1273 |
x = 1 |
| 1274 |
reveal_type(x) |
| 1275 |
[out] |
| 1276 |
main:3: error: Name "reveal_type" is not defined [unimported-reveal] |
| 1277 |
main:3: note: Did you forget to import it from "typing_extensions"? (Suggestion: "from typing_extensions import reveal_type") |
| 1278 |
main:3: note: Revealed type is "builtins.int" |
| 1279 |
[builtins fixtures/isinstancelist.pyi] |
| 1280 |
|
| 1281 |
[case testUnimportedRevealTypePy311] |
| 1282 |
# flags: --enable-error-code=unimported-reveal --python-version=3.11 |
| 1283 |
x = 1 |
| 1284 |
reveal_type(x) |
| 1285 |
[out] |
| 1286 |
main:3: error: Name "reveal_type" is not defined [unimported-reveal] |
| 1287 |
main:3: note: Did you forget to import it from "typing"? (Suggestion: "from typing import reveal_type") |
| 1288 |
main:3: note: Revealed type is "builtins.int" |
| 1289 |
[builtins fixtures/isinstancelist.pyi] |
| 1290 |
|
| 1291 |
[case testUnimportedRevealTypeInUncheckedFunc] |
| 1292 |
# flags: --enable-error-code=unimported-reveal |
| 1293 |
def unchecked(): |
| 1294 |
x = 1 |
| 1295 |
reveal_type(x) |
| 1296 |
[out] |
| 1297 |
main:4: error: Name "reveal_type" is not defined [unimported-reveal] |
| 1298 |
main:4: note: Did you forget to import it from "typing_extensions"? (Suggestion: "from typing_extensions import reveal_type") |
| 1299 |
main:4: note: Revealed type is "Any" |
| 1300 |
main:4: note: 'reveal_type' always outputs 'Any' in unchecked functions |
| 1301 |
[builtins fixtures/isinstancelist.pyi] |
| 1302 |
|
| 1303 |
[case testUnimportedRevealTypeImportedTypingExtensions] |
| 1304 |
# flags: --enable-error-code=unimported-reveal |
| 1305 |
from typing_extensions import reveal_type |
| 1306 |
x = 1 |
| 1307 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 1308 |
[builtins fixtures/isinstancelist.pyi] |
| 1309 |
|
| 1310 |
[case testUnimportedRevealTypeImportedTyping311] |
| 1311 |
# flags: --enable-error-code=unimported-reveal --python-version=3.11 |
| 1312 |
from typing import reveal_type |
| 1313 |
x = 1 |
| 1314 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 1315 |
[builtins fixtures/isinstancelist.pyi] |
| 1316 |
[typing fixtures/typing-full.pyi] |
| 1317 |
|
| 1318 |
[case testUnimportedRevealLocals] |
| 1319 |
# flags: --enable-error-code=unimported-reveal |
| 1320 |
x = 1 |
| 1321 |
reveal_locals() |
| 1322 |
[out] |
| 1323 |
main:3: note: Revealed local types are: |
| 1324 |
main:3: note: x: builtins.int |
| 1325 |
main:3: error: Name "reveal_locals" is not defined [unimported-reveal] |
| 1326 |
[builtins fixtures/isinstancelist.pyi] |
| 1327 |
|
| 1328 |
[case testCovariantMutableOverride] |
| 1329 |
# flags: --enable-error-code=mutable-override |
| 1330 |
from typing import Any |
| 1331 |
|
| 1332 |
class C: |
| 1333 |
x: float |
| 1334 |
y: float |
| 1335 |
z: float |
| 1336 |
w: Any |
| 1337 |
@property |
| 1338 |
def foo(self) -> float: ... |
| 1339 |
@property |
| 1340 |
def bar(self) -> float: ... |
| 1341 |
@bar.setter |
| 1342 |
def bar(self, val: float) -> None: ... |
| 1343 |
baz: float |
| 1344 |
bad1: float |
| 1345 |
bad2: float |
| 1346 |
class D(C): |
| 1347 |
x: int # E: Covariant override of a mutable attribute (base class "C" defined the type as "float", expression has type "int") [mutable-override] |
| 1348 |
y: float |
| 1349 |
z: Any |
| 1350 |
w: float |
| 1351 |
foo: int |
| 1352 |
bar: int # E: Covariant override of a mutable attribute (base class "C" defined the type as "float", expression has type "int") [mutable-override] |
| 1353 |
def one(self) -> None: |
| 1354 |
self.baz = 5 |
| 1355 |
bad1 = 5 # E: Covariant override of a mutable attribute (base class "C" defined the type as "float", expression has type "int") [mutable-override] |
| 1356 |
def other(self) -> None: |
| 1357 |
self.bad2: int = 5 # E: Covariant override of a mutable attribute (base class "C" defined the type as "float", expression has type "int") [mutable-override] |
| 1358 |
[builtins fixtures/property.pyi] |
| 1359 |
|
| 1360 |
[case testNarrowedTypeNotSubtype] |
| 1361 |
from typing_extensions import TypeIs |
| 1362 |
|
| 1363 |
def f(x: str) -> TypeIs[int]: # E: Narrowed type "int" is not a subtype of input type "str" [narrowed-type-not-subtype] |
| 1364 |
pass |
| 1365 |
|
| 1366 |
[builtins fixtures/tuple.pyi] |
| 1367 |
|
| 1368 |
[case testDynamicMetaclass] |
| 1369 |
class A(metaclass=type(tuple)): pass # E: Dynamic metaclass not supported for "A" [metaclass] |
| 1370 |
[builtins fixtures/tuple.pyi] |
| 1371 |
|
| 1372 |
[case testMetaclassOfTypeAny] |
| 1373 |
# mypy: disallow-subclassing-any=True |
| 1374 |
from typing import Any |
| 1375 |
foo: Any = ... |
| 1376 |
class A(metaclass=foo): pass # E: Class cannot use "foo" as a metaclass (has type "Any") [metaclass] |
| 1377 |
|
| 1378 |
[case testMetaclassOfWrongType] |
| 1379 |
class Foo: |
| 1380 |
bar = 1 |
| 1381 |
class A2(metaclass=Foo.bar): pass # E: Invalid metaclass "Foo.bar" [metaclass] |
| 1382 |
|
| 1383 |
[case testMetaclassNotTypeSubclass] |
| 1384 |
class M: pass |
| 1385 |
class A(metaclass=M): pass # E: Metaclasses not inheriting from "type" are not supported [metaclass] |
| 1386 |
|
| 1387 |
[case testMultipleMetaclasses] |
| 1388 |
import six |
| 1389 |
class M1(type): pass |
| 1390 |
|
| 1391 |
@six.add_metaclass(M1) |
| 1392 |
class A1(metaclass=M1): pass # E: Multiple metaclass definitions [metaclass] |
| 1393 |
|
| 1394 |
class A2(six.with_metaclass(M1), metaclass=M1): pass # E: Multiple metaclass definitions [metaclass] |
| 1395 |
|
| 1396 |
@six.add_metaclass(M1) |
| 1397 |
class A3(six.with_metaclass(M1)): pass # E: Multiple metaclass definitions [metaclass] |
| 1398 |
[builtins fixtures/tuple.pyi] |
| 1399 |
|
| 1400 |
[case testInvalidMetaclassStructure] |
| 1401 |
class X(type): pass |
| 1402 |
class Y(type): pass |
| 1403 |
class A(metaclass=X): pass |
| 1404 |
class B(A, metaclass=Y): pass # E: Metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases [metaclass] \ |
| 1405 |
# N: "__main__.Y" (metaclass of "__main__.B") conflicts with "__main__.X" (metaclass of "__main__.A") |
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
[case testOverloadedFunctionSignature] |
| 1411 |
from typing import overload, Union |
| 1412 |
|
| 1413 |
@overload |
| 1414 |
def process(response1: float,response2: float) -> float: |
| 1415 |
... |
| 1416 |
@overload |
| 1417 |
def process(response1: int,response2: int) -> int: # E: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader [overload-cannot-match] |
| 1418 |
... |
| 1419 |
|
| 1420 |
def process(response1,response2)-> Union[float,int]: |
| 1421 |
return response1 + response2 |