Skip to content

Added parsing of ignore annotation for import statements. #503

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
wants to merge 4 commits into from

Conversation

spkersten
Copy link
Contributor

This implements @JukkaL's suggestion of adding # mypy: ignore annotations in #500.

In this PR, only ignore annotations on import statements are allowed, but I think it would be easily extendable to other statements as well. Every statement Node could have an annotations field that could be used during semantic analysis or type checking.

return None
try:
annotation, index = parse_annotation(tokens, 0)
except TypeParseError as e:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this refer to AnnotationParseError?

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 30, 2014

Cool, thanks for implementing this! A few notes:

  • I recommend adding a few test cases for invalid annotations and no-op annotations.
  • Test what happens if we refer to an ignored imported module in code. The module object should probably have type Any if the import was ignored. What do you think?

I'm going to consider this as an experimental feature, since this feature hasn't received much discussion yet. However, a feature like this is probably going to be almost essential.

@@ -33,7 +33,8 @@
from mypy.parsetype import (
parse_type, parse_types, parse_signature, TypeParseError
)

from mypy.annotations import Annotation, IgnoreAnnotation
from mypy.parseannotation import parse_annotation

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing empty line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to see that I'm not the only one who cares about theses things 😄

@spkersten
Copy link
Contributor Author

I'd forgotten about from x import y. Makes perfect sense to me to give y (and a in import a) the Any type in these cases. I'll add it.

@spkersten
Copy link
Contributor Author

Actually, it doesn't seem necessary to define those imported symbols with type Any (or it already happens). I did some quick checks and after from x import y # mypy: ignore, y can be used as if it has Any type (as far as I can see).

There still an open issue. Annotations after all other statements are ignored. That doesn't seem like a big deal to me since, but it might be confusing. Since this is indeed an experimental feature, I propose to leave that for what it is.

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 3, 2014

Yeah, we should definitely complain about annotations in unsupported contexts. We can address that later.

I'm going to play around with this a little and think this through before merging, but this is probably okay as long we clearly document that this is an experimental feature that hasn't been fully implemented and that may be changed in the future.

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 20, 2015

Let's wait and see if PEP 484 will pick a notation for this: python/typing#16

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 29, 2015

I did a more general implementation of this functionality (the magic comment is # type: ignore, from PEP 484). Now you can ignore errors on arbitrary lines. Thanks for the inspiration!

@JukkaL JukkaL closed this Mar 29, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants