From d90f5065b09104d787911d8e6b7a8a1b030a5c5c Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Sun, 12 Oct 2014 16:29:30 -0700 Subject: [PATCH] Fix #307: Method default args can refer to class attributes --- mypy/semanal.py | 6 +++--- mypy/test/data/semanal-classes.test | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 149e7a3cf999..c8bb6a88df43 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -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: @@ -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 diff --git a/mypy/test/data/semanal-classes.test b/mypy/test/data/semanal-classes.test index 4dde4f929fed..2c85e119fe49 100644 --- a/mypy/test/data/semanal-classes.test +++ b/mypy/test/data/semanal-classes.test @@ -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()))))