Skip to content

Commit

Permalink
Fix python#307: Method default args can refer to class attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
JukkaL committed Oct 12, 2014
1 parent f22ce34 commit d90f506
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ def analyse_function(self, defn: FuncItem) -> None:
if isinstance(defn, FuncDef):
defn.info = self.type
defn.type = set_callable_name(defn.type, defn)
self.function_stack.append(defn)
self.enter()
for init in defn.init:
if init:
init.rvalue.accept(self)
self.function_stack.append(defn)
self.enter()
for v in defn.args:
self.add_local(v, defn)
for init_ in defn.init:
Expand Down Expand Up @@ -735,7 +735,7 @@ def analyse_lvalue(self, lval: Node, nested: bool = False,
Only if add_global is True, add name to globals table. If nested
is true, the lvalue is within a tuple or list lvalue expression.
"""

if isinstance(lval, NameExpr):
nested_global = (not self.is_func_scope() and
self.block_depth[-1] > 0 and
Expand Down
26 changes: 26 additions & 0 deletions mypy/test/data/semanal-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,29 @@ MypyFile:1(
Decorators(
NameExpr(builtinclass [typing.builtinclass]))
PassStmt:3()))

[case testClassAttributeAsMethodDefaultArgumentValue]
import typing
class A:
X = 1
def f(self, x : int = X) -> None: pass
[out]
MypyFile:1(
Import:1(typing : typing)
ClassDef:2(
A
AssignmentStmt:3(
NameExpr(X* [m])
IntExpr(1))
FuncDef:4(
f
Args(
Var(self)
Var(x))
def (self: __main__.A, x: builtins.int =)
Init(
AssignmentStmt:4(
NameExpr(x [l])
NameExpr(X [m])))
Block:4(
PassStmt:4()))))

0 comments on commit d90f506

Please sign in to comment.