| 1 |
-- Type checker test cases for abstract classes. |
| 2 |
|
| 3 |
|
| 4 |
-- Subtyping with abstract classes |
| 5 |
-- ------------------------------- |
| 6 |
|
| 7 |
|
| 8 |
[case testAbstractClassSubclasses] |
| 9 |
|
| 10 |
from abc import abstractmethod, ABCMeta |
| 11 |
|
| 12 |
i: I |
| 13 |
j: J |
| 14 |
a: A |
| 15 |
b: B |
| 16 |
c: C |
| 17 |
|
| 18 |
def f(): i, j, a, b, c # Prevent redefinition |
| 19 |
|
| 20 |
j = c # E: Incompatible types in assignment (expression has type "C", variable has type "J") |
| 21 |
a = i # E: Incompatible types in assignment (expression has type "I", variable has type "A") |
| 22 |
a = j # E: Incompatible types in assignment (expression has type "J", variable has type "A") |
| 23 |
b = i # E: Incompatible types in assignment (expression has type "I", variable has type "B") |
| 24 |
|
| 25 |
i = a |
| 26 |
i = b |
| 27 |
i = c |
| 28 |
j = a |
| 29 |
j = b |
| 30 |
a = b |
| 31 |
|
| 32 |
class I(metaclass=ABCMeta): |
| 33 |
@abstractmethod |
| 34 |
def f(self): pass |
| 35 |
class J(metaclass=ABCMeta): |
| 36 |
@abstractmethod |
| 37 |
def g(self): pass |
| 38 |
class A(I, J): pass |
| 39 |
class B(A): pass |
| 40 |
class C(I): pass |
| 41 |
[builtins fixtures/tuple.pyi] |
| 42 |
|
| 43 |
[case testAbstractClassSubtypingViaExtension] |
| 44 |
|
| 45 |
from abc import abstractmethod, ABCMeta |
| 46 |
|
| 47 |
i: I |
| 48 |
j: J |
| 49 |
a: A |
| 50 |
o: object |
| 51 |
|
| 52 |
def f(): i, j, a, o # Prevent redefinition |
| 53 |
|
| 54 |
j = i # E: Incompatible types in assignment (expression has type "I", variable has type "J") |
| 55 |
a = i # E: Incompatible types in assignment (expression has type "I", variable has type "A") |
| 56 |
a = j # E: Incompatible types in assignment (expression has type "J", variable has type "A") |
| 57 |
i = o # E: Incompatible types in assignment (expression has type "object", variable has type "I") |
| 58 |
j = o # E: Incompatible types in assignment (expression has type "object", variable has type "J") |
| 59 |
|
| 60 |
i = a |
| 61 |
j = a |
| 62 |
i = j |
| 63 |
o = i |
| 64 |
o = j |
| 65 |
|
| 66 |
class I(metaclass=ABCMeta): |
| 67 |
@abstractmethod |
| 68 |
def f(self): pass |
| 69 |
class J(I): pass |
| 70 |
class A(J): pass |
| 71 |
[builtins fixtures/tuple.pyi] |
| 72 |
|
| 73 |
[case testInheritingAbstractClassInSubclass] |
| 74 |
from abc import abstractmethod, ABCMeta |
| 75 |
|
| 76 |
i: I |
| 77 |
a: A |
| 78 |
b: B |
| 79 |
|
| 80 |
if int(): |
| 81 |
i = a # E: Incompatible types in assignment (expression has type "A", variable has type "I") |
| 82 |
if int(): |
| 83 |
b = a # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 84 |
|
| 85 |
if int(): |
| 86 |
a = b |
| 87 |
if int(): |
| 88 |
i = b |
| 89 |
|
| 90 |
class I(metaclass=ABCMeta): |
| 91 |
@abstractmethod |
| 92 |
def f(self): pass |
| 93 |
class A: pass |
| 94 |
class B(A, I): pass |
| 95 |
|
| 96 |
|
| 97 |
-- Abstract class objects |
| 98 |
-- ---------------------- |
| 99 |
|
| 100 |
|
| 101 |
[case testAbstractClassAsTypeObject] |
| 102 |
|
| 103 |
from abc import abstractmethod, ABCMeta |
| 104 |
|
| 105 |
class I(metaclass=ABCMeta): |
| 106 |
@abstractmethod |
| 107 |
def f(self): pass |
| 108 |
|
| 109 |
o: object |
| 110 |
t: type |
| 111 |
|
| 112 |
o = I |
| 113 |
t = I |
| 114 |
|
| 115 |
[case testAbstractClassInCasts] |
| 116 |
from typing import cast |
| 117 |
from abc import abstractmethod, ABCMeta |
| 118 |
|
| 119 |
class I(metaclass=ABCMeta): |
| 120 |
@abstractmethod |
| 121 |
def f(self): pass |
| 122 |
class A(I): pass |
| 123 |
class B: pass |
| 124 |
|
| 125 |
i: I |
| 126 |
a: A |
| 127 |
b: B |
| 128 |
o: object |
| 129 |
|
| 130 |
if int(): |
| 131 |
a = cast(I, o) # E: Incompatible types in assignment (expression has type "I", variable has type "A") |
| 132 |
if int(): |
| 133 |
b = cast(B, i) # Ok; a subclass of B might inherit I |
| 134 |
if int(): |
| 135 |
i = cast(I, b) # Ok; a subclass of B might inherit I |
| 136 |
|
| 137 |
if int(): |
| 138 |
i = cast(I, o) |
| 139 |
if int(): |
| 140 |
i = cast(I, a) |
| 141 |
[builtins fixtures/tuple.pyi] |
| 142 |
|
| 143 |
[case testInstantiatingClassThatImplementsAbstractMethod] |
| 144 |
from abc import abstractmethod, ABCMeta |
| 145 |
import typing |
| 146 |
class A(metaclass=ABCMeta): |
| 147 |
@abstractmethod |
| 148 |
def f(self): pass |
| 149 |
class B(A): |
| 150 |
def f(self): pass |
| 151 |
B() |
| 152 |
[out] |
| 153 |
|
| 154 |
[case testInstantiatingAbstractClass] |
| 155 |
from abc import abstractmethod, ABCMeta |
| 156 |
import typing |
| 157 |
class A(metaclass=ABCMeta): pass |
| 158 |
class B(metaclass=ABCMeta): |
| 159 |
@abstractmethod |
| 160 |
def f(self): pass |
| 161 |
A() # OK |
| 162 |
B() # E: Cannot instantiate abstract class "B" with abstract attribute "f" |
| 163 |
[out] |
| 164 |
|
| 165 |
[case testInstantiatingClassWithInheritedAbstractMethod] |
| 166 |
from abc import abstractmethod, ABCMeta |
| 167 |
import typing |
| 168 |
class A(metaclass=ABCMeta): |
| 169 |
@abstractmethod |
| 170 |
def f(self): pass |
| 171 |
@abstractmethod |
| 172 |
def g(self): pass |
| 173 |
class B(A): pass |
| 174 |
B() # E: Cannot instantiate abstract class "B" with abstract attributes "f" and "g" |
| 175 |
[out] |
| 176 |
|
| 177 |
[case testInstantiationAbstractsInTypeForFunctions] |
| 178 |
from typing import Type |
| 179 |
from abc import abstractmethod |
| 180 |
|
| 181 |
class A: |
| 182 |
@abstractmethod |
| 183 |
def m(self) -> None: pass |
| 184 |
class B(A): pass |
| 185 |
class C(B): |
| 186 |
def m(self) -> None: |
| 187 |
pass |
| 188 |
|
| 189 |
def f(cls: Type[A]) -> A: |
| 190 |
return cls() # OK |
| 191 |
def g() -> A: |
| 192 |
return A() # E: Cannot instantiate abstract class "A" with abstract attribute "m" |
| 193 |
|
| 194 |
f(A) # E: Only concrete class can be given where "type[A]" is expected |
| 195 |
f(B) # E: Only concrete class can be given where "type[A]" is expected |
| 196 |
f(C) # OK |
| 197 |
x: Type[B] |
| 198 |
f(x) # OK |
| 199 |
[out] |
| 200 |
|
| 201 |
[case testAbstractTypeInADict] |
| 202 |
from typing import Dict, Type |
| 203 |
from abc import abstractmethod |
| 204 |
|
| 205 |
class Class: |
| 206 |
@abstractmethod |
| 207 |
def method(self) -> None: |
| 208 |
pass |
| 209 |
|
| 210 |
my_dict_init: Dict[int, Type[Class]] = {0: Class} # E: Only concrete class can be given where "tuple[int, type[Class]]" is expected |
| 211 |
|
| 212 |
class Child(Class): |
| 213 |
def method(self) -> None: ... |
| 214 |
|
| 215 |
other_dict_init: Dict[int, Type[Class]] = {0: Child} # ok |
| 216 |
[builtins fixtures/dict.pyi] |
| 217 |
[out] |
| 218 |
|
| 219 |
[case testInstantiationAbstractsInTypeForAliases] |
| 220 |
from typing import Type |
| 221 |
from abc import abstractmethod |
| 222 |
|
| 223 |
class A: |
| 224 |
@abstractmethod |
| 225 |
def m(self) -> None: pass |
| 226 |
class B(A): pass |
| 227 |
class C(B): |
| 228 |
def m(self) -> None: |
| 229 |
pass |
| 230 |
|
| 231 |
def f(cls: Type[A]) -> A: |
| 232 |
return cls() # OK |
| 233 |
|
| 234 |
Alias = A |
| 235 |
GoodAlias = C |
| 236 |
Alias() # E: Cannot instantiate abstract class "A" with abstract attribute "m" |
| 237 |
GoodAlias() |
| 238 |
f(Alias) # E: Only concrete class can be given where "type[A]" is expected |
| 239 |
f(GoodAlias) |
| 240 |
[out] |
| 241 |
|
| 242 |
[case testInstantiationAbstractsInTypeForVariables] |
| 243 |
# flags: --no-strict-optional |
| 244 |
from typing import Type, overload |
| 245 |
from abc import abstractmethod |
| 246 |
|
| 247 |
class A: |
| 248 |
@abstractmethod |
| 249 |
def m(self) -> None: pass |
| 250 |
class B(A): pass |
| 251 |
class C(B): |
| 252 |
def m(self) -> None: |
| 253 |
pass |
| 254 |
|
| 255 |
var: Type[A] |
| 256 |
var() |
| 257 |
if int(): |
| 258 |
var = A # E: Can only assign concrete classes to a variable of type "type[A]" |
| 259 |
if int(): |
| 260 |
var = B # E: Can only assign concrete classes to a variable of type "type[A]" |
| 261 |
if int(): |
| 262 |
var = C # OK |
| 263 |
|
| 264 |
var_old = None # type: Type[A] # Old syntax for variable annotations |
| 265 |
var_old() |
| 266 |
if int(): |
| 267 |
var_old = A # E: Can only assign concrete classes to a variable of type "type[A]" |
| 268 |
if int(): |
| 269 |
var_old = B # E: Can only assign concrete classes to a variable of type "type[A]" |
| 270 |
if int(): |
| 271 |
var_old = C # OK |
| 272 |
|
| 273 |
class D(A): |
| 274 |
@overload |
| 275 |
def __new__(cls, a) -> "D": ... |
| 276 |
@overload |
| 277 |
def __new__(cls) -> "D": ... |
| 278 |
def __new__(cls, a=None) -> "D": ... |
| 279 |
if int(): |
| 280 |
var = D # E: Can only assign concrete classes to a variable of type "type[A]" |
| 281 |
[out] |
| 282 |
|
| 283 |
[case testInstantiationAbstractsInTypeForClassMethods] |
| 284 |
from typing import Type |
| 285 |
from abc import abstractmethod |
| 286 |
|
| 287 |
class Logger: |
| 288 |
@staticmethod |
| 289 |
def log(a: Type[C]): |
| 290 |
pass |
| 291 |
class C: |
| 292 |
@classmethod |
| 293 |
def action(cls) -> None: |
| 294 |
cls() #OK for classmethods |
| 295 |
Logger.log(cls) #OK for classmethods |
| 296 |
@abstractmethod |
| 297 |
def m(self) -> None: |
| 298 |
pass |
| 299 |
[builtins fixtures/classmethod.pyi] |
| 300 |
[out] |
| 301 |
|
| 302 |
[case testInstantiatingClassWithInheritedAbstractMethodAndSuppression] |
| 303 |
from abc import abstractmethod, ABCMeta |
| 304 |
import typing |
| 305 |
class A(metaclass=ABCMeta): |
| 306 |
@abstractmethod |
| 307 |
def a(self): pass |
| 308 |
@abstractmethod |
| 309 |
def b(self): pass |
| 310 |
@abstractmethod |
| 311 |
def c(self): pass |
| 312 |
@abstractmethod |
| 313 |
def d(self): pass |
| 314 |
@abstractmethod |
| 315 |
def e(self): pass |
| 316 |
@abstractmethod |
| 317 |
def f(self): pass |
| 318 |
@abstractmethod |
| 319 |
def g(self): pass |
| 320 |
@abstractmethod |
| 321 |
def h(self): pass |
| 322 |
@abstractmethod |
| 323 |
def i(self): pass |
| 324 |
@abstractmethod |
| 325 |
def j(self): pass |
| 326 |
a = A() # E: Cannot instantiate abstract class "A" with abstract attributes "a", "b", ... and "j" (7 methods suppressed) |
| 327 |
[out] |
| 328 |
|
| 329 |
|
| 330 |
-- Implementing abstract methods |
| 331 |
-- ----------------------------- |
| 332 |
|
| 333 |
|
| 334 |
[case testImplementingAbstractMethod] |
| 335 |
from abc import abstractmethod, ABCMeta |
| 336 |
import typing |
| 337 |
class A(metaclass=ABCMeta): |
| 338 |
@abstractmethod |
| 339 |
def f(self, x: int) -> int: pass |
| 340 |
@abstractmethod |
| 341 |
def g(self, x: int) -> int: pass |
| 342 |
class B(A): |
| 343 |
def f(self, x: str) -> int: \ |
| 344 |
# E: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "int" \ |
| 345 |
# N: This violates the Liskov substitution principle \ |
| 346 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 347 |
return 0 |
| 348 |
def g(self, x: int) -> int: return 0 |
| 349 |
[out] |
| 350 |
|
| 351 |
[case testImplementingAbstractMethodWithMultipleBaseClasses] |
| 352 |
from abc import abstractmethod, ABCMeta |
| 353 |
import typing |
| 354 |
class I(metaclass=ABCMeta): |
| 355 |
@abstractmethod |
| 356 |
def f(self, x: int) -> int: pass |
| 357 |
class J(metaclass=ABCMeta): |
| 358 |
@abstractmethod |
| 359 |
def g(self, x: str) -> str: pass |
| 360 |
class A(I, J): |
| 361 |
def f(self, x: str) -> int: return 0 \ |
| 362 |
# E: Argument 1 of "f" is incompatible with supertype "I"; supertype defines the argument type as "int" \ |
| 363 |
# N: This violates the Liskov substitution principle \ |
| 364 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 365 |
def g(self, x: str) -> int: return 0 \ |
| 366 |
# E: Return type "int" of "g" incompatible with return type "str" in supertype "J" |
| 367 |
def h(self) -> int: return 0 # Not related to any base class |
| 368 |
[out] |
| 369 |
|
| 370 |
[case testImplementingAbstractMethodWithExtension] |
| 371 |
from abc import abstractmethod, ABCMeta |
| 372 |
import typing |
| 373 |
class J(metaclass=ABCMeta): |
| 374 |
@abstractmethod |
| 375 |
def f(self, x: int) -> int: pass |
| 376 |
class I(J): pass |
| 377 |
class A(I): |
| 378 |
def f(self, x: str) -> int: return 0 \ |
| 379 |
# E: Argument 1 of "f" is incompatible with supertype "J"; supertype defines the argument type as "int" \ |
| 380 |
# N: This violates the Liskov substitution principle \ |
| 381 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 382 |
[out] |
| 383 |
|
| 384 |
[case testInvalidOverridingAbstractMethod] |
| 385 |
from abc import abstractmethod, ABCMeta |
| 386 |
import typing |
| 387 |
class J(metaclass=ABCMeta): |
| 388 |
@abstractmethod |
| 389 |
def f(self, x: 'J') -> None: pass |
| 390 |
class I(J): |
| 391 |
@abstractmethod |
| 392 |
def f(self, x: 'I') -> None: pass # E: Argument 1 of "f" is incompatible with supertype "J"; supertype defines the argument type as "J" \ |
| 393 |
# N: This violates the Liskov substitution principle \ |
| 394 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 395 |
[out] |
| 396 |
|
| 397 |
[case testAbstractClassCoAndContraVariance] |
| 398 |
from abc import abstractmethod, ABCMeta |
| 399 |
import typing |
| 400 |
class I(metaclass=ABCMeta): |
| 401 |
@abstractmethod |
| 402 |
def f(self, a: A) -> 'I': pass |
| 403 |
@abstractmethod |
| 404 |
def g(self, a: A) -> 'I': pass |
| 405 |
@abstractmethod |
| 406 |
def h(self, a: 'I') -> A: pass |
| 407 |
class A(I): |
| 408 |
def h(self, a: 'A') -> 'I': # Fail |
| 409 |
return A() |
| 410 |
def f(self, a: 'I') -> 'I': |
| 411 |
return A() |
| 412 |
def g(self, a: 'A') -> 'A': |
| 413 |
return A() |
| 414 |
[out] |
| 415 |
main:11: error: Return type "I" of "h" incompatible with return type "A" in supertype "I" |
| 416 |
main:11: error: Argument 1 of "h" is incompatible with supertype "I"; supertype defines the argument type as "I" |
| 417 |
main:11: note: This violates the Liskov substitution principle |
| 418 |
main:11: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 419 |
|
| 420 |
|
| 421 |
-- Accessing abstract members |
| 422 |
-- -------------------------- |
| 423 |
|
| 424 |
|
| 425 |
[case testAccessingAbstractMethod] |
| 426 |
from abc import abstractmethod, ABCMeta |
| 427 |
|
| 428 |
class I(metaclass=ABCMeta): |
| 429 |
@abstractmethod |
| 430 |
def f(self, a: int) -> str: pass |
| 431 |
|
| 432 |
i: I |
| 433 |
a: int |
| 434 |
b: str |
| 435 |
|
| 436 |
if int(): |
| 437 |
a = i.f(a) # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 438 |
if int(): |
| 439 |
b = i.f(b) # E: Argument 1 to "f" of "I" has incompatible type "str"; expected "int" |
| 440 |
i.g() # E: "I" has no attribute "g" |
| 441 |
|
| 442 |
if int(): |
| 443 |
b = i.f(a) |
| 444 |
[builtins fixtures/tuple.pyi] |
| 445 |
|
| 446 |
[case testAccessingInheritedAbstractMethod] |
| 447 |
from abc import abstractmethod, ABCMeta |
| 448 |
|
| 449 |
class J(metaclass=ABCMeta): |
| 450 |
@abstractmethod |
| 451 |
def f(self, a: int) -> str: pass |
| 452 |
class I(J): pass |
| 453 |
|
| 454 |
i: I |
| 455 |
a: int |
| 456 |
b: str |
| 457 |
|
| 458 |
if int(): |
| 459 |
a = i.f(1) # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 460 |
if int(): |
| 461 |
b = i.f(1) |
| 462 |
|
| 463 |
|
| 464 |
-- Any (dynamic) types |
| 465 |
-- ------------------- |
| 466 |
[builtins fixtures/tuple.pyi] |
| 467 |
|
| 468 |
|
| 469 |
[case testAbstractClassWithAllDynamicTypes] |
| 470 |
from abc import abstractmethod, ABCMeta |
| 471 |
import typing |
| 472 |
class I(metaclass=ABCMeta): |
| 473 |
@abstractmethod |
| 474 |
def f(self, x): pass |
| 475 |
@abstractmethod |
| 476 |
def g(self, x): pass |
| 477 |
class A(I): |
| 478 |
def f(self, x): pass |
| 479 |
def g(self, x, y) -> None: pass # Fail |
| 480 |
[out] |
| 481 |
main:10: error: Signature of "g" incompatible with supertype "I" |
| 482 |
main:10: note: Superclass: |
| 483 |
main:10: note: def g(self, x: Any) -> Any |
| 484 |
main:10: note: Subclass: |
| 485 |
main:10: note: def g(self, x: Any, y: Any) -> None |
| 486 |
|
| 487 |
[case testAbstractClassWithAllDynamicTypes2] |
| 488 |
from abc import abstractmethod, ABCMeta |
| 489 |
import typing |
| 490 |
class I(metaclass=ABCMeta): |
| 491 |
@abstractmethod |
| 492 |
def f(self, x): pass |
| 493 |
@abstractmethod |
| 494 |
def g(self, x): pass |
| 495 |
class A(I): |
| 496 |
def f(self, x): pass |
| 497 |
def g(self, x, y): pass |
| 498 |
[out] |
| 499 |
|
| 500 |
[case testAbstractClassWithImplementationUsingDynamicTypes] |
| 501 |
from abc import abstractmethod, ABCMeta |
| 502 |
import typing |
| 503 |
class I(metaclass=ABCMeta): |
| 504 |
@abstractmethod |
| 505 |
def f(self, x: int) -> None: pass |
| 506 |
@abstractmethod |
| 507 |
def g(self, x: int) -> None: pass |
| 508 |
class A(I): |
| 509 |
def f(self, x): pass |
| 510 |
def g(self, x, y): pass |
| 511 |
[out] |
| 512 |
|
| 513 |
|
| 514 |
-- Special cases |
| 515 |
-- ------------- |
| 516 |
|
| 517 |
|
| 518 |
[case testMultipleAbstractBases] |
| 519 |
from abc import abstractmethod, ABCMeta |
| 520 |
import typing |
| 521 |
class A(metaclass=ABCMeta): |
| 522 |
@abstractmethod |
| 523 |
def f(self) -> None: pass |
| 524 |
class B(metaclass=ABCMeta): |
| 525 |
@abstractmethod |
| 526 |
def g(self) -> None: pass |
| 527 |
class C(A, B): |
| 528 |
@abstractmethod |
| 529 |
def h(self) -> None: pass |
| 530 |
|
| 531 |
[case testMemberAccessWithMultipleAbstractBaseClasses] |
| 532 |
|
| 533 |
from abc import abstractmethod, ABCMeta |
| 534 |
|
| 535 |
class A(metaclass=ABCMeta): |
| 536 |
@abstractmethod |
| 537 |
def f(self) -> None: pass |
| 538 |
class B(metaclass=ABCMeta): |
| 539 |
@abstractmethod |
| 540 |
def g(self) -> None: pass |
| 541 |
class C(A, B): pass |
| 542 |
x: C |
| 543 |
x.f() |
| 544 |
x.g() |
| 545 |
x.f(x) # E: Too many arguments for "f" of "A" |
| 546 |
x.g(x) # E: Too many arguments for "g" of "B" |
| 547 |
|
| 548 |
[case testInstantiatingAbstractClassWithMultipleBaseClasses] |
| 549 |
|
| 550 |
from abc import abstractmethod, ABCMeta |
| 551 |
|
| 552 |
class A(metaclass=ABCMeta): |
| 553 |
@abstractmethod |
| 554 |
def f(self) -> None: pass |
| 555 |
class B(metaclass=ABCMeta): |
| 556 |
@abstractmethod |
| 557 |
def g(self) -> None: pass |
| 558 |
class C(A, B): |
| 559 |
def f(self) -> None: pass |
| 560 |
class D(A, B): |
| 561 |
def g(self) -> None: pass |
| 562 |
class E(A, B): |
| 563 |
def f(self) -> None: pass |
| 564 |
def g(self) -> None: pass |
| 565 |
C() # E: Cannot instantiate abstract class "C" with abstract attribute "g" |
| 566 |
D() # E: Cannot instantiate abstract class "D" with abstract attribute "f" |
| 567 |
E() |
| 568 |
|
| 569 |
[case testInconsistentMro] |
| 570 |
from abc import abstractmethod, ABCMeta |
| 571 |
import typing |
| 572 |
|
| 573 |
class A(metaclass=ABCMeta): pass |
| 574 |
class B(object, A, metaclass=ABCMeta): # E: Cannot determine consistent method resolution order (MRO) for "B" |
| 575 |
pass |
| 576 |
|
| 577 |
class C(object, A): # E: Cannot determine consistent method resolution order (MRO) for "C" \ |
| 578 |
# E: Metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases |
| 579 |
pass |
| 580 |
|
| 581 |
[case testOverloadedAbstractMethod] |
| 582 |
from foo import * |
| 583 |
[file foo.pyi] |
| 584 |
from abc import abstractmethod, ABCMeta |
| 585 |
from typing import overload |
| 586 |
|
| 587 |
class A(metaclass=ABCMeta): |
| 588 |
@abstractmethod |
| 589 |
@overload |
| 590 |
def f(self, x: int) -> int: pass |
| 591 |
@abstractmethod |
| 592 |
@overload |
| 593 |
def f(self, x: str) -> str: pass |
| 594 |
|
| 595 |
class B(A): |
| 596 |
@overload |
| 597 |
def f(self, x: int) -> int: pass |
| 598 |
@overload |
| 599 |
def f(self, x: str) -> str: pass |
| 600 |
A() # E: Cannot instantiate abstract class "A" with abstract attribute "f" |
| 601 |
B() |
| 602 |
B().f(1) |
| 603 |
a = B() # type: A |
| 604 |
a.f(1) |
| 605 |
a.f('') |
| 606 |
a.f(B()) # E: No overload variant of "f" of "A" matches argument type "B" \ |
| 607 |
# N: Possible overload variants: \ |
| 608 |
# N: def f(self, x: int) -> int \ |
| 609 |
# N: def f(self, x: str) -> str |
| 610 |
|
| 611 |
[case testOverloadedAbstractMethodWithAlternativeDecoratorOrder] |
| 612 |
from foo import * |
| 613 |
[file foo.pyi] |
| 614 |
from abc import abstractmethod, ABCMeta |
| 615 |
from typing import overload |
| 616 |
|
| 617 |
class A(metaclass=ABCMeta): |
| 618 |
@overload |
| 619 |
@abstractmethod |
| 620 |
def f(self, x: int) -> int: pass |
| 621 |
@overload |
| 622 |
@abstractmethod |
| 623 |
def f(self, x: str) -> str: pass |
| 624 |
|
| 625 |
class B(A): |
| 626 |
@overload |
| 627 |
def f(self, x: int) -> int: pass |
| 628 |
@overload |
| 629 |
def f(self, x: str) -> str: pass |
| 630 |
A() # E: Cannot instantiate abstract class "A" with abstract attribute "f" |
| 631 |
B() |
| 632 |
B().f(1) |
| 633 |
a = B() # type: A |
| 634 |
a.f(1) |
| 635 |
a.f('') |
| 636 |
a.f(B()) # E: No overload variant of "f" of "A" matches argument type "B" \ |
| 637 |
# N: Possible overload variants: \ |
| 638 |
# N: def f(self, x: int) -> int \ |
| 639 |
# N: def f(self, x: str) -> str |
| 640 |
|
| 641 |
[case testOverloadedAbstractMethodVariantMissingDecorator0] |
| 642 |
from foo import * |
| 643 |
[file foo.pyi] |
| 644 |
from abc import abstractmethod, ABCMeta |
| 645 |
from typing import overload |
| 646 |
|
| 647 |
class A(metaclass=ABCMeta): |
| 648 |
@abstractmethod \ |
| 649 |
# E: Overloaded method has both abstract and non-abstract variants |
| 650 |
@overload |
| 651 |
def f(self, x: int) -> int: pass |
| 652 |
@overload |
| 653 |
def f(self, x: str) -> str: pass |
| 654 |
[out] |
| 655 |
|
| 656 |
[case testOverloadedAbstractMethodVariantMissingDecorator1] |
| 657 |
from foo import * |
| 658 |
[file foo.pyi] |
| 659 |
from abc import abstractmethod, ABCMeta |
| 660 |
from typing import overload |
| 661 |
|
| 662 |
class A(metaclass=ABCMeta): |
| 663 |
@overload \ |
| 664 |
# E: Overloaded method has both abstract and non-abstract variants |
| 665 |
def f(self, x: int) -> int: pass |
| 666 |
@abstractmethod |
| 667 |
@overload |
| 668 |
def f(self, x: str) -> str: pass |
| 669 |
[out] |
| 670 |
|
| 671 |
[case testMultipleInheritanceAndAbstractMethod] |
| 672 |
import typing |
| 673 |
from abc import abstractmethod, ABCMeta |
| 674 |
class A: |
| 675 |
def f(self, x: str) -> None: pass |
| 676 |
class B(metaclass=ABCMeta): |
| 677 |
@abstractmethod |
| 678 |
def f(self, x: str) -> None: pass |
| 679 |
class C(A, B): pass |
| 680 |
|
| 681 |
[case testMultipleInheritanceAndAbstractMethod2] |
| 682 |
import typing |
| 683 |
from abc import abstractmethod, ABCMeta |
| 684 |
class A: |
| 685 |
def f(self, x: str) -> None: pass |
| 686 |
class B(metaclass=ABCMeta): |
| 687 |
@abstractmethod |
| 688 |
def f(self, x: int) -> None: pass |
| 689 |
class C(A, B): pass |
| 690 |
[out] |
| 691 |
main:8: error: Definition of "f" in base class "A" is incompatible with definition in base class "B" |
| 692 |
|
| 693 |
[case testCallAbstractMethodBeforeDefinition] |
| 694 |
import typing |
| 695 |
from abc import abstractmethod, ABCMeta |
| 696 |
class A(metaclass=ABCMeta): |
| 697 |
def f(self) -> None: |
| 698 |
self.g(1) # E: Argument 1 to "g" of "A" has incompatible type "int"; expected "str" |
| 699 |
@abstractmethod |
| 700 |
def g(self, x: str) -> None: pass |
| 701 |
[out] |
| 702 |
|
| 703 |
[case testAbstractOperatorMethods1] |
| 704 |
import typing |
| 705 |
from abc import abstractmethod, ABCMeta |
| 706 |
class A(metaclass=ABCMeta): |
| 707 |
@abstractmethod |
| 708 |
def __lt__(self, other: 'A') -> int: pass |
| 709 |
@abstractmethod |
| 710 |
def __gt__(self, other: 'A') -> int: pass |
| 711 |
|
| 712 |
[case testAbstractOperatorMethods2] |
| 713 |
from typing import cast, Any |
| 714 |
from abc import abstractmethod, ABCMeta |
| 715 |
class A(metaclass=ABCMeta): |
| 716 |
@abstractmethod |
| 717 |
def __radd__(self, other: 'C') -> str: pass # Error |
| 718 |
class B: |
| 719 |
@abstractmethod |
| 720 |
def __add__(self, other: 'A') -> int: pass |
| 721 |
class C: |
| 722 |
def __add__(self, other: int) -> B: |
| 723 |
return cast(Any, None) |
| 724 |
[out] |
| 725 |
|
| 726 |
[case testAbstractClassWithAnyBase] |
| 727 |
from typing import Any |
| 728 |
from abc import abstractmethod, ABCMeta |
| 729 |
|
| 730 |
A: Any |
| 731 |
|
| 732 |
class D(metaclass=ABCMeta): |
| 733 |
@abstractmethod |
| 734 |
def f(self) -> None: pass |
| 735 |
|
| 736 |
class C(A, D): |
| 737 |
pass |
| 738 |
|
| 739 |
C() # A might implement 'f' |
| 740 |
|
| 741 |
|
| 742 |
-- Abstract properties |
| 743 |
-- ------------------- |
| 744 |
|
| 745 |
|
| 746 |
[case testReadOnlyAbstractProperty] |
| 747 |
from abc import abstractproperty, ABCMeta |
| 748 |
class A(metaclass=ABCMeta): |
| 749 |
@abstractproperty |
| 750 |
def x(self) -> int: pass |
| 751 |
def f(a: A) -> None: |
| 752 |
a.x() # E: "int" not callable |
| 753 |
a.x = 1 # E: Property "x" defined in "A" is read-only |
| 754 |
[out] |
| 755 |
|
| 756 |
[case testReadOnlyAbstractPropertyForwardRef] |
| 757 |
from abc import abstractproperty, ABCMeta |
| 758 |
def f(a: A) -> None: |
| 759 |
a.x() # E: "int" not callable |
| 760 |
a.x = 1 # E: Property "x" defined in "A" is read-only |
| 761 |
class A(metaclass=ABCMeta): |
| 762 |
@abstractproperty |
| 763 |
def x(self) -> int: pass |
| 764 |
[out] |
| 765 |
|
| 766 |
[case testReadWriteAbstractProperty] |
| 767 |
from abc import abstractproperty, ABCMeta |
| 768 |
def f(a: A) -> None: |
| 769 |
a.x.y # E: "int" has no attribute "y" |
| 770 |
a.x = 1 |
| 771 |
class A(metaclass=ABCMeta): |
| 772 |
@abstractproperty |
| 773 |
def x(self) -> int: pass |
| 774 |
@x.setter |
| 775 |
def x(self, x: int) -> None: pass |
| 776 |
|
| 777 |
[case testReadWriteDeleteAbstractProperty] |
| 778 |
# flags: --no-strict-optional |
| 779 |
from abc import ABC, abstractmethod |
| 780 |
class Abstract(ABC): |
| 781 |
@property |
| 782 |
@abstractmethod |
| 783 |
def prop(self) -> str: ... |
| 784 |
|
| 785 |
@prop.setter |
| 786 |
@abstractmethod |
| 787 |
def prop(self, code: str) -> None: ... |
| 788 |
|
| 789 |
@prop.deleter |
| 790 |
@abstractmethod |
| 791 |
def prop(self) -> None: ... |
| 792 |
|
| 793 |
class Good(Abstract): |
| 794 |
@property |
| 795 |
def prop(self) -> str: ... |
| 796 |
@prop.setter |
| 797 |
def prop(self, code: str) -> None: ... |
| 798 |
@prop.deleter |
| 799 |
def prop(self) -> None: ... |
| 800 |
|
| 801 |
class Bad1(Abstract): |
| 802 |
@property # E: Read-only property cannot override read-write property |
| 803 |
def prop(self) -> str: ... |
| 804 |
|
| 805 |
class ThisShouldProbablyError(Abstract): |
| 806 |
@property |
| 807 |
def prop(self) -> str: ... |
| 808 |
@prop.setter |
| 809 |
def prop(self, code: str) -> None: ... |
| 810 |
|
| 811 |
a = Good() |
| 812 |
reveal_type(a.prop) # N: Revealed type is "builtins.str" |
| 813 |
a.prop = 123 # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 814 |
[builtins fixtures/property.pyi] |
| 815 |
|
| 816 |
[case testInstantiateClassWithReadOnlyAbstractProperty] |
| 817 |
from abc import abstractproperty, ABCMeta |
| 818 |
class A(metaclass=ABCMeta): |
| 819 |
@abstractproperty |
| 820 |
def x(self) -> int: pass |
| 821 |
class B(A): pass |
| 822 |
b = B() # E: Cannot instantiate abstract class "B" with abstract attribute "x" |
| 823 |
|
| 824 |
[case testInstantiateClassWithReadWriteAbstractProperty] |
| 825 |
from abc import abstractproperty, ABCMeta |
| 826 |
class A(metaclass=ABCMeta): |
| 827 |
@abstractproperty |
| 828 |
def x(self) -> int: pass |
| 829 |
@x.setter |
| 830 |
def x(self, x: int) -> None: pass |
| 831 |
class B(A): pass |
| 832 |
b = B() # E: Cannot instantiate abstract class "B" with abstract attribute "x" |
| 833 |
|
| 834 |
[case testImplementAbstractPropertyViaProperty] |
| 835 |
from abc import abstractproperty, ABCMeta |
| 836 |
class A(metaclass=ABCMeta): |
| 837 |
@abstractproperty |
| 838 |
def x(self) -> int: pass |
| 839 |
class B(A): |
| 840 |
@property |
| 841 |
def x(self) -> int: return 0 |
| 842 |
b = B() |
| 843 |
b.x() # E: "int" not callable |
| 844 |
[builtins fixtures/property.pyi] |
| 845 |
|
| 846 |
[case testImplementReadWriteAbstractPropertyViaProperty] |
| 847 |
from abc import abstractproperty, ABCMeta |
| 848 |
class A(metaclass=ABCMeta): |
| 849 |
@abstractproperty |
| 850 |
def x(self) -> int: pass |
| 851 |
@x.setter |
| 852 |
def x(self, v: int) -> None: pass |
| 853 |
class B(A): |
| 854 |
@property |
| 855 |
def x(self) -> int: return 0 |
| 856 |
@x.setter |
| 857 |
def x(self, v: int) -> None: pass |
| 858 |
b = B() |
| 859 |
b.x.y # E: "int" has no attribute "y" |
| 860 |
[builtins fixtures/property.pyi] |
| 861 |
|
| 862 |
[case testImplementAbstractPropertyViaPropertyInvalidType] |
| 863 |
from abc import abstractproperty, ABCMeta |
| 864 |
class A(metaclass=ABCMeta): |
| 865 |
@abstractproperty |
| 866 |
def x(self) -> int: pass |
| 867 |
class B(A): |
| 868 |
@property |
| 869 |
def x(self) -> str: return "no" # E: Signature of "x" incompatible with supertype "A" \ |
| 870 |
# N: Superclass: \ |
| 871 |
# N: int \ |
| 872 |
# N: Subclass: \ |
| 873 |
# N: str |
| 874 |
b = B() |
| 875 |
b.x() # E: "str" not callable |
| 876 |
[builtins fixtures/property.pyi] |
| 877 |
|
| 878 |
[case testCantImplementAbstractPropertyViaInstanceVariable] |
| 879 |
from abc import abstractproperty, ABCMeta |
| 880 |
class A(metaclass=ABCMeta): |
| 881 |
@abstractproperty |
| 882 |
def x(self) -> int: pass |
| 883 |
class B(A): |
| 884 |
def __init__(self) -> None: |
| 885 |
self.x = 1 # E |
| 886 |
b = B() # E |
| 887 |
b.x.y # E |
| 888 |
[builtins fixtures/property.pyi] |
| 889 |
[out] |
| 890 |
main:7: error: Property "x" defined in "A" is read-only |
| 891 |
main:8: error: Cannot instantiate abstract class "B" with abstract attribute "x" |
| 892 |
main:9: error: "int" has no attribute "y" |
| 893 |
|
| 894 |
[case testSuperWithAbstractProperty] |
| 895 |
# flags: --no-strict-optional |
| 896 |
from abc import abstractproperty, ABCMeta |
| 897 |
class A(metaclass=ABCMeta): |
| 898 |
@abstractproperty |
| 899 |
def x(self) -> int: pass |
| 900 |
class B(A): |
| 901 |
@property |
| 902 |
def x(self) -> int: |
| 903 |
return super().x.y # E: Call to abstract method "x" of "A" with trivial body via super() is unsafe \ |
| 904 |
# E: "int" has no attribute "y" |
| 905 |
[builtins fixtures/property.pyi] |
| 906 |
|
| 907 |
[case testSuperWithReadWriteAbstractProperty] |
| 908 |
from abc import abstractproperty, ABCMeta |
| 909 |
class A(metaclass=ABCMeta): |
| 910 |
@abstractproperty |
| 911 |
def x(self) -> int: pass |
| 912 |
@x.setter |
| 913 |
def x(self, v: int) -> None: pass |
| 914 |
class B(A): |
| 915 |
@property |
| 916 |
def x(self) -> int: |
| 917 |
return super().x.y # E |
| 918 |
@x.setter |
| 919 |
def x(self, v: int) -> None: |
| 920 |
super().x = '' # E |
| 921 |
[builtins fixtures/property.pyi] |
| 922 |
[out] |
| 923 |
main:10: error: "int" has no attribute "y" |
| 924 |
main:13: error: Invalid assignment target |
| 925 |
|
| 926 |
[case testOnlyImplementGetterOfReadWriteAbstractProperty] |
| 927 |
from abc import abstractproperty, ABCMeta |
| 928 |
class A(metaclass=ABCMeta): |
| 929 |
@abstractproperty |
| 930 |
def x(self) -> int: pass |
| 931 |
@x.setter |
| 932 |
def x(self, v: int) -> None: pass |
| 933 |
class B(A): |
| 934 |
@property # E |
| 935 |
def x(self) -> int: return 0 |
| 936 |
b = B() |
| 937 |
b.x.y # E |
| 938 |
[builtins fixtures/property.pyi] |
| 939 |
[out] |
| 940 |
main:8: error: Read-only property cannot override read-write property |
| 941 |
main:11: error: "int" has no attribute "y" |
| 942 |
|
| 943 |
[case testDynamicallyTypedReadOnlyAbstractProperty] |
| 944 |
from abc import abstractproperty, ABCMeta |
| 945 |
class A(metaclass=ABCMeta): |
| 946 |
@abstractproperty |
| 947 |
def x(self): pass |
| 948 |
def f(a: A) -> None: |
| 949 |
a.x.y |
| 950 |
a.x = 1 # E: Property "x" defined in "A" is read-only |
| 951 |
[out] |
| 952 |
|
| 953 |
[case testDynamicallyTypedReadOnlyAbstractPropertyForwardRef] |
| 954 |
from abc import abstractproperty, ABCMeta |
| 955 |
def f(a: A) -> None: |
| 956 |
a.x.y |
| 957 |
a.x = 1 # E: Property "x" defined in "A" is read-only |
| 958 |
class A(metaclass=ABCMeta): |
| 959 |
@abstractproperty |
| 960 |
def x(self): pass |
| 961 |
[out] |
| 962 |
|
| 963 |
[case testDynamicallyTypedReadWriteAbstractProperty] |
| 964 |
from abc import abstractproperty, ABCMeta |
| 965 |
def f(a: A) -> None: |
| 966 |
a.x.y |
| 967 |
a.x = 1 |
| 968 |
class A(metaclass=ABCMeta): |
| 969 |
@abstractproperty |
| 970 |
def x(self): pass |
| 971 |
@x.setter |
| 972 |
def x(self, x): pass |
| 973 |
[out] |
| 974 |
|
| 975 |
[case testMixinTypedAbstractProperty] |
| 976 |
from abc import ABCMeta, abstractproperty |
| 977 |
class A(metaclass=ABCMeta): |
| 978 |
@abstractproperty |
| 979 |
def foo(cls) -> str: |
| 980 |
pass |
| 981 |
class Mixin: |
| 982 |
foo = "foo" |
| 983 |
class C(Mixin, A): |
| 984 |
pass |
| 985 |
[out] |
| 986 |
|
| 987 |
[case testMixinTypedProperty] |
| 988 |
class A: |
| 989 |
@property |
| 990 |
def foo(cls) -> str: |
| 991 |
return "yes" |
| 992 |
class Mixin: |
| 993 |
foo = "foo" |
| 994 |
class C(Mixin, A): |
| 995 |
pass |
| 996 |
[builtins fixtures/property.pyi] |
| 997 |
|
| 998 |
[case testMixinSubtypedProperty] |
| 999 |
class X: |
| 1000 |
pass |
| 1001 |
class Y(X): |
| 1002 |
pass |
| 1003 |
class A: |
| 1004 |
@property |
| 1005 |
def foo(cls) -> X: |
| 1006 |
return X() |
| 1007 |
class Mixin: |
| 1008 |
foo = Y() |
| 1009 |
class C(Mixin, A): |
| 1010 |
pass |
| 1011 |
[builtins fixtures/property.pyi] |
| 1012 |
|
| 1013 |
[case testMixinTypedPropertyReversed] |
| 1014 |
class A: |
| 1015 |
@property |
| 1016 |
def foo(cls) -> str: |
| 1017 |
return "no" |
| 1018 |
class Mixin: |
| 1019 |
foo = "foo" |
| 1020 |
class C(A, Mixin): # E: Cannot override writeable attribute "foo" in base "Mixin" with read-only property in base "A" |
| 1021 |
pass |
| 1022 |
[builtins fixtures/property.pyi] |
| 1023 |
|
| 1024 |
-- Special cases |
| 1025 |
-- ------------- |
| 1026 |
|
| 1027 |
|
| 1028 |
[case testNestedAbstractClass] |
| 1029 |
from abc import abstractmethod, ABCMeta |
| 1030 |
|
| 1031 |
class A: |
| 1032 |
class B(metaclass=ABCMeta): |
| 1033 |
@abstractmethod |
| 1034 |
def f(self) -> None: pass |
| 1035 |
|
| 1036 |
class C(B): pass |
| 1037 |
|
| 1038 |
A.B() # E: Cannot instantiate abstract class "B" with abstract attribute "f" |
| 1039 |
A.C() # E: Cannot instantiate abstract class "C" with abstract attribute "f" |
| 1040 |
|
| 1041 |
[case testAbstractNewTypeAllowed] |
| 1042 |
from typing import NewType, Mapping |
| 1043 |
|
| 1044 |
Config = NewType('Config', Mapping[str, str]) |
| 1045 |
|
| 1046 |
bad = Mapping[str, str]() # E: Cannot instantiate abstract class "Mapping" with abstract attribute "__iter__" |
| 1047 |
default = Config({'cannot': 'modify'}) # OK |
| 1048 |
|
| 1049 |
default[1] = 2 # E: Unsupported target for indexed assignment ("Config") |
| 1050 |
[builtins fixtures/dict.pyi] |
| 1051 |
[typing fixtures/typing-full.pyi] |
| 1052 |
|
| 1053 |
[case testSubclassOfABCFromDictionary] |
| 1054 |
from abc import abstractmethod, ABCMeta |
| 1055 |
|
| 1056 |
class MyAbstractType(metaclass=ABCMeta): |
| 1057 |
@abstractmethod |
| 1058 |
def do(self): pass |
| 1059 |
|
| 1060 |
class MyConcreteA(MyAbstractType): |
| 1061 |
def do(self): |
| 1062 |
print('A') |
| 1063 |
|
| 1064 |
class MyConcreteB(MyAbstractType): |
| 1065 |
def do(self): |
| 1066 |
print('B') |
| 1067 |
|
| 1068 |
class MyAbstractA(MyAbstractType): |
| 1069 |
@abstractmethod |
| 1070 |
def do(self): pass |
| 1071 |
|
| 1072 |
class MyAbstractB(MyAbstractType): |
| 1073 |
@abstractmethod |
| 1074 |
def do(self): pass |
| 1075 |
|
| 1076 |
my_concrete_types = { |
| 1077 |
'A': MyConcreteA, |
| 1078 |
'B': MyConcreteB, |
| 1079 |
} |
| 1080 |
|
| 1081 |
my_abstract_types = { |
| 1082 |
'A': MyAbstractA, |
| 1083 |
'B': MyAbstractB, |
| 1084 |
} |
| 1085 |
|
| 1086 |
reveal_type(my_concrete_types) # N: Revealed type is "builtins.dict[builtins.str, def () -> __main__.MyAbstractType]" |
| 1087 |
reveal_type(my_abstract_types) # N: Revealed type is "builtins.dict[builtins.str, def () -> __main__.MyAbstractType]" |
| 1088 |
|
| 1089 |
a = my_concrete_types['A']() |
| 1090 |
a.do() |
| 1091 |
b = my_concrete_types['B']() |
| 1092 |
b.do() |
| 1093 |
|
| 1094 |
c = my_abstract_types['A']() # E: Cannot instantiate abstract class "MyAbstractType" with abstract attribute "do" |
| 1095 |
c.do() |
| 1096 |
d = my_abstract_types['B']() # E: Cannot instantiate abstract class "MyAbstractType" with abstract attribute "do" |
| 1097 |
d.do() |
| 1098 |
|
| 1099 |
[builtins fixtures/dict.pyi] |
| 1100 |
|
| 1101 |
[case testAbstractClassesWorkWithGenericDecorators] |
| 1102 |
from abc import abstractmethod, ABCMeta |
| 1103 |
from typing import Type, TypeVar |
| 1104 |
|
| 1105 |
T = TypeVar("T") |
| 1106 |
def deco(cls: Type[T]) -> Type[T]: return cls |
| 1107 |
|
| 1108 |
@deco |
| 1109 |
class A(metaclass=ABCMeta): |
| 1110 |
@abstractmethod |
| 1111 |
def foo(self, x: int) -> None: ... |
| 1112 |
|
| 1113 |
[case testAbstractPropertiesAllowed] |
| 1114 |
from abc import abstractmethod |
| 1115 |
|
| 1116 |
class B: |
| 1117 |
@property |
| 1118 |
@abstractmethod |
| 1119 |
def x(self) -> int: ... |
| 1120 |
@property |
| 1121 |
@abstractmethod |
| 1122 |
def y(self) -> int: ... |
| 1123 |
@y.setter |
| 1124 |
@abstractmethod |
| 1125 |
def y(self, value: int) -> None: ... |
| 1126 |
|
| 1127 |
B() # E: Cannot instantiate abstract class "B" with abstract attributes "x" and "y" |
| 1128 |
b: B |
| 1129 |
b.x = 1 # E: Property "x" defined in "B" is read-only |
| 1130 |
b.y = 1 |
| 1131 |
[builtins fixtures/property.pyi] |
| 1132 |
|
| 1133 |
|
| 1134 |
-- Treatment of empty bodies in ABCs and protocols |
| 1135 |
-- ----------------------------------------------- |
| 1136 |
|
| 1137 |
[case testEmptyBodyProhibitedFunction] |
| 1138 |
from typing import overload, Union |
| 1139 |
|
| 1140 |
def func1(x: str) -> int: pass # E: Missing return statement |
| 1141 |
def func2(x: str) -> int: ... # E: Missing return statement |
| 1142 |
def func3(x: str) -> int: # E: Missing return statement |
| 1143 |
"""Some function.""" |
| 1144 |
|
| 1145 |
@overload |
| 1146 |
def func4(x: int) -> int: ... |
| 1147 |
@overload |
| 1148 |
def func4(x: str) -> str: ... |
| 1149 |
def func4(x: Union[int, str]) -> Union[int, str]: # E: Missing return statement |
| 1150 |
pass |
| 1151 |
|
| 1152 |
@overload |
| 1153 |
def func5(x: int) -> int: ... |
| 1154 |
@overload |
| 1155 |
def func5(x: str) -> str: ... |
| 1156 |
def func5(x: Union[int, str]) -> Union[int, str]: # E: Missing return statement |
| 1157 |
"""Some function.""" |
| 1158 |
|
| 1159 |
[case testEmptyBodyProhibitedMethodNonAbstract] |
| 1160 |
from typing import overload, Union |
| 1161 |
|
| 1162 |
class A: |
| 1163 |
def func1(self, x: str) -> int: pass # E: Missing return statement |
| 1164 |
def func2(self, x: str) -> int: ... # E: Missing return statement |
| 1165 |
def func3(self, x: str) -> int: # E: Missing return statement |
| 1166 |
"""Some function.""" |
| 1167 |
|
| 1168 |
class B: |
| 1169 |
@classmethod |
| 1170 |
def func1(cls, x: str) -> int: pass # E: Missing return statement |
| 1171 |
@classmethod |
| 1172 |
def func2(cls, x: str) -> int: ... # E: Missing return statement |
| 1173 |
@classmethod |
| 1174 |
def func3(cls, x: str) -> int: # E: Missing return statement |
| 1175 |
"""Some function.""" |
| 1176 |
|
| 1177 |
class C: |
| 1178 |
@overload |
| 1179 |
def func4(self, x: int) -> int: ... |
| 1180 |
@overload |
| 1181 |
def func4(self, x: str) -> str: ... |
| 1182 |
def func4(self, x: Union[int, str]) -> Union[int, str]: # E: Missing return statement |
| 1183 |
pass |
| 1184 |
|
| 1185 |
@overload |
| 1186 |
def func5(self, x: int) -> int: ... |
| 1187 |
@overload |
| 1188 |
def func5(self, x: str) -> str: ... |
| 1189 |
def func5(self, x: Union[int, str]) -> Union[int, str]: # E: Missing return statement |
| 1190 |
"""Some function.""" |
| 1191 |
[builtins fixtures/classmethod.pyi] |
| 1192 |
|
| 1193 |
[case testEmptyBodyProhibitedPropertyNonAbstract] |
| 1194 |
class A: |
| 1195 |
@property |
| 1196 |
def x(self) -> int: ... # E: Missing return statement |
| 1197 |
@property |
| 1198 |
def y(self) -> int: ... # E: Missing return statement |
| 1199 |
@y.setter |
| 1200 |
def y(self, value: int) -> None: ... |
| 1201 |
|
| 1202 |
class B: |
| 1203 |
@property |
| 1204 |
def x(self) -> int: pass # E: Missing return statement |
| 1205 |
@property |
| 1206 |
def y(self) -> int: pass # E: Missing return statement |
| 1207 |
@y.setter |
| 1208 |
def y(self, value: int) -> None: pass |
| 1209 |
|
| 1210 |
class C: |
| 1211 |
@property |
| 1212 |
def x(self) -> int: # E: Missing return statement |
| 1213 |
"""Some property.""" |
| 1214 |
@property |
| 1215 |
def y(self) -> int: # E: Missing return statement |
| 1216 |
"""Some property.""" |
| 1217 |
@y.setter |
| 1218 |
def y(self, value: int) -> None: pass |
| 1219 |
[builtins fixtures/property.pyi] |
| 1220 |
|
| 1221 |
[case testEmptyBodyNoteABCMeta] |
| 1222 |
from abc import ABC |
| 1223 |
|
| 1224 |
class A(ABC): |
| 1225 |
def foo(self) -> int: # E: Missing return statement \ |
| 1226 |
# N: If the method is meant to be abstract, use @abc.abstractmethod |
| 1227 |
... |
| 1228 |
|
| 1229 |
[case testEmptyBodyAllowedFunctionStub] |
| 1230 |
import stub |
| 1231 |
[file stub.pyi] |
| 1232 |
from typing import overload, Union |
| 1233 |
|
| 1234 |
def func1(x: str) -> int: pass |
| 1235 |
def func2(x: str) -> int: ... |
| 1236 |
def func3(x: str) -> int: |
| 1237 |
"""Some function.""" |
| 1238 |
|
| 1239 |
[case testEmptyBodyAllowedMethodNonAbstractStub] |
| 1240 |
import stub |
| 1241 |
[file stub.pyi] |
| 1242 |
from typing import overload, Union |
| 1243 |
|
| 1244 |
class A: |
| 1245 |
def func1(self, x: str) -> int: pass |
| 1246 |
def func2(self, x: str) -> int: ... |
| 1247 |
def func3(self, x: str) -> int: |
| 1248 |
"""Some function.""" |
| 1249 |
|
| 1250 |
class B: |
| 1251 |
@classmethod |
| 1252 |
def func1(cls, x: str) -> int: pass |
| 1253 |
@classmethod |
| 1254 |
def func2(cls, x: str) -> int: ... |
| 1255 |
@classmethod |
| 1256 |
def func3(cls, x: str) -> int: |
| 1257 |
"""Some function.""" |
| 1258 |
[builtins fixtures/classmethod.pyi] |
| 1259 |
|
| 1260 |
[case testEmptyBodyAllowedPropertyNonAbstractStub] |
| 1261 |
import stub |
| 1262 |
[file stub.pyi] |
| 1263 |
class A: |
| 1264 |
@property |
| 1265 |
def x(self) -> int: ... |
| 1266 |
@property |
| 1267 |
def y(self) -> int: ... |
| 1268 |
@y.setter |
| 1269 |
def y(self, value: int) -> None: ... |
| 1270 |
|
| 1271 |
class B: |
| 1272 |
@property |
| 1273 |
def x(self) -> int: pass |
| 1274 |
@property |
| 1275 |
def y(self) -> int: pass |
| 1276 |
@y.setter |
| 1277 |
def y(self, value: int) -> None: pass |
| 1278 |
|
| 1279 |
class C: |
| 1280 |
@property |
| 1281 |
def x(self) -> int: |
| 1282 |
"""Some property.""" |
| 1283 |
@property |
| 1284 |
def y(self) -> int: |
| 1285 |
"""Some property.""" |
| 1286 |
@y.setter |
| 1287 |
def y(self, value: int) -> None: pass |
| 1288 |
[builtins fixtures/property.pyi] |
| 1289 |
|
| 1290 |
[case testEmptyBodyAllowedMethodAbstract] |
| 1291 |
from typing import overload, Union |
| 1292 |
from abc import abstractmethod |
| 1293 |
|
| 1294 |
class A: |
| 1295 |
@abstractmethod |
| 1296 |
def func1(self, x: str) -> int: pass |
| 1297 |
@abstractmethod |
| 1298 |
def func2(self, x: str) -> int: ... |
| 1299 |
@abstractmethod |
| 1300 |
def func3(self, x: str) -> int: |
| 1301 |
"""Some function.""" |
| 1302 |
|
| 1303 |
class B: |
| 1304 |
@classmethod |
| 1305 |
@abstractmethod |
| 1306 |
def func1(cls, x: str) -> int: pass |
| 1307 |
@classmethod |
| 1308 |
@abstractmethod |
| 1309 |
def func2(cls, x: str) -> int: ... |
| 1310 |
@classmethod |
| 1311 |
@abstractmethod |
| 1312 |
def func3(cls, x: str) -> int: |
| 1313 |
"""Some function.""" |
| 1314 |
|
| 1315 |
class C: |
| 1316 |
@overload |
| 1317 |
@abstractmethod |
| 1318 |
def func4(self, x: int) -> int: ... |
| 1319 |
@overload |
| 1320 |
@abstractmethod |
| 1321 |
def func4(self, x: str) -> str: ... |
| 1322 |
@abstractmethod |
| 1323 |
def func4(self, x: Union[int, str]) -> Union[int, str]: |
| 1324 |
pass |
| 1325 |
|
| 1326 |
@overload |
| 1327 |
@abstractmethod |
| 1328 |
def func5(self, x: int) -> int: ... |
| 1329 |
@overload |
| 1330 |
@abstractmethod |
| 1331 |
def func5(self, x: str) -> str: ... |
| 1332 |
@abstractmethod |
| 1333 |
def func5(self, x: Union[int, str]) -> Union[int, str]: |
| 1334 |
"""Some function.""" |
| 1335 |
[builtins fixtures/classmethod.pyi] |
| 1336 |
|
| 1337 |
[case testEmptyBodyAllowedPropertyAbstract] |
| 1338 |
from abc import abstractmethod |
| 1339 |
class A: |
| 1340 |
@property |
| 1341 |
@abstractmethod |
| 1342 |
def x(self) -> int: ... |
| 1343 |
@property |
| 1344 |
@abstractmethod |
| 1345 |
def y(self) -> int: ... |
| 1346 |
@y.setter |
| 1347 |
@abstractmethod |
| 1348 |
def y(self, value: int) -> None: ... |
| 1349 |
|
| 1350 |
class B: |
| 1351 |
@property |
| 1352 |
@abstractmethod |
| 1353 |
def x(self) -> int: pass |
| 1354 |
@property |
| 1355 |
@abstractmethod |
| 1356 |
def y(self) -> int: pass |
| 1357 |
@y.setter |
| 1358 |
@abstractmethod |
| 1359 |
def y(self, value: int) -> None: pass |
| 1360 |
|
| 1361 |
class C: |
| 1362 |
@property |
| 1363 |
@abstractmethod |
| 1364 |
def x(self) -> int: |
| 1365 |
"""Some property.""" |
| 1366 |
@property |
| 1367 |
@abstractmethod |
| 1368 |
def y(self) -> int: |
| 1369 |
"""Some property.""" |
| 1370 |
@y.setter |
| 1371 |
@abstractmethod |
| 1372 |
def y(self, value: int) -> None: pass |
| 1373 |
[builtins fixtures/property.pyi] |
| 1374 |
|
| 1375 |
[case testEmptyBodyImplicitlyAbstractProtocol] |
| 1376 |
from typing import Protocol, overload, Union |
| 1377 |
|
| 1378 |
class P1(Protocol): |
| 1379 |
def meth(self) -> int: ... |
| 1380 |
class B1(P1): ... |
| 1381 |
class C1(P1): |
| 1382 |
def meth(self) -> int: |
| 1383 |
return 0 |
| 1384 |
B1() # E: Cannot instantiate abstract class "B1" with abstract attribute "meth" |
| 1385 |
C1() |
| 1386 |
|
| 1387 |
class P2(Protocol): |
| 1388 |
@classmethod |
| 1389 |
def meth(cls) -> int: ... |
| 1390 |
class B2(P2): ... |
| 1391 |
class C2(P2): |
| 1392 |
@classmethod |
| 1393 |
def meth(cls) -> int: |
| 1394 |
return 0 |
| 1395 |
B2() # E: Cannot instantiate abstract class "B2" with abstract attribute "meth" |
| 1396 |
C2() |
| 1397 |
|
| 1398 |
class P3(Protocol): |
| 1399 |
@overload |
| 1400 |
def meth(self, x: int) -> int: ... |
| 1401 |
@overload |
| 1402 |
def meth(self, x: str) -> str: ... |
| 1403 |
class B3(P3): ... |
| 1404 |
class C3(P3): |
| 1405 |
@overload |
| 1406 |
def meth(self, x: int) -> int: ... |
| 1407 |
@overload |
| 1408 |
def meth(self, x: str) -> str: ... |
| 1409 |
def meth(self, x: Union[int, str]) -> Union[int, str]: |
| 1410 |
return 0 |
| 1411 |
B3() # E: Cannot instantiate abstract class "B3" with abstract attribute "meth" |
| 1412 |
C3() |
| 1413 |
[builtins fixtures/classmethod.pyi] |
| 1414 |
|
| 1415 |
[case testEmptyBodyImplicitlyAbstractProtocolProperty] |
| 1416 |
from typing import Protocol |
| 1417 |
|
| 1418 |
class P1(Protocol): |
| 1419 |
@property |
| 1420 |
def attr(self) -> int: ... |
| 1421 |
class B1(P1): ... |
| 1422 |
class C1(P1): |
| 1423 |
@property |
| 1424 |
def attr(self) -> int: |
| 1425 |
return 0 |
| 1426 |
B1() # E: Cannot instantiate abstract class "B1" with abstract attribute "attr" |
| 1427 |
C1() |
| 1428 |
|
| 1429 |
class P2(Protocol): |
| 1430 |
@property |
| 1431 |
def attr(self) -> int: ... |
| 1432 |
@attr.setter |
| 1433 |
def attr(self, value: int) -> None: ... |
| 1434 |
class B2(P2): ... |
| 1435 |
class C2(P2): |
| 1436 |
@property |
| 1437 |
def attr(self) -> int: return 0 |
| 1438 |
@attr.setter |
| 1439 |
def attr(self, value: int) -> None: pass |
| 1440 |
B2() # E: Cannot instantiate abstract class "B2" with abstract attribute "attr" |
| 1441 |
C2() |
| 1442 |
[builtins fixtures/property.pyi] |
| 1443 |
|
| 1444 |
[case testEmptyBodyImplicitlyAbstractProtocolStub] |
| 1445 |
from stub import P1, P2, P3, P4 |
| 1446 |
|
| 1447 |
class B1(P1): ... |
| 1448 |
class B2(P2): ... |
| 1449 |
class B3(P3): ... |
| 1450 |
class B4(P4): ... |
| 1451 |
|
| 1452 |
B1() |
| 1453 |
B2() |
| 1454 |
B3() |
| 1455 |
B4() # E: Cannot instantiate abstract class "B4" with abstract attribute "meth" |
| 1456 |
|
| 1457 |
[file stub.pyi] |
| 1458 |
from typing import Protocol, overload, Union |
| 1459 |
from abc import abstractmethod |
| 1460 |
|
| 1461 |
class P1(Protocol): |
| 1462 |
def meth(self) -> int: ... |
| 1463 |
|
| 1464 |
class P2(Protocol): |
| 1465 |
@classmethod |
| 1466 |
def meth(cls) -> int: ... |
| 1467 |
|
| 1468 |
class P3(Protocol): |
| 1469 |
@overload |
| 1470 |
def meth(self, x: int) -> int: ... |
| 1471 |
@overload |
| 1472 |
def meth(self, x: str) -> str: ... |
| 1473 |
|
| 1474 |
class P4(Protocol): |
| 1475 |
@abstractmethod |
| 1476 |
def meth(self) -> int: ... |
| 1477 |
[builtins fixtures/classmethod.pyi] |
| 1478 |
|
| 1479 |
[case testEmptyBodyUnsafeAbstractSuper] |
| 1480 |
from stub import StubProto, StubAbstract |
| 1481 |
from typing import Protocol |
| 1482 |
from abc import abstractmethod |
| 1483 |
|
| 1484 |
class Proto(Protocol): |
| 1485 |
def meth(self) -> int: ... |
| 1486 |
class ProtoDef(Protocol): |
| 1487 |
def meth(self) -> int: return 0 |
| 1488 |
|
| 1489 |
class Abstract: |
| 1490 |
@abstractmethod |
| 1491 |
def meth(self) -> int: ... |
| 1492 |
class AbstractDef: |
| 1493 |
@abstractmethod |
| 1494 |
def meth(self) -> int: return 0 |
| 1495 |
|
| 1496 |
class SubProto(Proto): |
| 1497 |
def meth(self) -> int: |
| 1498 |
return super().meth() # E: Call to abstract method "meth" of "Proto" with trivial body via super() is unsafe |
| 1499 |
class SubProtoDef(ProtoDef): |
| 1500 |
def meth(self) -> int: |
| 1501 |
return super().meth() |
| 1502 |
|
| 1503 |
class SubAbstract(Abstract): |
| 1504 |
def meth(self) -> int: |
| 1505 |
return super().meth() # E: Call to abstract method "meth" of "Abstract" with trivial body via super() is unsafe |
| 1506 |
class SubAbstractDef(AbstractDef): |
| 1507 |
def meth(self) -> int: |
| 1508 |
return super().meth() |
| 1509 |
|
| 1510 |
class SubStubProto(StubProto): |
| 1511 |
def meth(self) -> int: |
| 1512 |
return super().meth() |
| 1513 |
class SubStubAbstract(StubAbstract): |
| 1514 |
def meth(self) -> int: |
| 1515 |
return super().meth() |
| 1516 |
|
| 1517 |
[file stub.pyi] |
| 1518 |
from typing import Protocol |
| 1519 |
from abc import abstractmethod |
| 1520 |
|
| 1521 |
class StubProto(Protocol): |
| 1522 |
def meth(self) -> int: ... |
| 1523 |
class StubAbstract: |
| 1524 |
@abstractmethod |
| 1525 |
def meth(self) -> int: ... |
| 1526 |
|
| 1527 |
[case testEmptyBodyUnsafeAbstractSuperProperty] |
| 1528 |
from stub import StubProto, StubAbstract |
| 1529 |
from typing import Protocol |
| 1530 |
from abc import abstractmethod |
| 1531 |
|
| 1532 |
class Proto(Protocol): |
| 1533 |
@property |
| 1534 |
def attr(self) -> int: ... |
| 1535 |
class SubProto(Proto): |
| 1536 |
@property |
| 1537 |
def attr(self) -> int: return super().attr # E: Call to abstract method "attr" of "Proto" with trivial body via super() is unsafe |
| 1538 |
|
| 1539 |
class ProtoDef(Protocol): |
| 1540 |
@property |
| 1541 |
def attr(self) -> int: return 0 |
| 1542 |
class SubProtoDef(ProtoDef): |
| 1543 |
@property |
| 1544 |
def attr(self) -> int: return super().attr |
| 1545 |
|
| 1546 |
class Abstract: |
| 1547 |
@property |
| 1548 |
@abstractmethod |
| 1549 |
def attr(self) -> int: ... |
| 1550 |
class SubAbstract(Abstract): |
| 1551 |
@property |
| 1552 |
@abstractmethod |
| 1553 |
def attr(self) -> int: return super().attr # E: Call to abstract method "attr" of "Abstract" with trivial body via super() is unsafe |
| 1554 |
|
| 1555 |
class AbstractDef: |
| 1556 |
@property |
| 1557 |
@abstractmethod |
| 1558 |
def attr(self) -> int: return 0 |
| 1559 |
class SubAbstractDef(AbstractDef): |
| 1560 |
@property |
| 1561 |
@abstractmethod |
| 1562 |
def attr(self) -> int: return super().attr |
| 1563 |
|
| 1564 |
class SubStubProto(StubProto): |
| 1565 |
@property |
| 1566 |
def attr(self) -> int: return super().attr |
| 1567 |
class SubStubAbstract(StubAbstract): |
| 1568 |
@property |
| 1569 |
def attr(self) -> int: return super().attr |
| 1570 |
|
| 1571 |
[file stub.pyi] |
| 1572 |
from typing import Protocol |
| 1573 |
from abc import abstractmethod |
| 1574 |
|
| 1575 |
class StubProto(Protocol): |
| 1576 |
@property |
| 1577 |
def attr(self) -> int: ... |
| 1578 |
class StubAbstract: |
| 1579 |
@property |
| 1580 |
@abstractmethod |
| 1581 |
def attr(self) -> int: ... |
| 1582 |
[builtins fixtures/property.pyi] |
| 1583 |
|
| 1584 |
[case testEmptyBodyUnsafeAbstractSuperOverloads] |
| 1585 |
from stub import StubProto |
| 1586 |
from typing import Protocol, overload, Union |
| 1587 |
|
| 1588 |
class ProtoEmptyImpl(Protocol): |
| 1589 |
@overload |
| 1590 |
def meth(self, x: str) -> str: ... |
| 1591 |
@overload |
| 1592 |
def meth(self, x: int) -> int: ... |
| 1593 |
def meth(self, x: Union[int, str]) -> Union[int, str]: |
| 1594 |
raise NotImplementedError |
| 1595 |
class ProtoDefImpl(Protocol): |
| 1596 |
@overload |
| 1597 |
def meth(self, x: str) -> str: ... |
| 1598 |
@overload |
| 1599 |
def meth(self, x: int) -> int: ... |
| 1600 |
def meth(self, x: Union[int, str]) -> Union[int, str]: |
| 1601 |
return 0 |
| 1602 |
class ProtoNoImpl(Protocol): |
| 1603 |
@overload |
| 1604 |
def meth(self, x: str) -> str: ... |
| 1605 |
@overload |
| 1606 |
def meth(self, x: int) -> int: ... |
| 1607 |
|
| 1608 |
class SubProtoEmptyImpl(ProtoEmptyImpl): |
| 1609 |
@overload |
| 1610 |
def meth(self, x: str) -> str: ... |
| 1611 |
@overload |
| 1612 |
def meth(self, x: int) -> int: ... |
| 1613 |
def meth(self, x: Union[int, str]) -> Union[int, str]: |
| 1614 |
return super().meth(0) # E: Call to abstract method "meth" of "ProtoEmptyImpl" with trivial body via super() is unsafe |
| 1615 |
class SubProtoDefImpl(ProtoDefImpl): |
| 1616 |
@overload |
| 1617 |
def meth(self, x: str) -> str: ... |
| 1618 |
@overload |
| 1619 |
def meth(self, x: int) -> int: ... |
| 1620 |
def meth(self, x: Union[int, str]) -> Union[int, str]: |
| 1621 |
return super().meth(0) |
| 1622 |
class SubStubProto(StubProto): |
| 1623 |
@overload |
| 1624 |
def meth(self, x: str) -> str: ... |
| 1625 |
@overload |
| 1626 |
def meth(self, x: int) -> int: ... |
| 1627 |
def meth(self, x: Union[int, str]) -> Union[int, str]: |
| 1628 |
return super().meth(0) |
| 1629 |
|
| 1630 |
# TODO: it would be good to also give an error in this case. |
| 1631 |
class SubProtoNoImpl(ProtoNoImpl): |
| 1632 |
@overload |
| 1633 |
def meth(self, x: str) -> str: ... |
| 1634 |
@overload |
| 1635 |
def meth(self, x: int) -> int: ... |
| 1636 |
def meth(self, x: Union[int, str]) -> Union[int, str]: |
| 1637 |
return super().meth(0) |
| 1638 |
|
| 1639 |
[file stub.pyi] |
| 1640 |
from typing import Protocol, overload |
| 1641 |
|
| 1642 |
class StubProto(Protocol): |
| 1643 |
@overload |
| 1644 |
def meth(self, x: str) -> str: ... |
| 1645 |
@overload |
| 1646 |
def meth(self, x: int) -> int: ... |
| 1647 |
|
| 1648 |
[builtins fixtures/exception.pyi] |
| 1649 |
|
| 1650 |
[case testEmptyBodyNoSuperWarningWithoutStrict] |
| 1651 |
# flags: --no-strict-optional |
| 1652 |
from typing import Protocol |
| 1653 |
from abc import abstractmethod |
| 1654 |
|
| 1655 |
class Proto(Protocol): |
| 1656 |
def meth(self) -> int: ... |
| 1657 |
class Abstract: |
| 1658 |
@abstractmethod |
| 1659 |
def meth(self) -> int: ... |
| 1660 |
|
| 1661 |
class SubProto(Proto): |
| 1662 |
def meth(self) -> int: |
| 1663 |
return super().meth() # E: Call to abstract method "meth" of "Proto" with trivial body via super() is unsafe |
| 1664 |
class SubAbstract(Abstract): |
| 1665 |
def meth(self) -> int: |
| 1666 |
return super().meth() # E: Call to abstract method "meth" of "Abstract" with trivial body via super() is unsafe |
| 1667 |
|
| 1668 |
[case testEmptyBodyNoSuperWarningOptionalReturn] |
| 1669 |
from typing import Protocol, Optional |
| 1670 |
from abc import abstractmethod |
| 1671 |
|
| 1672 |
class Proto(Protocol): |
| 1673 |
def meth(self) -> Optional[int]: pass |
| 1674 |
class Abstract: |
| 1675 |
@abstractmethod |
| 1676 |
def meth(self) -> Optional[int]: pass |
| 1677 |
|
| 1678 |
class SubProto(Proto): |
| 1679 |
def meth(self) -> Optional[int]: |
| 1680 |
return super().meth() # E: Call to abstract method "meth" of "Proto" with trivial body via super() is unsafe |
| 1681 |
class SubAbstract(Abstract): |
| 1682 |
def meth(self) -> Optional[int]: |
| 1683 |
return super().meth() # E: Call to abstract method "meth" of "Abstract" with trivial body via super() is unsafe |
| 1684 |
|
| 1685 |
[case testEmptyBodyTypeCheckingOnly] |
| 1686 |
from typing import TYPE_CHECKING |
| 1687 |
|
| 1688 |
class C: |
| 1689 |
if TYPE_CHECKING: |
| 1690 |
def dynamic(self) -> int: ... # OK |