-
Notifications
You must be signed in to change notification settings - Fork 26.3k
print a warning if a type annotation prefix is invalid according to mypy #20884
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
Conversation
| type_lines = list(filter(lambda line: type_comment in line[1], lines)) | ||
|
|
||
| if len(type_lines) == 0: | ||
| type_pattern = re.compile('#[\t ]*type[\t ]*:') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if we necessarily want to support type annotation prefixes that MyPy considers invalid. It might be better to just nudge a user in the right direction, so they fix their prefixes to work with mypy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This regex is a little confusing, won't it also match # type:?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it does, but had there been # type: we wouldn't have even run this regex due to this check : if len(type_lines) == 0:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's valid to have multiple type lines, so it's not enough to check that if there is one valid match. Can we instead filter all lines that include type, check for the exact match, and if there isn't an exact match throw if the regex matches?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this particular case, we would still get Number of type annotations (1) did not match the number of function parameters (2):. We could tweak that error message to advise our user to check their type annotation prefixes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
def foo(
a, # type: int
b # type: bad type annotation
)
# type: (...) -> int
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as there's at least one valid type annotation prefix, we should get an error.
I just double checked that this exact example also gives the same error.
@torch.jit.script
def add_two(a,# type: int
b # type: int
):
# type: (...) -> int
return a+b
torch/jit/annotations.py
Outdated
| type_pattern = re.compile('#[\t ]*type[\t ]*:') | ||
| wrong_type_lines = list(filter(lambda line: type_pattern.search(line[1]), lines)) | ||
| if len(wrong_type_lines) > 0: | ||
| print("WARNING: the annotation prefix in line " + str(wrong_type_lines[0][0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should either use the actual warnings module or be an error. I'm leaning towards it being an error since people should be fixing this if it comes up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's turn this into an error then.
torch/jit/annotations.py
Outdated
| wrong_type_lines = list(filter(lambda line: type_pattern.search(line[1]), lines)) | ||
| if len(wrong_type_lines) > 0: | ||
| print("WARNING: the annotation prefix in line " + str(wrong_type_lines[0][0]) | ||
| + " is invalid. MyPy requires the exact prefix: '# type:'") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also let's not refer to MyPy directly and make people think it's secretly running under the hood
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on the other hand, wouldn't we annoy the heck out of our user if we don't explain her why exactly we don't just support this syntax and raise an error instead? I don't mind removing the mypy comment if you think that's the best course of action here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea i would rather not reference MyPy directly, there are other python type checkers as well. It's a python standard not a mypy standard
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can refer to the PEP or something lol
suo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
accepting to unblock
|
@pytorchbot rebase this please |
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Krovatkin has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
…o krovatkin/parse_annotate
facebook-github-bot
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Krovatkin has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
@Krovatkin merged this pull request in cbf2a4f. |
This PR adds a check that prints a warning if a type annotation prefix isn't what mypy expects.