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

[Question] Is there an API to annotate the ast #4713

Closed
sk- opened this issue Mar 9, 2018 · 7 comments
Closed

[Question] Is there an API to annotate the ast #4713

sk- opened this issue Mar 9, 2018 · 7 comments

Comments

@sk-
Copy link

sk- commented Mar 9, 2018

Is there an api to annotate the AST, I'm interested in knowing the type of all subexpressions.

For example in the code:

min(3, 4)
'foo' * 2
def f(a: int, min: Callable[[int, int], int]) -> int:
  return min(a, 314)

I would like to know that

min -> __builtin__.min
3 -> int
4 -> int
min(3, 4) -> int
'foo' -> str
2 -> int
'foo' * 2 -> str

# In function f
a -> int
314 int
min -> Callable[[int, int], int] not __builtin__.min
min(a, 314) -> int

and ideally the expression should have line number and offset information to match it with the AST nodes.

@gvanrossum
Copy link
Member

Such a feature currently does not exist. But you can insert reveal_type(x) calls in your code and mypy will print the type of that specific expression.

@emmatyping
Copy link
Collaborator

Do we want to make this issue a feature request? There was an attempt before to do something similar in #2444, and I think it would be a nice thing to do. (Also I don't think there is another relevant issue open).

@JukkaL
Copy link
Collaborator

JukkaL commented Apr 5, 2018

Something like this would potentially be useful, but we can't make any progress with this until we have an understanding of the use cases where this could be helpful. The specifics of use cases could have a major impact on implementation details (and complexity of implementation).

Here are some potential use cases I can think of (some of them are probably unrealistic, but including them here for completeness):

  • Editor integration
  • Code browsing tools
  • Static analysis tools
  • Compilers/transpilers
  • Runtime type checkers

@sk-
Copy link
Author

sk- commented Apr 5, 2018

The other important use case is refactorings.

Also note that reveal_type cannot be used everywhere, for example it cannot be used inside a comprehension.

And that in unannotated functions reveal_type just returns Any (with the message note: 'reveal_type' always outputs 'Any' in unchecked functions).

@kamahen
Copy link
Contributor

kamahen commented Apr 5, 2018 via email

@JukkaL
Copy link
Collaborator

JukkaL commented Apr 6, 2018

And that in unannotated functions reveal_type just returns Any (with the message note: 'reveal_type' always outputs 'Any' in unchecked functions).

Mypy doesn't record inferred types in unannotated functions so this feature would not help with this.

@gvanrossum
Copy link
Member

Let's close this issue, it's formulated as a question, and as a feature request it's too general. For a specific use case please file a fresh issue describing the use case in more detail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants