Skip to content

Commit

Permalink
Adds in_checked_function() to semanal similar to the one in checker (
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored Sep 22, 2021
1 parent 2f2c377 commit b7d8724
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
3 changes: 3 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3506,6 +3506,9 @@ def visit_lambda_expr(self, e: LambdaExpr) -> Type:
# Type check everything in the body except for the final return
# statement (it can contain tuple unpacking before return).
with self.chk.scope.push_function(e):
# Lambdas can have more than one element in body,
# when we add "fictional" AssigmentStatement nodes, like in:
# `lambda (a, b): a`
for stmt in e.body.body[:-1]:
stmt.accept(self.chk)
# Only type check the return expression, not the return statement.
Expand Down
21 changes: 14 additions & 7 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4953,17 +4953,26 @@ def is_local_name(self, name: str) -> bool:
"""Does name look like reference to a definition in the current module?"""
return self.is_defined_in_current_module(name) or '.' not in name

def in_checked_function(self) -> bool:
"""Should we type-check the current function?
- Yes if --check-untyped-defs is set.
- Yes outside functions.
- Yes in annotated functions.
- No otherwise.
"""
return (self.options.check_untyped_defs
or not self.function_stack
or not self.function_stack[-1].is_dynamic())

def fail(self,
msg: str,
ctx: Context,
serious: bool = False,
*,
code: Optional[ErrorCode] = None,
blocker: bool = False) -> None:
if (not serious and
not self.options.check_untyped_defs and
self.function_stack and
self.function_stack[-1].is_dynamic()):
if not serious and not self.in_checked_function():
return
# In case it's a bug and we don't really have context
assert ctx is not None, msg
Expand All @@ -4973,9 +4982,7 @@ def fail_blocker(self, msg: str, ctx: Context) -> None:
self.fail(msg, ctx, blocker=True)

def note(self, msg: str, ctx: Context, code: Optional[ErrorCode] = None) -> None:
if (not self.options.check_untyped_defs and
self.function_stack and
self.function_stack[-1].is_dynamic()):
if not self.in_checked_function():
return
self.errors.report(ctx.get_line(), ctx.get_column(), msg, severity='note', code=code)

Expand Down

0 comments on commit b7d8724

Please sign in to comment.