1def find_class(): 2 """ This scope is special, because its in front of TestClass """ 3 #? ['ret'] 4 TestClass.ret 5 if 1: 6 #? ['ret'] 7 TestClass.ret 8 9class FindClass(): 10 #? [] 11 TestClass.ret 12 if a: 13 #? [] 14 TestClass.ret 15 16 def find_class(self): 17 #? ['ret'] 18 TestClass.ret 19 if 1: 20 #? ['ret'] 21 TestClass.ret 22 23#? [] 24FindClass().find_class.self 25#? [] 26FindClass().find_class.self.find_class 27 28# set variables, which should not be included, because they don't belong to the 29# class 30second = 1 31second = "" 32class TestClass(object): 33 var_class = TestClass(1) 34 self.pseudo_var = 3 35 36 def __init__(self2, first_param, second_param, third=1.0): 37 self2.var_inst = first_param 38 self2.second = second_param 39 self2.first = first_param 40 self2.first.var_on_argument = 5 41 a = 3 42 43 def var_func(self): 44 return 1 45 46 def get_first(self): 47 # traversal 48 self.second_new = self.second 49 return self.var_inst 50 51 def values(self): 52 self.var_local = 3 53 #? ['var_class', 'var_func', 'var_inst', 'var_local'] 54 self.var_ 55 #? 56 var_local 57 58 def ret(self, a1): 59 # should not know any class functions! 60 #? [] 61 values 62 #? 63 values 64 #? ['return'] 65 ret 66 return a1 67 68# should not work 69#? [] 70var_local 71#? [] 72var_inst 73#? [] 74var_func 75 76# instance 77inst = TestClass(1) 78 79#? ['var_class', 'var_func', 'var_inst', 'var_local'] 80inst.var 81 82#? ['var_class', 'var_func'] 83TestClass.var 84 85#? int() 86inst.var_local 87#? [] 88TestClass.var_local. 89#? 90TestClass.pseudo_var 91#? 92TestClass().pseudo_var 93 94#? int() 95TestClass().ret(1) 96# Should not return int(), because we want the type before `.ret(1)`. 97#? 11 TestClass() 98TestClass().ret(1) 99#? int() 100inst.ret(1) 101 102myclass = TestClass(1, '', 3.0) 103#? int() 104myclass.get_first() 105#? [] 106myclass.get_first.real 107 108# too many params 109#? int() 110TestClass(1,1,1).var_inst 111 112# too few params 113#? int() 114TestClass(1).first 115#? [] 116TestClass(1).second. 117 118# complicated variable settings in class 119#? str() 120myclass.second 121#? str() 122myclass.second_new 123 124# multiple classes / ordering 125ints = TestClass(1, 1.0) 126strs = TestClass("", '') 127#? float() 128ints.second 129#? str() 130strs.second 131 132#? ['var_class'] 133TestClass.var_class.var_class.var_class.var_class 134 135# operations (+, *, etc) shouldn't be InstanceElements - #246 136class A(): 137 def __init__(self): 138 self.addition = 1 + 2 139#? int() 140A().addition 141 142# should also work before `=` 143#? 8 int() 144A().addition = None 145#? 8 int() 146A(1).addition = None 147#? 1 A 148A(1).addition = None 149a = A() 150#? 8 int() 151a.addition = None 152 153 154# ----------------- 155# inheritance 156# ----------------- 157 158class Base(object): 159 def method_base(self): 160 return 1 161 162class SuperClass(Base): 163 class_super = 3 164 def __init__(self): 165 self.var_super = '' 166 def method_super(self): 167 self.var2_super = list 168 169class Mixin(SuperClass): 170 def method_mixin(self): 171 return int 172 173#? 20 SuperClass 174class SubClass(SuperClass): 175 class_sub = 3 176 def __init__(self): 177 self.var_sub = '' 178 def method_sub(self): 179 self.var_sub = list 180 return tuple 181 182instance = SubClass() 183 184#? ['method_base', 'method_sub', 'method_super'] 185instance.method_ 186#? ['var2_super', 'var_sub', 'var_super'] 187instance.var 188#? ['class_sub', 'class_super'] 189instance.class_ 190 191#? ['method_base', 'method_sub', 'method_super'] 192SubClass.method_ 193#? [] 194SubClass.var 195#? ['class_sub', 'class_super'] 196SubClass.class_ 197 198# ----------------- 199# inheritance of builtins 200# ----------------- 201 202class Base(str): 203 pass 204 205#? ['upper'] 206Base.upper 207#? ['upper'] 208Base().upper 209 210# ----------------- 211# dynamic inheritance 212# ----------------- 213 214class Angry(object): 215 def shout(self): 216 return 'THIS IS MALARKEY!' 217 218def classgetter(): 219 return Angry 220 221class Dude(classgetter()): 222 def react(self): 223 #? ['shout'] 224 self.s 225 226# ----------------- 227# multiple inheritance # 1071 228# ----------------- 229 230class FactorMixin(object): 231 FACTOR_1 = 0.1 232 233class Calc(object): 234 def sum(self, a, b): 235 self.xxx = 3 236 return a + b 237 238class BetterCalc(Calc, FactorMixin): 239 def multiply_factor(self, a): 240 return a * self.FACTOR_1 241 242calc = BetterCalc() 243#? ['sum'] 244calc.sum 245#? ['multiply_factor'] 246calc.multip 247#? ['FACTOR_1'] 248calc.FACTOR_1 249#? ['xxx'] 250calc.xxx 251 252# ----------------- 253# __call__ 254# ----------------- 255 256class CallClass(): 257 def __call__(self): 258 return 1 259 260#? int() 261CallClass()() 262 263# ----------------- 264# variable assignments 265# ----------------- 266 267class V: 268 def __init__(self, a): 269 self.a = a 270 271 def ret(self): 272 return self.a 273 274 d = b 275 b = ret 276 if 1: 277 c = b 278 279#? int() 280V(1).b() 281#? int() 282V(1).c() 283#? 284V(1).d() 285# Only keywords should be possible to complete. 286#? ['is', 'in', 'not', 'and', 'or', 'if'] 287V(1).d() 288 289 290# ----------------- 291# ordering 292# ----------------- 293class A(): 294 def b(self): 295 #? int() 296 a_func() 297 #? str() 298 self.a_func() 299 return a_func() 300 301 def a_func(self): 302 return "" 303 304def a_func(): 305 return 1 306 307#? int() 308A().b() 309#? str() 310A().a_func() 311 312# ----------------- 313# nested classes 314# ----------------- 315class A(): 316 class B(): 317 pass 318 def b(self): 319 return 1.0 320 321#? float() 322A().b() 323 324class A(): 325 def b(self): 326 class B(): 327 def b(self): 328 return [] 329 return B().b() 330 331#? list() 332A().b() 333 334# ----------------- 335# ducktyping 336# ----------------- 337 338def meth(self): 339 return self.a, self.b 340 341class WithoutMethod(): 342 a = 1 343 def __init__(self): 344 self.b = 1.0 345 def blub(self): 346 return self.b 347 m = meth 348 349class B(): 350 b = '' 351 352a = WithoutMethod().m() 353#? int() 354a[0] 355#? float() 356a[1] 357 358#? float() 359WithoutMethod.blub(WithoutMethod()) 360#? str() 361WithoutMethod.blub(B()) 362 363# ----------------- 364# __getattr__ / getattr() / __getattribute__ 365# ----------------- 366 367#? str().upper 368getattr(str(), 'upper') 369#? str.upper 370getattr(str, 'upper') 371 372# some strange getattr calls 373#? 374getattr(str, 1) 375#? 376getattr() 377#? 378getattr(str) 379#? 380getattr(getattr, 1) 381#? 382getattr(str, []) 383 384 385class Base(): 386 def ret(self, b): 387 return b 388 389class Wrapper(): 390 def __init__(self, obj): 391 self.obj = obj 392 393 def __getattr__(self, name): 394 return getattr(self.obj, name) 395 396class Wrapper2(): 397 def __getattribute__(self, name): 398 return getattr(Base(), name) 399 400#? int() 401Wrapper(Base()).ret(3) 402#? ['ret'] 403Wrapper(Base()).ret 404#? int() 405Wrapper(Wrapper(Base())).ret(3) 406#? ['ret'] 407Wrapper(Wrapper(Base())).ret 408 409#? int() 410Wrapper2(Base()).ret(3) 411 412class GetattrArray(): 413 def __getattr__(self, name): 414 return [1] 415 416#? int() 417GetattrArray().something[0] 418#? [] 419GetattrArray().something 420 421class WeirdGetattr: 422 class __getattr__(): 423 pass 424 425#? [] 426WeirdGetattr().something 427 428 429# ----------------- 430# private vars 431# ----------------- 432class PrivateVar(): 433 def __init__(self): 434 self.__var = 1 435 #? int() 436 self.__var 437 #? ['__var'] 438 self.__var 439 440 def __private_func(self): 441 return 1 442 443 #? int() 444 __private_func() 445 446 def wrap_private(self): 447 return self.__private_func() 448#? [] 449PrivateVar().__var 450#? 451PrivateVar().__var 452#? [] 453PrivateVar().__private_func 454#? [] 455PrivateVar.__private_func 456#? int() 457PrivateVar().wrap_private() 458 459 460class PrivateSub(PrivateVar): 461 def test(self): 462 #? [] 463 self.__var 464 465 def wrap_private(self): 466 #? [] 467 self.__var 468 469#? [] 470PrivateSub().__var 471 472# ----------------- 473# super 474# ----------------- 475class Super(object): 476 a = 3 477 def return_sup(self): 478 return 1 479SuperCopy = Super 480 481class TestSuper(Super): 482 #? 483 super() 484 def test(self): 485 #? SuperCopy() 486 super() 487 #? ['a'] 488 super().a 489 if 1: 490 #? SuperCopy() 491 super() 492 def a(): 493 #? 494 super() 495 496 def return_sup(self): 497 #? int() 498 return super().return_sup() 499 500#? int() 501TestSuper().return_sup() 502 503 504Super = 3 505 506class Foo(): 507 def foo(self): 508 return 1 509# Somehow overwriting the same name caused problems (#1044) 510class Foo(Foo): 511 def foo(self): 512 #? int() 513 super().foo() 514 515# ----------------- 516# if flow at class level 517# ----------------- 518class TestX(object): 519 def normal_method(self): 520 return 1 521 522 if True: 523 def conditional_method(self): 524 var = self.normal_method() 525 #? int() 526 var 527 return 2 528 529 def other_method(self): 530 var = self.conditional_method() 531 #? int() 532 var 533 534# ----------------- 535# mro method 536# ----------------- 537 538class A(object): 539 a = 3 540 541#? ['mro'] 542A.mro 543#? [] 544A().mro 545 546 547# ----------------- 548# mro resolution 549# ----------------- 550 551class B(A()): 552 b = 3 553 554#? 555B.a 556#? 557B().a 558#? int() 559B.b 560#? int() 561B().b 562 563 564# ----------------- 565# With import 566# ----------------- 567 568from import_tree.classes import Config2, BaseClass 569 570class Config(BaseClass): 571 """#884""" 572 573#? Config2() 574Config.mode 575 576#? int() 577Config.mode2 578 579 580# ----------------- 581# Nested class/def/class 582# ----------------- 583class Foo(object): 584 a = 3 585 def create_class(self): 586 class X(): 587 a = self.a 588 self.b = 3.0 589 return X 590 591#? int() 592Foo().create_class().a 593#? float() 594Foo().b 595 596class Foo(object): 597 def comprehension_definition(self): 598 return [1 for self.b in [1]] 599 600#? int() 601Foo().b 602 603# ----------------- 604# default arguments 605# ----------------- 606 607default = '' 608class DefaultArg(): 609 default = 3 610 def x(self, arg=default): 611 #? str() 612 default 613 return arg 614 def y(self): 615 return default 616 617#? int() 618DefaultArg().x() 619#? str() 620DefaultArg().y() 621#? int() 622DefaultArg.x() 623#? str() 624DefaultArg.y() 625 626 627# ----------------- 628# Error Recovery 629# ----------------- 630 631from import_tree.pkg.base import MyBase 632 633class C1(MyBase): 634 def f3(self): 635 #! 13 ['def f1'] 636 self.f1() . # hey''' 637 #? 13 MyBase.f1 638 self.f1() . # hey''' 639 640# ----------------- 641# With a very weird __init__ 642# ----------------- 643 644class WithWeirdInit: 645 class __init__: 646 def __init__(self, a): 647 self.a = a 648 649 def y(self): 650 return self.a 651 652 653#? 654WithWeirdInit(1).y() 655