Skip to content
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

Fix bug with *args on __call__ #600

Merged
merged 1 commit into from
Mar 13, 2023
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
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

# Changelog

## Unreleased

- Fix bug where objects with a `__call__` method that takes `*args` instead
of `self` was not considered callable (#600)

## Version 0.9.0 (January 16, 2023)

Release highlights:
Expand Down
23 changes: 16 additions & 7 deletions pyanalyze/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -1892,21 +1892,30 @@ def bind_self(
if not params:
return None
kind = params[0].kind
if self_value is not None:
tv_map = get_tv_map(params[0].annotation, self_value, ctx)
if isinstance(tv_map, CanAssignError):
return None
else:
tv_map = {}
if kind is ParameterKind.ELLIPSIS:
if kind in (ParameterKind.ELLIPSIS, ParameterKind.VAR_POSITIONAL):
new_params = params
self_tuple_annotation = params[0].annotation
if (
isinstance(self_tuple_annotation, GenericValue)
and self_tuple_annotation.typ is tuple
):
self_annotation = self_tuple_annotation.args[0]
else:
self_annotation = AnyValue(AnySource.inference)
elif kind in (
ParameterKind.POSITIONAL_ONLY,
ParameterKind.POSITIONAL_OR_KEYWORD,
):
new_params = params[1:]
self_annotation = params[0].annotation
else:
return None
if self_value is not None:
tv_map = get_tv_map(self_annotation, self_value, ctx)
if isinstance(tv_map, CanAssignError):
return None
else:
tv_map = {}
if tv_map:
new_params = {
param.name: param.substitute_typevars(tv_map) for param in new_params
Expand Down
31 changes: 31 additions & 0 deletions pyanalyze/test_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,37 @@ def capybara(x):
assert_is_value(obj, TypedValue(WithCall))
assert_is_value(obj(x), AnyValue(AnySource.unannotated))

@assert_passes()
def test__call__annotated(self):
class WithCall(object):
def __call__(self, arg: int) -> int:
return arg * 2

def capybara(x: int):
obj = WithCall()
assert_is_value(obj, TypedValue(WithCall))
assert_is_value(obj(x), TypedValue(int))
obj("x") # E: incompatible_argument

@assert_passes()
def test__call__starargs(self):
class WithCall(object):
def __call__(*args) -> int: # E: method_first_arg
return 42

class WithWrongAnnotation(object):
def __call__(*args: int) -> int: # E: method_first_arg
return 42

def capybara(x: int):
obj = WithCall()
assert_is_value(obj, TypedValue(WithCall))
assert_is_value(obj(x), TypedValue(int))
assert_is_value(obj("x"), TypedValue(int))

obj2 = WithWrongAnnotation()
obj2(1) # E: not_callable

@assert_passes()
def test_unbound_method(self):
class Capybara(object):
Expand Down