ducktyping tests

This commit is contained in:
David Halter
2012-09-04 15:37:37 +02:00
parent db7c2fc6e7
commit 51ee262706
3 changed files with 41 additions and 8 deletions
+2 -1
View File
@@ -892,7 +892,8 @@ def get_names_for_scope(scope, position=None, star_search=True,
the whole thing would probably start a little recursive madness. the whole thing would probably start a little recursive madness.
""" """
in_func_scope = scope in_func_scope = scope
non_flow = scope.get_parent_until(parsing.Flow, reverse=True) non_flow = scope.get_parent_until(parsing.Flow, reverse=True,
include_current=True)
while scope: while scope:
# `parsing.Class` is used, because the parent is never `Class`. # `parsing.Class` is used, because the parent is never `Class`.
# Ignore the Flows, because the classes and functions care for that. # Ignore the Flows, because the classes and functions care for that.
+4 -1
View File
@@ -99,13 +99,16 @@ class Simple(Base):
self.parent = lambda: None self.parent = lambda: None
@Python3Method @Python3Method
def get_parent_until(self, classes=(), reverse=False): def get_parent_until(self, classes=(), reverse=False,
include_current=False):
""" Takes always the parent, until one class (not a Class) """ """ Takes always the parent, until one class (not a Class) """
if type(classes) not in (tuple, list): if type(classes) not in (tuple, list):
classes = (classes,) classes = (classes,)
scope = self scope = self
while not scope.parent() is None: while not scope.parent() is None:
if classes and reverse != scope.isinstance(*classes): if classes and reverse != scope.isinstance(*classes):
if include_current:
return scope
break break
scope = scope.parent() scope = scope.parent()
return scope return scope
+35 -6
View File
@@ -198,21 +198,21 @@ V(1).d()
class A(): class A():
def b(self): def b(self):
#? int() #? int()
a() a_func()
#? str() #? str()
self.a() self.a_func()
return a() return a_func()
def a(self): def a_func(self):
return "" return ""
def a(): def a_func():
return 1 return 1
#? int() #? int()
A().b() A().b()
#? str() #? str()
A().a() A().a_func()
# ----------------- # -----------------
# nested classes # nested classes
@@ -435,3 +435,32 @@ Recursion().a
#? #?
Recursion().b Recursion().b
# -----------------
# ducktyping
# -----------------
def meth(self):
return self.a, self.b
class WithoutMethod():
a = 1
def __init__(self):
self.b = 1.0
def blub(self):
return self.b
m = meth
class B():
b = ''
a = WithoutMethod().m()
#? int()
a[0]
#? float()
a[1]
#? float()
WithoutMethod.blub(WithoutMethod())
#? str()
WithoutMethod.blub(B())