Skip to content

Commit

Permalink
fix nested self (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra authored Dec 31, 2021
1 parent 36d54a1 commit 7ec7d75
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Fix incorrect inference of `self` argument on
some nested methods (#382)
- Fix compatibility between `Callable` and `Annotated`
(#381)
- Fix inference for nested `async def` functions (#380)
Expand Down
5 changes: 4 additions & 1 deletion pyanalyze/name_check_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1553,8 +1553,11 @@ def visit_FunctionDef(
info = compute_function_info(
node,
self,
# If we set the current_class in the collecting phase,
# the self argument of nested methods with an unannotated
# first argument is incorrectly inferred.
enclosing_class=TypedValue(self.current_class)
if self.current_class is not None
if self.current_class is not None and self._is_checking()
else None,
is_nested_in_class=self.node_context.includes(ast.ClassDef),
potential_function=potential_function,
Expand Down
12 changes: 12 additions & 0 deletions pyanalyze/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ def inner():
inner.punare = 3
assert_is_value(inner.punare, KnownValue(3))

@assert_passes()
def test_nested_in_method(self):
class Capybara:
def method(self):
def nested(arg) -> int:
assert_is_value(arg, AnyValue(AnySource.unannotated))
# Make sure we don't think this is an access to Capybara.numerator
print(arg.numerator)
return 1

assert_is_value(nested(1), TypedValue(int))


class TestFunctionDefinitions(TestNameCheckVisitorBase):
@assert_passes()
Expand Down

0 comments on commit 7ec7d75

Please sign in to comment.