-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Support multi-line # type: comments for functions #1102
Comments
Yes. I've actually been waiting for somebody to complain about this :-) |
But what should it look like?
|
What about only considering the next line to be type comment if it simply isn't complete? e.g. |
I'm not sure how to do that with Jukka's parser. (If it was Python's parser
we could use the strategy for recognizing unfinished input used by code.py
(based on codeop.py). But mypy's syntax for function annotations in
comments is not valid Python.
Also, this might mask subtle syntax errors (a line with an unclosed paren
followed by a line meant as a non-type comment).
I guess this should be a low priority feature. And we should carefully
consider how to do it so that other tools interested in supporting this
syntax can also parse it (e.g. PyCharm in 2.7 mode).
|
I always assumed that we'd keep track of open parens and square brackets to determine when the type annotation ends. Example:
Reporting parse errors correctly might be a little tricky. Mypy should perhaps explicitly mention in a parse error message whether the error is within an (assumed) type annotation. However, repeating Should we discuss this in https://github.com/ambv/typehinting as well? |
Yeah, we probably need to augment PEP 484 with the comment-based function
annotations anyways.
|
Another idea is to support optionally including argument names in comments:
This way it would be easier to map argument names to types, especially if there are a ton of arguments. The obvious tradeoff is that there would be more redundancy, but mypy could check that the argument names match, and we could have a tool that automatically inserts dummy annotations with the correct argument names. Another idea for specifying signatures of functions with many arguments:
This way argument name and type would closer to each other, a bit like in the Python 3 annotation syntax. The tradeoff is that there can only be a single argument per line. For functions with only a few arguments the original annotation syntax is fine and we'll want to preserve it as the common case option. |
I'd like to hear from the PyCharm developers -- @vlasovskikh do you have any preference? How easy would any of these be to add to PyCharm? |
Either form would be relatively easy to support in PyCharm. There are a couple of minor issues with the first named-arguments-in-comments form. Our code formatter can automatically reformat the second form (with |
The second form seems preferable to me. My understanding is that good style generally dictates having one arg per line once you need to wrap arguments anyway, so it's a pretty natural fit. |
OK, let's go with 2. |
I've filed python/typing#186 to track this as an addition to PEP 484. |
This is settled in the PEP. It's pretty important for our internal customers. Maybe move to 0.3.2? |
Agreed that this is important.
|
I think the main question for me here is: do we want to implement this in the current parser or wait for the new one? |
Is the new Python 2 parser going to be in 0.3.2? If not, we probably should On Tue, Mar 22, 2016 at 20:46 David Fisher [email protected] wrote:
|
I expect it'll be easy enough in the current parser actually, and it looks
like the new parser isn't going to be ready really soon (esp. for 2.7).
|
The new parser is not in the 0.3.2 milestone (and IMO this is correct).
|
PEP484 doesn't mention an explicit example of how to define return types for functions with no parameters. In Python 3, there would be no annotations at all. It mentions that you can use ellipsis instead of type hinting an argument list, but what if there are no arguments? Sorry if this is a nitpicky question. It would assume this is the valid way: def fun():
# type: (...) -> None
pass But when I first got going, I thought perhaps this was (mimicking what I would expect from Python 3): def fun():
# type: () -> None
pass Just some feedback from a first time user of the new syntax and trying to get things to work in PyCharm. Both of the above forms currently work in PyCharm 2016.1RC. |
Yes, it's `# type: () -> None`. What's unclear about that?
|
@gvanrossum For me, it was just because there wasn't an explicit example for the empty case, but there were two that showed how to use the I'd like to say I would be the only one to make this mistake, but having made it I'd suggest adding a brief example or note in the PEP. If not, though, not a big deal. Thanks for the clarification. |
It's looking like the new parser will be in 0.3.2 now. I'll implement this after it lands (see #1353). |
This adds support for the (Python 2) per-argument type comment syntax now that python/typed_ast#5 has landed. It only works when using `--fast-parser`; without that these comments will be ignore. The code works by converting Python 2.7 per-argument type comments to Python 3 annotations in the typed_ast's conversion module -- this allows them to be mixed with the # type: (...) -> None syntax. Fixes #1102.
Some linters get mad about long lines, even comments, so we would like to be able to break long
# type:
comments into multiple lines. This is especially desirable for function annotations if the function has lots of arguments, since you can't shorten those using type aliases. (And claiming that's just bad code doesn't do anyone any good -- I know, but sometimes I can't afford to fix it.)The text was updated successfully, but these errors were encountered: