mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-29 08:26:53 +08:00
ducktyping tests
This commit is contained in:
@@ -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.
|
||||
"""
|
||||
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:
|
||||
# `parsing.Class` is used, because the parent is never `Class`.
|
||||
# Ignore the Flows, because the classes and functions care for that.
|
||||
|
||||
@@ -99,13 +99,16 @@ class Simple(Base):
|
||||
self.parent = lambda: None
|
||||
|
||||
@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) """
|
||||
if type(classes) not in (tuple, list):
|
||||
classes = (classes,)
|
||||
scope = self
|
||||
while not scope.parent() is None:
|
||||
if classes and reverse != scope.isinstance(*classes):
|
||||
if include_current:
|
||||
return scope
|
||||
break
|
||||
scope = scope.parent()
|
||||
return scope
|
||||
|
||||
@@ -198,21 +198,21 @@ V(1).d()
|
||||
class A():
|
||||
def b(self):
|
||||
#? int()
|
||||
a()
|
||||
a_func()
|
||||
#? str()
|
||||
self.a()
|
||||
return a()
|
||||
self.a_func()
|
||||
return a_func()
|
||||
|
||||
def a(self):
|
||||
def a_func(self):
|
||||
return ""
|
||||
|
||||
def a():
|
||||
def a_func():
|
||||
return 1
|
||||
|
||||
#? int()
|
||||
A().b()
|
||||
#? str()
|
||||
A().a()
|
||||
A().a_func()
|
||||
|
||||
# -----------------
|
||||
# nested classes
|
||||
@@ -435,3 +435,32 @@ Recursion().a
|
||||
|
||||
#?
|
||||
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())
|
||||
|
||||
Reference in New Issue
Block a user