| 1 |
-- Test cases related to classes for the semantic analyzer. |
| 2 |
|
| 3 |
[case testSimpleClass] |
| 4 |
class A: pass |
| 5 |
x = A |
| 6 |
[out] |
| 7 |
MypyFile:1( |
| 8 |
ClassDef:1( |
| 9 |
A |
| 10 |
PassStmt:1()) |
| 11 |
AssignmentStmt:2( |
| 12 |
NameExpr(x* [__main__.x]) |
| 13 |
NameExpr(A [__main__.A]))) |
| 14 |
|
| 15 |
[case testMethods] |
| 16 |
class A: |
| 17 |
def __init__(self, x): |
| 18 |
y = x |
| 19 |
def f(self): |
| 20 |
y = self |
| 21 |
[out] |
| 22 |
MypyFile:1( |
| 23 |
ClassDef:1( |
| 24 |
A |
| 25 |
FuncDef:2( |
| 26 |
__init__ |
| 27 |
Args( |
| 28 |
Var(self) |
| 29 |
Var(x)) |
| 30 |
Block:3( |
| 31 |
AssignmentStmt:3( |
| 32 |
NameExpr(y* [l]) |
| 33 |
NameExpr(x [l])))) |
| 34 |
FuncDef:4( |
| 35 |
f |
| 36 |
Args( |
| 37 |
Var(self)) |
| 38 |
Block:5( |
| 39 |
AssignmentStmt:5( |
| 40 |
NameExpr(y* [l]) |
| 41 |
NameExpr(self [l])))))) |
| 42 |
|
| 43 |
[case testMemberDefinitionInInit] |
| 44 |
class A: |
| 45 |
def __init__(self): |
| 46 |
self.x = 1 |
| 47 |
self.y = 2 |
| 48 |
[out] |
| 49 |
MypyFile:1( |
| 50 |
ClassDef:1( |
| 51 |
A |
| 52 |
FuncDef:2( |
| 53 |
__init__ |
| 54 |
Args( |
| 55 |
Var(self)) |
| 56 |
Block:3( |
| 57 |
AssignmentStmt:3( |
| 58 |
MemberExpr:3( |
| 59 |
NameExpr(self [l]) |
| 60 |
x*) |
| 61 |
IntExpr(1)) |
| 62 |
AssignmentStmt:4( |
| 63 |
MemberExpr:4( |
| 64 |
NameExpr(self [l]) |
| 65 |
y*) |
| 66 |
IntExpr(2)))))) |
| 67 |
|
| 68 |
[case testMemberAssignmentViaSelfOutsideInit] |
| 69 |
class A: |
| 70 |
def f(self): |
| 71 |
self.x = 1 |
| 72 |
def __init__(self): |
| 73 |
self.y = 1 |
| 74 |
[out] |
| 75 |
MypyFile:1( |
| 76 |
ClassDef:1( |
| 77 |
A |
| 78 |
FuncDef:2( |
| 79 |
f |
| 80 |
Args( |
| 81 |
Var(self)) |
| 82 |
Block:3( |
| 83 |
AssignmentStmt:3( |
| 84 |
MemberExpr:3( |
| 85 |
NameExpr(self [l]) |
| 86 |
x*) |
| 87 |
IntExpr(1))))) |
| 88 |
FuncDef:4( |
| 89 |
__init__ |
| 90 |
Args( |
| 91 |
Var(self)) |
| 92 |
Block:5( |
| 93 |
AssignmentStmt:5( |
| 94 |
MemberExpr:5( |
| 95 |
NameExpr(self [l]) |
| 96 |
y) |
| 97 |
IntExpr(1))))) |
| 98 |
|
| 99 |
[case testMemberAssignmentNotViaSelf] |
| 100 |
class A: |
| 101 |
def __init__(x, self): |
| 102 |
self.y = 1 # not really self |
| 103 |
class B: |
| 104 |
def __init__(x): |
| 105 |
self = x |
| 106 |
self.z = 1 |
| 107 |
[out] |
| 108 |
MypyFile:1( |
| 109 |
ClassDef:1( |
| 110 |
A |
| 111 |
FuncDef:2( |
| 112 |
__init__ |
| 113 |
Args( |
| 114 |
Var(x) |
| 115 |
Var(self)) |
| 116 |
Block:3( |
| 117 |
AssignmentStmt:3( |
| 118 |
MemberExpr:3( |
| 119 |
NameExpr(self [l]) |
| 120 |
y) |
| 121 |
IntExpr(1))))) |
| 122 |
ClassDef:4( |
| 123 |
B |
| 124 |
FuncDef:5( |
| 125 |
__init__ |
| 126 |
Args( |
| 127 |
Var(x)) |
| 128 |
Block:6( |
| 129 |
AssignmentStmt:6( |
| 130 |
NameExpr(self* [l]) |
| 131 |
NameExpr(x [l])) |
| 132 |
AssignmentStmt:7( |
| 133 |
MemberExpr:7( |
| 134 |
NameExpr(self [l]) |
| 135 |
z) |
| 136 |
IntExpr(1)))))) |
| 137 |
|
| 138 |
[case testNonStandardNameForSelfAndInit] |
| 139 |
class A: |
| 140 |
def __init__(x): |
| 141 |
x.y = 1 |
| 142 |
[out] |
| 143 |
MypyFile:1( |
| 144 |
ClassDef:1( |
| 145 |
A |
| 146 |
FuncDef:2( |
| 147 |
__init__ |
| 148 |
Args( |
| 149 |
Var(x)) |
| 150 |
Block:3( |
| 151 |
AssignmentStmt:3( |
| 152 |
MemberExpr:3( |
| 153 |
NameExpr(x [l]) |
| 154 |
y*) |
| 155 |
IntExpr(1)))))) |
| 156 |
|
| 157 |
[case testAssignmentAfterAttributeInit] |
| 158 |
class A: |
| 159 |
def __init__(self): |
| 160 |
self.x = 1 |
| 161 |
self.x = 2 |
| 162 |
[out] |
| 163 |
MypyFile:1( |
| 164 |
ClassDef:1( |
| 165 |
A |
| 166 |
FuncDef:2( |
| 167 |
__init__ |
| 168 |
Args( |
| 169 |
Var(self)) |
| 170 |
Block:3( |
| 171 |
AssignmentStmt:3( |
| 172 |
MemberExpr:3( |
| 173 |
NameExpr(self [l]) |
| 174 |
x*) |
| 175 |
IntExpr(1)) |
| 176 |
AssignmentStmt:4( |
| 177 |
MemberExpr:4( |
| 178 |
NameExpr(self [l]) |
| 179 |
x) |
| 180 |
IntExpr(2)))))) |
| 181 |
|
| 182 |
[case testOverloadedMethod] |
| 183 |
from typing import overload |
| 184 |
class A: |
| 185 |
@overload |
| 186 |
def f(self) -> None: self |
| 187 |
@overload |
| 188 |
def f(self, x: 'A') -> None: self |
| 189 |
def f(self, *args): self |
| 190 |
[out] |
| 191 |
MypyFile:1( |
| 192 |
ImportFrom:1(typing, [overload]) |
| 193 |
ClassDef:2( |
| 194 |
A |
| 195 |
OverloadedFuncDef:3( |
| 196 |
FuncDef:7( |
| 197 |
f |
| 198 |
Args( |
| 199 |
Var(self)) |
| 200 |
VarArg( |
| 201 |
Var(args)) |
| 202 |
Block:7( |
| 203 |
ExpressionStmt:7( |
| 204 |
NameExpr(self [l])))) |
| 205 |
Overload(def (self: __main__.A), \ |
| 206 |
def (self: __main__.A, x: __main__.A)) |
| 207 |
Decorator:3( |
| 208 |
Var(f) |
| 209 |
NameExpr(overload [typing.overload]) |
| 210 |
FuncDef:4( |
| 211 |
f |
| 212 |
Args( |
| 213 |
Var(self)) |
| 214 |
def (self: __main__.A) |
| 215 |
Block:4( |
| 216 |
ExpressionStmt:4( |
| 217 |
NameExpr(self [l]))))) |
| 218 |
Decorator:5( |
| 219 |
Var(f) |
| 220 |
NameExpr(overload [typing.overload]) |
| 221 |
FuncDef:6( |
| 222 |
f |
| 223 |
Args( |
| 224 |
Var(self) |
| 225 |
Var(x)) |
| 226 |
def (self: __main__.A, x: __main__.A) |
| 227 |
Block:6( |
| 228 |
ExpressionStmt:6( |
| 229 |
NameExpr(self [l])))))))) |
| 230 |
|
| 231 |
[case testAttributeWithoutType] |
| 232 |
class A: |
| 233 |
a = object |
| 234 |
[out] |
| 235 |
MypyFile:1( |
| 236 |
ClassDef:1( |
| 237 |
A |
| 238 |
AssignmentStmt:2( |
| 239 |
NameExpr(a* [m]) |
| 240 |
NameExpr(object [builtins.object])))) |
| 241 |
|
| 242 |
[case testDataAttributeRefInClassBody] |
| 243 |
class A: |
| 244 |
x = 1 |
| 245 |
y = x |
| 246 |
[out] |
| 247 |
MypyFile:1( |
| 248 |
ClassDef:1( |
| 249 |
A |
| 250 |
AssignmentStmt:2( |
| 251 |
NameExpr(x [m]) |
| 252 |
IntExpr(1) |
| 253 |
builtins.int) |
| 254 |
AssignmentStmt:3( |
| 255 |
NameExpr(y* [m]) |
| 256 |
NameExpr(x [__main__.A.x])))) |
| 257 |
|
| 258 |
[case testMethodRefInClassBody] |
| 259 |
class A: |
| 260 |
def f(self): pass |
| 261 |
g = f |
| 262 |
[out] |
| 263 |
MypyFile:1( |
| 264 |
ClassDef:1( |
| 265 |
A |
| 266 |
FuncDef:2( |
| 267 |
f |
| 268 |
Args( |
| 269 |
Var(self)) |
| 270 |
Block:2( |
| 271 |
PassStmt:2())) |
| 272 |
AssignmentStmt:3( |
| 273 |
NameExpr(g* [m]) |
| 274 |
NameExpr(f [__main__.A.f])))) |
| 275 |
|
| 276 |
[case testIfStatementInClassBody] |
| 277 |
class A: |
| 278 |
if A: |
| 279 |
x = 1 |
| 280 |
else: |
| 281 |
x = 2 |
| 282 |
[out] |
| 283 |
MypyFile:1( |
| 284 |
ClassDef:1( |
| 285 |
A |
| 286 |
IfStmt:2( |
| 287 |
If( |
| 288 |
NameExpr(A [__main__.A])) |
| 289 |
Then( |
| 290 |
AssignmentStmt:3( |
| 291 |
NameExpr(x [m]) |
| 292 |
IntExpr(1) |
| 293 |
builtins.int)) |
| 294 |
Else( |
| 295 |
AssignmentStmt:5( |
| 296 |
NameExpr(x [__main__.A.x]) |
| 297 |
IntExpr(2)))))) |
| 298 |
|
| 299 |
[case testForStatementInClassBody] |
| 300 |
class A: |
| 301 |
for x in [1, 2]: |
| 302 |
y = x |
| 303 |
[out] |
| 304 |
MypyFile:1( |
| 305 |
ClassDef:1( |
| 306 |
A |
| 307 |
ForStmt:2( |
| 308 |
NameExpr(x* [m]) |
| 309 |
ListExpr:2( |
| 310 |
IntExpr(1) |
| 311 |
IntExpr(2)) |
| 312 |
Block:3( |
| 313 |
AssignmentStmt:3( |
| 314 |
NameExpr(y* [m]) |
| 315 |
NameExpr(x [__main__.A.x])))))) |
| 316 |
|
| 317 |
[case testReferenceToClassWithinFunction] |
| 318 |
def f(): |
| 319 |
class A: pass |
| 320 |
A |
| 321 |
[out] |
| 322 |
MypyFile:1( |
| 323 |
FuncDef:1( |
| 324 |
f |
| 325 |
Block:2( |
| 326 |
ClassDef:2( |
| 327 |
A |
| 328 |
PassStmt:2()) |
| 329 |
ExpressionStmt:3( |
| 330 |
NameExpr(A [__main__.A@2]))))) |
| 331 |
|
| 332 |
[case testReferenceToClassWithinClass] |
| 333 |
class A: |
| 334 |
class B: pass |
| 335 |
B |
| 336 |
[out] |
| 337 |
MypyFile:1( |
| 338 |
ClassDef:1( |
| 339 |
A |
| 340 |
ClassDef:2( |
| 341 |
B |
| 342 |
PassStmt:2()) |
| 343 |
ExpressionStmt:3( |
| 344 |
NameExpr(B [__main__.A.B])))) |
| 345 |
|
| 346 |
[case testClassWithBaseClassWithinClass] |
| 347 |
class A: |
| 348 |
class B: pass |
| 349 |
class C(B): pass |
| 350 |
[out] |
| 351 |
MypyFile:1( |
| 352 |
ClassDef:1( |
| 353 |
A |
| 354 |
ClassDef:2( |
| 355 |
B |
| 356 |
PassStmt:2()) |
| 357 |
ClassDef:3( |
| 358 |
C |
| 359 |
BaseType( |
| 360 |
__main__.A.B) |
| 361 |
PassStmt:3()))) |
| 362 |
|
| 363 |
[case testDeclarationReferenceToNestedClass] |
| 364 |
def f() -> None: |
| 365 |
class A: pass |
| 366 |
x = None # type: A |
| 367 |
[out] |
| 368 |
MypyFile:1( |
| 369 |
FuncDef:1( |
| 370 |
f |
| 371 |
def () |
| 372 |
Block:2( |
| 373 |
ClassDef:2( |
| 374 |
A |
| 375 |
PassStmt:2()) |
| 376 |
AssignmentStmt:3( |
| 377 |
NameExpr(x [l]) |
| 378 |
NameExpr(None [builtins.None]) |
| 379 |
__main__.A@2)))) |
| 380 |
|
| 381 |
[case testAccessToLocalInOuterScopeWithinNestedClass] |
| 382 |
def f(x): |
| 383 |
class A: |
| 384 |
y = x |
| 385 |
def g(self): |
| 386 |
z = x |
| 387 |
[out] |
| 388 |
MypyFile:1( |
| 389 |
FuncDef:1( |
| 390 |
f |
| 391 |
Args( |
| 392 |
Var(x)) |
| 393 |
Block:2( |
| 394 |
ClassDef:2( |
| 395 |
A |
| 396 |
AssignmentStmt:3( |
| 397 |
NameExpr(y* [m]) |
| 398 |
NameExpr(x [l])) |
| 399 |
FuncDef:4( |
| 400 |
g |
| 401 |
Args( |
| 402 |
Var(self)) |
| 403 |
Block:5( |
| 404 |
AssignmentStmt:5( |
| 405 |
NameExpr(z* [l]) |
| 406 |
NameExpr(x [l])))))))) |
| 407 |
|
| 408 |
[case testQualifiedMetaclass] |
| 409 |
import abc |
| 410 |
class A(metaclass=abc.ABCMeta): pass |
| 411 |
[out] |
| 412 |
MypyFile:1( |
| 413 |
Import:1(abc) |
| 414 |
ClassDef:2( |
| 415 |
A |
| 416 |
Keywords(metaclass=MemberExpr:2( |
| 417 |
NameExpr(abc) |
| 418 |
ABCMeta [abc.ABCMeta])) |
| 419 |
Metaclass(MemberExpr:2( |
| 420 |
NameExpr(abc) |
| 421 |
ABCMeta [abc.ABCMeta])) |
| 422 |
PassStmt:2())) |
| 423 |
|
| 424 |
[case testStaticMethod] |
| 425 |
class A: |
| 426 |
@staticmethod |
| 427 |
def f(z: int) -> str: pass |
| 428 |
[builtins fixtures/staticmethod.pyi] |
| 429 |
[out] |
| 430 |
MypyFile:1( |
| 431 |
ClassDef:1( |
| 432 |
A |
| 433 |
Decorator:2( |
| 434 |
Var(f) |
| 435 |
FuncDef:3( |
| 436 |
f |
| 437 |
Args( |
| 438 |
Var(z)) |
| 439 |
def (z: builtins.int) -> builtins.str |
| 440 |
Static |
| 441 |
Block:3( |
| 442 |
PassStmt:3()))))) |
| 443 |
|
| 444 |
[case testStaticMethodWithNoArgs] |
| 445 |
class A: |
| 446 |
@staticmethod |
| 447 |
def f() -> str: pass |
| 448 |
[builtins fixtures/staticmethod.pyi] |
| 449 |
[out] |
| 450 |
MypyFile:1( |
| 451 |
ClassDef:1( |
| 452 |
A |
| 453 |
Decorator:2( |
| 454 |
Var(f) |
| 455 |
FuncDef:3( |
| 456 |
f |
| 457 |
def () -> builtins.str |
| 458 |
Static |
| 459 |
Block:3( |
| 460 |
PassStmt:3()))))) |
| 461 |
|
| 462 |
[case testClassMethod] |
| 463 |
class A: |
| 464 |
@classmethod |
| 465 |
def f(cls, z: int) -> str: pass |
| 466 |
[builtins fixtures/classmethod.pyi] |
| 467 |
[out] |
| 468 |
MypyFile:1( |
| 469 |
ClassDef:1( |
| 470 |
A |
| 471 |
Decorator:2( |
| 472 |
Var(f) |
| 473 |
FuncDef:3( |
| 474 |
f |
| 475 |
Args( |
| 476 |
Var(cls) |
| 477 |
Var(z)) |
| 478 |
def (cls: type[__main__.A], z: builtins.int) -> builtins.str |
| 479 |
Class |
| 480 |
Block:3( |
| 481 |
PassStmt:3()))))) |
| 482 |
|
| 483 |
[case testClassMethodWithNoArgs] |
| 484 |
class A: |
| 485 |
@classmethod |
| 486 |
def f(cls) -> str: pass |
| 487 |
[builtins fixtures/classmethod.pyi] |
| 488 |
[out] |
| 489 |
MypyFile:1( |
| 490 |
ClassDef:1( |
| 491 |
A |
| 492 |
Decorator:2( |
| 493 |
Var(f) |
| 494 |
FuncDef:3( |
| 495 |
f |
| 496 |
Args( |
| 497 |
Var(cls)) |
| 498 |
def (cls: type[__main__.A]) -> builtins.str |
| 499 |
Class |
| 500 |
Block:3( |
| 501 |
PassStmt:3()))))) |
| 502 |
|
| 503 |
[case testProperty] |
| 504 |
import typing |
| 505 |
class A: |
| 506 |
@property |
| 507 |
def f(self) -> str: pass |
| 508 |
[builtins fixtures/property.pyi] |
| 509 |
[out] |
| 510 |
MypyFile:1( |
| 511 |
Import:1(typing) |
| 512 |
ClassDef:2( |
| 513 |
A |
| 514 |
Decorator:3( |
| 515 |
Var(f) |
| 516 |
FuncDef:4( |
| 517 |
f |
| 518 |
Args( |
| 519 |
Var(self)) |
| 520 |
def (self: __main__.A) -> builtins.str |
| 521 |
Property |
| 522 |
Block:4( |
| 523 |
PassStmt:4()))))) |
| 524 |
|
| 525 |
[case testClassDecorator] |
| 526 |
import typing |
| 527 |
@object |
| 528 |
class A: pass |
| 529 |
[out] |
| 530 |
MypyFile:1( |
| 531 |
Import:1(typing) |
| 532 |
ClassDef:3( |
| 533 |
A |
| 534 |
Decorators( |
| 535 |
NameExpr(object [builtins.object])) |
| 536 |
PassStmt:3())) |
| 537 |
|
| 538 |
[case testClassAttributeAsMethodDefaultArgumentValue] |
| 539 |
import typing |
| 540 |
class A: |
| 541 |
X = 1 |
| 542 |
def f(self, x : int = X) -> None: pass |
| 543 |
[out] |
| 544 |
MypyFile:1( |
| 545 |
Import:1(typing) |
| 546 |
ClassDef:2( |
| 547 |
A |
| 548 |
AssignmentStmt:3( |
| 549 |
NameExpr(X [m]) |
| 550 |
IntExpr(1) |
| 551 |
builtins.int) |
| 552 |
FuncDef:4( |
| 553 |
f |
| 554 |
Args( |
| 555 |
Var(self) |
| 556 |
default( |
| 557 |
Var(x) |
| 558 |
NameExpr(X [__main__.A.X]))) |
| 559 |
def (self: __main__.A, x: builtins.int =) |
| 560 |
Block:4( |
| 561 |
PassStmt:4())))) |
| 562 |
|
| 563 |
[case testInvalidBaseClass] |
| 564 |
|
| 565 |
from typing import Any, Callable |
| 566 |
class A(None): pass |
| 567 |
class B(Any): pass |
| 568 |
class C(Callable[[], int]): pass |
| 569 |
[out] |
| 570 |
main:3: error: Invalid base class "None" |
| 571 |
main:5: error: Invalid base class "Callable" |
| 572 |
|
| 573 |
[case testTupleAsBaseClass] |
| 574 |
|
| 575 |
import m |
| 576 |
[file m.pyi] |
| 577 |
from typing import Tuple |
| 578 |
class A(Tuple[int, str]): pass |
| 579 |
[builtins fixtures/tuple.pyi] |
| 580 |
[out] |
| 581 |
MypyFile:1( |
| 582 |
Import:2(m)) |
| 583 |
MypyFile:1( |
| 584 |
tmp/m.pyi |
| 585 |
ImportFrom:1(typing, [Tuple]) |
| 586 |
ClassDef:2( |
| 587 |
A |
| 588 |
TupleType( |
| 589 |
tuple[builtins.int, builtins.str]) |
| 590 |
BaseType( |
| 591 |
builtins.tuple[builtins.int | builtins.str, ...]) |
| 592 |
PassStmt:2())) |
| 593 |
|
| 594 |
[case testBaseClassFromIgnoredModule] |
| 595 |
import m # type: ignore |
| 596 |
class B(m.A): |
| 597 |
pass |
| 598 |
[out] |
| 599 |
MypyFile:1( |
| 600 |
Import:1(m) |
| 601 |
ClassDef:2( |
| 602 |
B |
| 603 |
FallbackToAny |
| 604 |
PassStmt:3()) |
| 605 |
IgnoredLines(1)) |
| 606 |
|
| 607 |
[case testBaseClassFromIgnoredModuleUsingImportFrom] |
| 608 |
from m import A # type: ignore |
| 609 |
class B(A, int): |
| 610 |
pass |
| 611 |
[out] |
| 612 |
MypyFile:1( |
| 613 |
ImportFrom:1(m, [A]) |
| 614 |
ClassDef:2( |
| 615 |
B |
| 616 |
FallbackToAny |
| 617 |
BaseType( |
| 618 |
builtins.int) |
| 619 |
PassStmt:3()) |
| 620 |
IgnoredLines(1)) |
| 621 |
|
| 622 |
[case testBaseClassWithExplicitAnyType] |
| 623 |
from typing import Any |
| 624 |
A = 1 # type: Any |
| 625 |
class B(A): |
| 626 |
pass |
| 627 |
[out] |
| 628 |
MypyFile:1( |
| 629 |
ImportFrom:1(typing, [Any]) |
| 630 |
AssignmentStmt:2( |
| 631 |
NameExpr(A [__main__.A]) |
| 632 |
IntExpr(1) |
| 633 |
Any) |
| 634 |
ClassDef:3( |
| 635 |
B |
| 636 |
FallbackToAny |
| 637 |
PassStmt:4())) |