Skip to content

Better message for invalid types like List(int) #4172

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

Closed
JukkaL opened this issue Oct 27, 2017 · 4 comments
Closed

Better message for invalid types like List(int) #4172

JukkaL opened this issue Oct 27, 2017 · 4 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 27, 2017

The error message "invalid type comment or annotation" isn't very helpful if a user writes List(int) instead of List[int] (or Optional(int)). This seems to be a common error.

Example:

from typing import List

def f():
    # type: () -> List(int)   # invalid type comment or annotation
    return [1]

A better message could be something like this:

program.py:3: error: Syntax error in type annotation
program.py:3:note: Suggestion: Use List[...] instead of List(...)
@paultgibbons
Copy link

paultgibbons commented Oct 27, 2017

Another example that's not immediately obvious is missing parens around input eg

def f(x):
    # type: List[int] -> Tuple[List[int], Optional[str]]
    ...

throws message error: syntax error in type comment,
but would be nicer if it was more like

error: syntax error in type comment
Suggestion: Expected: `(` but found `L` at column 13

or

error: syntax error in type comment
Suggestion: Wrap input in parens

or something else to highlight the issue

@gvanrossum
Copy link
Member

Here's one that took a while to figure out:

def update_state(conf,               # type: SourceConf
                 new_state,          # type: SourceState
                 tid,                # type: int
                 vid,                # type: int
                 update_ts=None,     # type: Optional[float],
                 ):         # type: (...) -> SourceConf

This gave error: Invalid tuple literal type on the def line.

spoiler alert

.

.

.

.

.

.

.

.

.

.

.

.

.

There's an extra comma after Optional[float].

@emmatyping
Copy link
Member

@gvanrossum Well, #3871 should help a bit in your case. I'd fix it but I'm currently occupied with my PEP.

@ilevkivskyi
Copy link
Member

It looks like #4323 fixed only part of the problems mentioned here, I am reopening this, just not to lose track.

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