| 1 |
-- Test cases for always defined attributes. |
| 2 |
-- |
| 3 |
-- If class C has attributes x and y that are always defined, the output will |
| 4 |
-- have a line like this: |
| 5 |
-- |
| 6 |
-- C: [x, y] |
| 7 |
|
| 8 |
[case testAlwaysDefinedSimple] |
| 9 |
class C: |
| 10 |
def __init__(self, x: int) -> None: |
| 11 |
self.x = x |
| 12 |
[out] |
| 13 |
C: [x] |
| 14 |
|
| 15 |
[case testAlwaysDefinedFail] |
| 16 |
class MethodCall: |
| 17 |
def __init__(self, x: int) -> None: |
| 18 |
self.f() |
| 19 |
self.x = x |
| 20 |
|
| 21 |
def f(self) -> None: |
| 22 |
pass |
| 23 |
|
| 24 |
class FuncCall: |
| 25 |
def __init__(self, x: int) -> None: |
| 26 |
f(x) |
| 27 |
self.x = x |
| 28 |
f(self) |
| 29 |
self.y = x |
| 30 |
|
| 31 |
class GetAttr: |
| 32 |
x: int |
| 33 |
def __init__(self, x: int) -> None: |
| 34 |
a = self.x |
| 35 |
self.x = x |
| 36 |
|
| 37 |
class _Base: |
| 38 |
def __init__(self) -> None: |
| 39 |
f(self) |
| 40 |
|
| 41 |
class CallSuper(_Base): |
| 42 |
def __init__(self, x: int) -> None: |
| 43 |
super().__init__() |
| 44 |
self.x = x |
| 45 |
|
| 46 |
class Lambda: |
| 47 |
def __init__(self, x: int) -> None: |
| 48 |
f = lambda x: x + 1 |
| 49 |
self.x = x |
| 50 |
g = lambda x: self |
| 51 |
self.y = x |
| 52 |
|
| 53 |
class If: |
| 54 |
def __init__(self, x: int) -> None: |
| 55 |
self.a = 1 |
| 56 |
if x: |
| 57 |
self.x = x |
| 58 |
else: |
| 59 |
self.y = 1 |
| 60 |
|
| 61 |
class Deletable: |
| 62 |
__deletable__ = ('x', 'y') |
| 63 |
|
| 64 |
def __init__(self) -> None: |
| 65 |
self.x = 0 |
| 66 |
self.y = 1 |
| 67 |
self.z = 2 |
| 68 |
|
| 69 |
class PrimitiveWithSelf: |
| 70 |
def __init__(self, s: str) -> None: |
| 71 |
self.x = getattr(self, s) |
| 72 |
|
| 73 |
def f(a) -> None: pass |
| 74 |
[out] |
| 75 |
MethodCall: [] |
| 76 |
FuncCall: [x] |
| 77 |
GetAttr: [] |
| 78 |
CallSuper: [] |
| 79 |
Lambda: [] |
| 80 |
If: [a] |
| 81 |
Deletable: [z] |
| 82 |
PrimitiveWithSelf: [] |
| 83 |
|
| 84 |
[case testAlwaysDefinedConditional] |
| 85 |
class IfAlways: |
| 86 |
def __init__(self, x: int, y: int) -> None: |
| 87 |
if x: |
| 88 |
self.x = x |
| 89 |
self.y = y |
| 90 |
elif y: |
| 91 |
self.x = y |
| 92 |
self.y = x |
| 93 |
else: |
| 94 |
self.x = 0 |
| 95 |
self.y = 0 |
| 96 |
self.z = 0 |
| 97 |
|
| 98 |
class IfSometimes1: |
| 99 |
def __init__(self, x: int, y: int) -> None: |
| 100 |
if x: |
| 101 |
self.x = x |
| 102 |
self.y = y |
| 103 |
elif y: |
| 104 |
self.z = y |
| 105 |
self.y = x |
| 106 |
else: |
| 107 |
self.y = 0 |
| 108 |
self.a = 0 |
| 109 |
|
| 110 |
class IfSometimes2: |
| 111 |
def __init__(self, x: int, y: int) -> None: |
| 112 |
if x: |
| 113 |
self.x = x |
| 114 |
self.y = y |
| 115 |
|
| 116 |
class IfStopAnalysis1: |
| 117 |
def __init__(self, x: int, y: int) -> None: |
| 118 |
if x: |
| 119 |
self.x = x |
| 120 |
f(self) |
| 121 |
else: |
| 122 |
self.x = x |
| 123 |
self.y = y |
| 124 |
|
| 125 |
class IfStopAnalysis2: |
| 126 |
def __init__(self, x: int, y: int) -> None: |
| 127 |
if x: |
| 128 |
self.x = x |
| 129 |
else: |
| 130 |
self.x = x |
| 131 |
f(self) |
| 132 |
self.y = y |
| 133 |
|
| 134 |
class IfStopAnalysis3: |
| 135 |
def __init__(self, x: int, y: int) -> None: |
| 136 |
if x: |
| 137 |
self.x = x |
| 138 |
else: |
| 139 |
f(self) |
| 140 |
self.x = x |
| 141 |
self.y = y |
| 142 |
|
| 143 |
class IfConditionalAndNonConditional1: |
| 144 |
def __init__(self, x: int) -> None: |
| 145 |
self.x = 0 |
| 146 |
if x: |
| 147 |
self.x = x |
| 148 |
|
| 149 |
class IfConditionalAndNonConditional2: |
| 150 |
def __init__(self, x: int) -> None: |
| 151 |
# x is not considered always defined, since the second assignment may |
| 152 |
# either initialize or update. |
| 153 |
if x: |
| 154 |
self.x = x |
| 155 |
self.x = 0 |
| 156 |
|
| 157 |
def f(a) -> None: pass |
| 158 |
[out] |
| 159 |
IfAlways: [x, y, z] |
| 160 |
IfSometimes1: [y] |
| 161 |
IfSometimes2: [y] |
| 162 |
IfStopAnalysis1: [x] |
| 163 |
IfStopAnalysis2: [x] |
| 164 |
IfStopAnalysis3: [] |
| 165 |
IfConditionalAndNonConditional1: [x] |
| 166 |
IfConditionalAndNonConditional2: [] |
| 167 |
|
| 168 |
[case testAlwaysDefinedExpressions] |
| 169 |
from typing import Dict, Final, List, Set, Optional, cast |
| 170 |
|
| 171 |
import other |
| 172 |
|
| 173 |
class C: pass |
| 174 |
|
| 175 |
class Collections: |
| 176 |
def __init__(self, x: int) -> None: |
| 177 |
self.l = [x] |
| 178 |
self.d: Dict[str, str] = {} |
| 179 |
self.s: Set[int] = set() |
| 180 |
self.d2 = {'x': x} |
| 181 |
self.s2 = {x} |
| 182 |
self.l2 = [f(), None] * x |
| 183 |
self.t = tuple(self.l2) |
| 184 |
|
| 185 |
class Comparisons: |
| 186 |
def __init__(self, y: int, c: C, s: str, o: Optional[str]) -> None: |
| 187 |
self.n1 = y < 5 |
| 188 |
self.n2 = y == 5 |
| 189 |
self.c1 = y is c |
| 190 |
self.c2 = y is not c |
| 191 |
self.o1 = o is None |
| 192 |
self.o2 = o is not None |
| 193 |
self.s = s < 'x' |
| 194 |
|
| 195 |
class BinaryOps: |
| 196 |
def __init__(self, x: int, s: str) -> None: |
| 197 |
self.a = x + 2 |
| 198 |
self.b = x & 2 |
| 199 |
self.c = x * 2 |
| 200 |
self.d = -x |
| 201 |
self.e = 'x' + s |
| 202 |
self.f = x << x |
| 203 |
|
| 204 |
g = 2 |
| 205 |
|
| 206 |
class LocalsAndGlobals: |
| 207 |
def __init__(self, x: int) -> None: |
| 208 |
t = x + 1 |
| 209 |
self.a = t - t |
| 210 |
self.g = g |
| 211 |
|
| 212 |
class Booleans: |
| 213 |
def __init__(self, x: int, b: bool) -> None: |
| 214 |
self.a = True |
| 215 |
self.b = False |
| 216 |
self.c = not b |
| 217 |
self.d = b or b |
| 218 |
self.e = b and b |
| 219 |
|
| 220 |
F: Final = 3 |
| 221 |
|
| 222 |
class ModuleFinal: |
| 223 |
def __init__(self) -> None: |
| 224 |
self.a = F |
| 225 |
self.b = other.Y |
| 226 |
|
| 227 |
class ClassFinal: |
| 228 |
F: Final = 3 |
| 229 |
|
| 230 |
def __init__(self) -> None: |
| 231 |
self.a = ClassFinal.F |
| 232 |
|
| 233 |
class Literals: |
| 234 |
def __init__(self) -> None: |
| 235 |
self.a = 'x' |
| 236 |
self.b = b'x' |
| 237 |
self.c = 2.2 |
| 238 |
|
| 239 |
class ListComprehension: |
| 240 |
def __init__(self, x: List[int]) -> None: |
| 241 |
self.a = [i + 1 for i in x] |
| 242 |
|
| 243 |
class Helper: |
| 244 |
def __init__(self, arg) -> None: |
| 245 |
self.x = 0 |
| 246 |
|
| 247 |
def foo(self, arg) -> int: |
| 248 |
return 1 |
| 249 |
|
| 250 |
class AttrAccess: |
| 251 |
def __init__(self, o: Helper) -> None: |
| 252 |
self.x = o.x |
| 253 |
o.x = o.x + 1 |
| 254 |
self.y = o.foo(self.x) |
| 255 |
o.foo(self) |
| 256 |
self.z = 1 |
| 257 |
|
| 258 |
class Construct: |
| 259 |
def __init__(self) -> None: |
| 260 |
self.x = Helper(1) |
| 261 |
self.y = Helper(self) |
| 262 |
|
| 263 |
class IsInstance: |
| 264 |
def __init__(self, x: object) -> None: |
| 265 |
if isinstance(x, str): |
| 266 |
self.x = 0 |
| 267 |
elif isinstance(x, Helper): |
| 268 |
self.x = 1 |
| 269 |
elif isinstance(x, (list, tuple)): |
| 270 |
self.x = 2 |
| 271 |
else: |
| 272 |
self.x = 3 |
| 273 |
|
| 274 |
class Cast: |
| 275 |
def __init__(self, x: object) -> None: |
| 276 |
self.x = cast(int, x) |
| 277 |
self.s = cast(str, x) |
| 278 |
self.c = cast(Cast, x) |
| 279 |
|
| 280 |
class PropertyAccessGetter: |
| 281 |
def __init__(self, other: PropertyAccessGetter) -> None: |
| 282 |
self.x = other.p |
| 283 |
self.y = 1 |
| 284 |
self.z = self.p |
| 285 |
|
| 286 |
@property |
| 287 |
def p(self) -> int: |
| 288 |
return 0 |
| 289 |
|
| 290 |
class PropertyAccessSetter: |
| 291 |
def __init__(self, other: PropertyAccessSetter) -> None: |
| 292 |
other.p = 1 |
| 293 |
self.y = 1 |
| 294 |
self.z = self.p |
| 295 |
|
| 296 |
@property |
| 297 |
def p(self) -> int: |
| 298 |
return 0 |
| 299 |
|
| 300 |
@p.setter |
| 301 |
def p(self, x: int) -> None: |
| 302 |
pass |
| 303 |
|
| 304 |
def f() -> int: |
| 305 |
return 0 |
| 306 |
|
| 307 |
[file other.py] |
| 308 |
# Not compiled |
| 309 |
from typing import Final |
| 310 |
|
| 311 |
Y: Final = 3 |
| 312 |
|
| 313 |
[out] |
| 314 |
C: [] |
| 315 |
Collections: [d, d2, l, l2, s, s2, t] |
| 316 |
Comparisons: [c1, c2, n1, n2, o1, o2, s] |
| 317 |
BinaryOps: [a, b, c, d, e, f] |
| 318 |
LocalsAndGlobals: [a, g] |
| 319 |
Booleans: [a, b, c, d, e] |
| 320 |
ModuleFinal: [a, b] |
| 321 |
ClassFinal: [F, a] |
| 322 |
Literals: [a, b, c] |
| 323 |
ListComprehension: [a] |
| 324 |
Helper: [x] |
| 325 |
AttrAccess: [x, y] |
| 326 |
Construct: [x] |
| 327 |
IsInstance: [x] |
| 328 |
Cast: [c, s, x] |
| 329 |
PropertyAccessGetter: [x, y] |
| 330 |
PropertyAccessSetter: [y] |
| 331 |
|
| 332 |
[case testAlwaysDefinedExpressions2] |
| 333 |
from typing import List, Tuple |
| 334 |
|
| 335 |
class C: |
| 336 |
def __init__(self) -> None: |
| 337 |
self.x = 0 |
| 338 |
|
| 339 |
class AttributeRef: |
| 340 |
def __init__(self, c: C) -> None: |
| 341 |
self.aa = c.x |
| 342 |
self.bb = self.aa |
| 343 |
if c is not None: |
| 344 |
self.z = 0 |
| 345 |
self.cc = 0 |
| 346 |
self.dd = self.z |
| 347 |
|
| 348 |
class ListOps: |
| 349 |
def __init__(self, x: List[int], n: int) -> None: |
| 350 |
self.a = len(x) |
| 351 |
self.b = x[n] |
| 352 |
self.c = [y + 1 for y in x] |
| 353 |
|
| 354 |
class TupleOps: |
| 355 |
def __init__(self, t: Tuple[int, str]) -> None: |
| 356 |
x, y = t |
| 357 |
self.x = x |
| 358 |
self.y = t[0] |
| 359 |
s = x, y |
| 360 |
self.z = s |
| 361 |
|
| 362 |
class IfExpr: |
| 363 |
def __init__(self, x: int) -> None: |
| 364 |
self.a = 1 if x < 5 else 2 |
| 365 |
|
| 366 |
class Base: |
| 367 |
def __init__(self, x: int) -> None: |
| 368 |
self.x = x |
| 369 |
|
| 370 |
class Derived1(Base): |
| 371 |
def __init__(self, y: int) -> None: |
| 372 |
self.aa = y |
| 373 |
super().__init__(y) |
| 374 |
self.bb = y |
| 375 |
|
| 376 |
class Derived2(Base): |
| 377 |
pass |
| 378 |
|
| 379 |
class Conditionals: |
| 380 |
def __init__(self, b: bool, n: int) -> None: |
| 381 |
if not (n == 5 or n >= n + 1): |
| 382 |
self.a = b |
| 383 |
else: |
| 384 |
self.a = not b |
| 385 |
if b: |
| 386 |
self.b = 2 |
| 387 |
else: |
| 388 |
self.b = 4 |
| 389 |
|
| 390 |
[out] |
| 391 |
C: [x] |
| 392 |
AttributeRef: [aa, bb, cc, dd] |
| 393 |
ListOps: [a, b, c] |
| 394 |
TupleOps: [x, y, z] |
| 395 |
IfExpr: [a] |
| 396 |
Base: [x] |
| 397 |
Derived1: [aa, bb, x] |
| 398 |
Derived2: [x] |
| 399 |
Conditionals: [a, b] |
| 400 |
|
| 401 |
[case testAlwaysDefinedStatements] |
| 402 |
from typing import Any, List, Optional, Iterable |
| 403 |
|
| 404 |
class Return: |
| 405 |
def __init__(self, x: int) -> None: |
| 406 |
self.x = x |
| 407 |
if x > 5: |
| 408 |
self.y = 1 |
| 409 |
return |
| 410 |
self.y = 2 |
| 411 |
self.z = x |
| 412 |
|
| 413 |
class While: |
| 414 |
def __init__(self, x: int) -> None: |
| 415 |
n = 2 |
| 416 |
while x > 0: |
| 417 |
n *=2 |
| 418 |
x -= 1 |
| 419 |
self.a = n |
| 420 |
while x < 5: |
| 421 |
self.b = 1 |
| 422 |
self.b += 1 |
| 423 |
|
| 424 |
class Try: |
| 425 |
def __init__(self, x: List[int]) -> None: |
| 426 |
self.a = 0 |
| 427 |
try: |
| 428 |
self.b = x[0] |
| 429 |
except: |
| 430 |
self.c = x |
| 431 |
self.d = 0 |
| 432 |
try: |
| 433 |
self.e = x[0] |
| 434 |
except: |
| 435 |
self.e = 1 |
| 436 |
|
| 437 |
class TryFinally: |
| 438 |
def __init__(self, x: List[int]) -> None: |
| 439 |
self.a = 0 |
| 440 |
try: |
| 441 |
self.b = x[0] |
| 442 |
finally: |
| 443 |
self.c = x |
| 444 |
self.d = 0 |
| 445 |
try: |
| 446 |
self.e = x[0] |
| 447 |
finally: |
| 448 |
self.e = 1 |
| 449 |
|
| 450 |
class Assert: |
| 451 |
def __init__(self, x: Optional[str], y: int) -> None: |
| 452 |
assert x is not None |
| 453 |
assert y < 5 |
| 454 |
self.a = x |
| 455 |
|
| 456 |
class For: |
| 457 |
def __init__(self, it: Iterable[int]) -> None: |
| 458 |
self.x = 0 |
| 459 |
for x in it: |
| 460 |
self.x += x |
| 461 |
for x in it: |
| 462 |
self.y = x |
| 463 |
|
| 464 |
class Assignment1: |
| 465 |
def __init__(self, other: Assignment1) -> None: |
| 466 |
self.x = 0 |
| 467 |
self = other # Give up after assignment to self |
| 468 |
self.y = 1 |
| 469 |
|
| 470 |
class Assignment2: |
| 471 |
def __init__(self) -> None: |
| 472 |
self.x = 0 |
| 473 |
other = self # Give up after self is aliased |
| 474 |
self.y = other.x |
| 475 |
|
| 476 |
class With: |
| 477 |
def __init__(self, x: Any) -> None: |
| 478 |
self.a = 0 |
| 479 |
with x: |
| 480 |
self.b = 1 |
| 481 |
self.c = 2 |
| 482 |
|
| 483 |
def f() -> None: |
| 484 |
pass |
| 485 |
|
| 486 |
[out] |
| 487 |
Return: [x, y] |
| 488 |
While: [a] |
| 489 |
-- We could infer 'e' as always defined, but this is tricky, since always defined attribute |
| 490 |
-- analysis must be performed earlier than exception handling transform. This would be |
| 491 |
-- easy to infer *after* exception handling transform. |
| 492 |
Try: [a, d] |
| 493 |
-- Again, 'e' could be always defined, but it would be a bit tricky to do it. |
| 494 |
TryFinally: [a, c, d] |
| 495 |
Assert: [a] |
| 496 |
For: [x] |
| 497 |
Assignment1: [x] |
| 498 |
Assignment2: [x] |
| 499 |
-- TODO: Why is not 'b' included? |
| 500 |
With: [a, c] |
| 501 |
|
| 502 |
[case testAlwaysDefinedAttributeDefaults] |
| 503 |
class Basic: |
| 504 |
x = 0 |
| 505 |
|
| 506 |
class ClassBodyAndInit: |
| 507 |
x = 0 |
| 508 |
s = 'x' |
| 509 |
|
| 510 |
def __init__(self, n: int) -> None: |
| 511 |
self.n = 0 |
| 512 |
|
| 513 |
class AttrWithDefaultAndInit: |
| 514 |
x = 0 |
| 515 |
|
| 516 |
def __init__(self, x: int) -> None: |
| 517 |
self.x = x |
| 518 |
|
| 519 |
class Base: |
| 520 |
x = 0 |
| 521 |
y = 1 |
| 522 |
|
| 523 |
class Derived(Base): |
| 524 |
y = 2 |
| 525 |
z = 3 |
| 526 |
[out] |
| 527 |
Basic: [x] |
| 528 |
ClassBodyAndInit: [n, s, x] |
| 529 |
AttrWithDefaultAndInit: [x] |
| 530 |
Base: [x, y] |
| 531 |
Derived: [x, y, z] |
| 532 |
|
| 533 |
[case testAlwaysDefinedWithInheritance] |
| 534 |
class Base: |
| 535 |
def __init__(self, x: int) -> None: |
| 536 |
self.x = x |
| 537 |
|
| 538 |
class Deriv1(Base): |
| 539 |
def __init__(self, x: int, y: str) -> None: |
| 540 |
super().__init__(x) |
| 541 |
self.y = y |
| 542 |
|
| 543 |
class Deriv2(Base): |
| 544 |
def __init__(self, x: int, y: str) -> None: |
| 545 |
self.y = y |
| 546 |
super().__init__(x) |
| 547 |
|
| 548 |
class Deriv22(Deriv2): |
| 549 |
def __init__(self, x: int, y: str, z: bool) -> None: |
| 550 |
super().__init__(x, y) |
| 551 |
self.z = False |
| 552 |
|
| 553 |
class Deriv3(Base): |
| 554 |
def __init__(self) -> None: |
| 555 |
super().__init__(1) |
| 556 |
|
| 557 |
class Deriv4(Base): |
| 558 |
def __init__(self) -> None: |
| 559 |
self.y = 1 |
| 560 |
self.x = 2 |
| 561 |
|
| 562 |
def f(a): pass |
| 563 |
|
| 564 |
class BaseUnsafe: |
| 565 |
def __init__(self, x: int, y: int) -> None: |
| 566 |
self.x = x |
| 567 |
f(self) # Unknown function |
| 568 |
self.y = y |
| 569 |
|
| 570 |
class DerivUnsafe(BaseUnsafe): |
| 571 |
def __init__(self, z: int, zz: int) -> None: |
| 572 |
self.z = z |
| 573 |
super().__init__(1, 2) # Calls unknown function |
| 574 |
self.zz = zz |
| 575 |
|
| 576 |
class BaseWithDefault: |
| 577 |
x = 1 |
| 578 |
|
| 579 |
def __init__(self) -> None: |
| 580 |
self.y = 1 |
| 581 |
|
| 582 |
class DerivedWithDefault(BaseWithDefault): |
| 583 |
def __init__(self) -> None: |
| 584 |
super().__init__() |
| 585 |
self.z = 1 |
| 586 |
|
| 587 |
class AlwaysDefinedInBase: |
| 588 |
def __init__(self) -> None: |
| 589 |
self.x = 1 |
| 590 |
self.y = 1 |
| 591 |
|
| 592 |
class UndefinedInDerived(AlwaysDefinedInBase): |
| 593 |
def __init__(self, x: bool) -> None: |
| 594 |
self.x = 1 |
| 595 |
if x: |
| 596 |
self.y = 2 |
| 597 |
|
| 598 |
class UndefinedInDerived2(UndefinedInDerived): |
| 599 |
def __init__(self, x: bool): |
| 600 |
if x: |
| 601 |
self.y = 2 |
| 602 |
[out] |
| 603 |
Base: [x] |
| 604 |
Deriv1: [x, y] |
| 605 |
Deriv2: [x, y] |
| 606 |
Deriv22: [x, y, z] |
| 607 |
Deriv3: [x] |
| 608 |
Deriv4: [x, y] |
| 609 |
BaseUnsafe: [x] |
| 610 |
DerivUnsafe: [x, z] |
| 611 |
BaseWithDefault: [x, y] |
| 612 |
DerivedWithDefault: [x, y, z] |
| 613 |
AlwaysDefinedInBase: [] |
| 614 |
UndefinedInDerived: [] |
| 615 |
UndefinedInDerived2: [] |
| 616 |
|
| 617 |
[case testAlwaysDefinedWithInheritance2] |
| 618 |
from mypy_extensions import trait, mypyc_attr |
| 619 |
|
| 620 |
from interpreted import PythonBase |
| 621 |
|
| 622 |
class BasePartiallyDefined: |
| 623 |
def __init__(self, x: int) -> None: |
| 624 |
self.a = 0 |
| 625 |
if x: |
| 626 |
self.x = x |
| 627 |
|
| 628 |
class Derived1(BasePartiallyDefined): |
| 629 |
def __init__(self, x: int) -> None: |
| 630 |
super().__init__(x) |
| 631 |
self.y = x |
| 632 |
|
| 633 |
class BaseUndefined: |
| 634 |
x: int |
| 635 |
|
| 636 |
class DerivedAlwaysDefined(BaseUndefined): |
| 637 |
def __init__(self) -> None: |
| 638 |
super().__init__() |
| 639 |
self.z = 0 |
| 640 |
self.x = 2 |
| 641 |
|
| 642 |
@trait |
| 643 |
class MyTrait: |
| 644 |
def f(self) -> None: pass |
| 645 |
|
| 646 |
class SimpleTraitImpl(MyTrait): |
| 647 |
def __init__(self) -> None: |
| 648 |
super().__init__() |
| 649 |
self.x = 0 |
| 650 |
|
| 651 |
@trait |
| 652 |
class TraitWithAttr: |
| 653 |
x: int |
| 654 |
y: str |
| 655 |
|
| 656 |
class TraitWithAttrImpl(TraitWithAttr): |
| 657 |
def __init__(self) -> None: |
| 658 |
self.y = 'x' |
| 659 |
|
| 660 |
@trait |
| 661 |
class TraitWithAttr2: |
| 662 |
z: int |
| 663 |
|
| 664 |
class TraitWithAttrImpl2(TraitWithAttr, TraitWithAttr2): |
| 665 |
def __init__(self) -> None: |
| 666 |
self.y = 'x' |
| 667 |
self.z = 2 |
| 668 |
|
| 669 |
@mypyc_attr(allow_interpreted_subclasses=True) |
| 670 |
class BaseWithGeneralSubclassing: |
| 671 |
x = 0 |
| 672 |
y: int |
| 673 |
def __init__(self, s: str) -> None: |
| 674 |
self.s = s |
| 675 |
|
| 676 |
class Derived2(BaseWithGeneralSubclassing): |
| 677 |
def __init__(self) -> None: |
| 678 |
super().__init__('x') |
| 679 |
self.z = 0 |
| 680 |
|
| 681 |
class SubclassPythonclass(PythonBase): |
| 682 |
def __init__(self) -> None: |
| 683 |
self.y = 1 |
| 684 |
|
| 685 |
class BaseWithSometimesDefined: |
| 686 |
def __init__(self, b: bool) -> None: |
| 687 |
if b: |
| 688 |
self.x = 0 |
| 689 |
|
| 690 |
class Derived3(BaseWithSometimesDefined): |
| 691 |
def __init__(self, b: bool) -> None: |
| 692 |
super().__init__(b) |
| 693 |
self.x = 1 |
| 694 |
|
| 695 |
[file interpreted.py] |
| 696 |
class PythonBase: |
| 697 |
def __init__(self) -> None: |
| 698 |
self.x = 0 |
| 699 |
|
| 700 |
[out] |
| 701 |
BasePartiallyDefined: [a] |
| 702 |
Derived1: [a, y] |
| 703 |
BaseUndefined: [] |
| 704 |
DerivedAlwaysDefined: [x, z] |
| 705 |
MyTrait: [] |
| 706 |
SimpleTraitImpl: [x] |
| 707 |
TraitWithAttr: [] |
| 708 |
TraitWithAttrImpl: [y] |
| 709 |
TraitWithAttr2: [] |
| 710 |
TraitWithAttrImpl2: [y, z] |
| 711 |
BaseWithGeneralSubclassing: [] |
| 712 |
-- TODO: 's' could also be always defined |
| 713 |
Derived2: [x, z] |
| 714 |
-- Always defined attribute analysis is turned off when inheriting a non-native class. |
| 715 |
SubclassPythonclass: [] |
| 716 |
BaseWithSometimesDefined: [] |
| 717 |
-- TODO: 'x' could also be always defined, but it is a bit tricky to support |
| 718 |
Derived3: [] |
| 719 |
|
| 720 |
[case testAlwaysDefinedWithNesting] |
| 721 |
class NestedFunc: |
| 722 |
def __init__(self) -> None: |
| 723 |
self.x = 0 |
| 724 |
def f() -> None: |
| 725 |
self.y = 0 |
| 726 |
f() |
| 727 |
self.z = 1 |
| 728 |
[out] |
| 729 |
-- TODO: Support nested functions. |
| 730 |
NestedFunc: [] |
| 731 |
f___init___NestedFunc_obj: [] |
| 732 |
|
| 733 |
[case testAlwaysDefinedWithVecSetItem] |
| 734 |
from librt.vecs import vec |
| 735 |
|
| 736 |
class C: |
| 737 |
def __init__(self, v: vec[C]) -> None: |
| 738 |
self.v = v |
| 739 |
self.v[0] = self |
| 740 |
[out] |
| 741 |
C: [v] |