1.1 --- a/inspector.py Fri Sep 09 17:23:45 2016 +0200
1.2 +++ b/inspector.py Fri Sep 09 17:24:18 2016 +0200
1.3 @@ -554,12 +554,38 @@
1.4 # Initialise argument and local records.
1.5
1.6 function_name = self.get_object_path(name)
1.7 + argnames = get_argnames(n.argnames)
1.8 + is_method = self.in_class and not self.in_function
1.9
1.10 - argnames = self.importer.function_parameters[function_name] = \
1.11 - self.function_parameters[function_name] = get_argnames(n.argnames)
1.12 + # Remove explicit "self" from method parameters.
1.13 +
1.14 + if is_method and argnames and argnames[0] == "self":
1.15 + del argnames[0]
1.16 +
1.17 + # Copy and propagate the parameters.
1.18 +
1.19 + self.importer.function_parameters[function_name] = \
1.20 + self.function_parameters[function_name] = argnames[:]
1.21 +
1.22 + # Define all arguments/parameters in the local namespace.
1.23 +
1.24 locals = self.function_locals[function_name] = {}
1.25
1.26 - for argname in argnames:
1.27 + # Insert "self" into method locals.
1.28 +
1.29 + if is_method:
1.30 + argnames.insert(0, "self")
1.31 +
1.32 + # Define "self" in terms of the class if in a method.
1.33 + # This does not diminish the need for type-narrowing in the deducer.
1.34 +
1.35 + if argnames:
1.36 + if self.in_class and not self.in_function and argnames[0] == "self":
1.37 + locals[argnames[0]] = Reference("<instance>", self.in_class)
1.38 + else:
1.39 + locals[argnames[0]] = Reference("<var>")
1.40 +
1.41 + for argname in argnames[1:]:
1.42 locals[argname] = Reference("<var>")
1.43
1.44 globals = self.scope_globals[function_name] = set()
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/tests/methods.py Fri Sep 09 17:24:18 2016 +0200
2.3 @@ -0,0 +1,26 @@
2.4 +class C:
2.5 + def __init__(self, x, y, z):
2.6 + self.x = x
2.7 + self.y = y
2.8 + self.z = z
2.9 +
2.10 + def c(self):
2.11 + return self.x
2.12 +
2.13 +class D(C):
2.14 + def d(self):
2.15 + return self.y
2.16 +
2.17 +class E(D):
2.18 + def c(self):
2.19 + return self.z
2.20 +
2.21 +c = C(1, 2, 3)
2.22 +d = D(1, 2, 3)
2.23 +e = E(1, 2, 3)
2.24 +
2.25 +result1 = c.c() # 1
2.26 +result2 = d.c() # 1
2.27 +result3 = e.c() # 3
2.28 +result4 = d.d() # 2
2.29 +result5 = e.d() # 2
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/tests/methods_selfless.py Fri Sep 09 17:24:18 2016 +0200
3.3 @@ -0,0 +1,26 @@
3.4 +class C:
3.5 + def __init__(x, y, z): # no explicit self
3.6 + self.x = x
3.7 + self.y = y
3.8 + self.z = z
3.9 +
3.10 + def c():
3.11 + return self.x
3.12 +
3.13 +class D(C):
3.14 + def d():
3.15 + return self.y
3.16 +
3.17 +class E(D):
3.18 + def c():
3.19 + return self.z
3.20 +
3.21 +c = C(1, 2, 3)
3.22 +d = D(1, 2, 3)
3.23 +e = E(1, 2, 3)
3.24 +
3.25 +result1 = c.c() # 1
3.26 +result2 = d.c() # 1
3.27 +result3 = e.c() # 3
3.28 +result4 = d.d() # 2
3.29 +result5 = e.d() # 2