Skip to content

Do pass 1 semanatic analysis on the FuncDef of decorated functions #5654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions mypy/semanal_pass1.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,20 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
for lval in s.lvalues:
self.analyze_lvalue(lval, explicit_type=s.type is not None)

def visit_func_def(self, func: FuncDef) -> None:
def visit_func_def(self, func: FuncDef, decorated: bool = False) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would document the argument and the need for it, because it is not obvious.

"""Process a func def.

decorated is true if we are processing a func def in a
Decorator that needs a _fullname and to have its body analyzed but
does not need to be added to the symbol table.
"""
sem = self.sem
if sem.type is not None:
# Don't process methods during pass 1.
return
func.is_conditional = sem.block_depth[-1] > 0
func._fullname = sem.qualified_name(func.name())
at_module = sem.is_module_scope()
at_module = sem.is_module_scope() and not decorated
if (at_module and func.name() == '__getattr__' and
self.sem.cur_mod_node.is_package_init_file() and self.sem.cur_mod_node.is_stub):
if isinstance(func.type, CallableType):
Expand Down Expand Up @@ -311,6 +317,7 @@ def visit_decorator(self, d: Decorator) -> None:
return
d.var._fullname = self.sem.qualified_name(d.var.name())
self.add_symbol(d.var.name(), SymbolTableNode(self.kind_by_scope(), d), d)
self.visit_func_def(d.func, decorated=True)

def visit_if_stmt(self, s: IfStmt) -> None:
infer_reachability_of_if_statement(s, self.sem.options)
Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,27 @@ import m.a
[file m/a.py]
[out]

[case testCheckDecoratedFuncAsAnnotWithImportCycle]
import a
[file a.py]
from typing import TypeVar
import b

T = TypeVar('T')
def idf(x: T) -> T: return x

@idf
def Session() -> None: pass

[file b.py]
MYPY = False
if MYPY:
from a import Session

def f(self, session: Session) -> None: # E: Invalid type "a.Session"
pass
[builtins fixtures/bool.pyi]


-- Checks dealing with submodules and different kinds of imports
-- -------------------------------------------------------------
Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,25 @@ x = 1
[builtins fixtures/ops.pyi]
[out]

[case testSysPlatformInFunctionImport3]
from typing import Callable
import sys

def idf(x: Callable[[], None]) -> Callable[[], None]: return x

@idf
def foo() -> None:
if sys.platform == 'fictional':
import b as a
else:
import a
a.x
[file a.py]
x = 1
[builtins fixtures/ops.pyi]
[out]


[case testSysPlatformInMethodImport2]
import sys
class A:
Expand Down