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

PY-42693 (wip): Infer completitions for lambda parameters in PyCharm #2737

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Christopher-Chianelli
Copy link

A partial fix for https://youtrack.jetbrains.com/issue/PY-42693/PyCharm-cannot-infer-type-for-lambda-parameters

Added a new TypeProvider that tries to determine the callable type of a lambda in Python. In particular:

  • If the lambda is part of an argument list that has a call expression with a known callee
  • Then it finds the position of the lambda inside the argument list, and resolves the callee. The type of the entire lambda expression is determined to be the same as the type of the Nth argument of the callee (where N is the lambda's position in the argument list).

Modified PyTypingTypeProvider to determine type of lambda named parameters. In particular:

  • If a NamedParameter is in a parameter list belonging to a lambda expression
  • Then get the type of the Nth parameter (where N is the index of the NamedParameter in the lambda expression)

With these changes, code completions work; i.e., given

from typing import Callable

class A:
    my_attribute: int

def function(a: A, fun: Callable[[A], int]):
    return fun(a)

a = A()
a.my_attribute = 10
function(a, lambda parameter: parameter.my_

it would suggest the completion "my_attribute", which has type "int".

However, code insights still suggests parameter is Any, and doesn't seem to check the return type of the lambda expression.

Needs tests.

(I am unfamiliar with the code base and unsure where to go from here)

Added a new TypeProvider that tries to determine the callable type of a
lambda in Python. In particular:

- If the lambda is part of an argument list that has a call expression
  with a known callee
- Then it finds the position of the lambda inside the argument list,
  and resolves the callee. The type of the entire lambda expression
  is determined to be the same as the type of the Nth argument of
  the callee (where N is the lambda's position in the argument list).

Modified PyTypingTypeProvider to determine type of lambda named parameters.
In particular:

- If a NamedParameter is in a parameter list belonging to a lambda
  expression
- Then get the type of the Nth parameter (where N is the index of
  the NamedParameter in the lambda expression)

With these changes, code completions work; i.e., given

```python
from typing import Callable

class A:
    my_attribute: int

def function(a: A, fun: Callable[[A], int]):
    return fun(a)

a = A()
a.my_attribute = 10
function(a, lambda parameter: parameter.my_
```

it would suggest the completion "my_attribute", which has type "int".

However, code insights still suggests parameter is `Any`, and doesn't
seem to check the return type of the lambda expression.

Needs tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants