-
Notifications
You must be signed in to change notification settings - Fork 283
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
Do not parse default values as types #1350
Conversation
@martindemello Could you take a look at this one? Based on the other usages of visit(), it looks like it has a return value, and I'm not sure if discarding that is okay or not. |
you're right, annotation visitor methods should return a value because of this usage: https://github.com/google/pytype/blob/main/pytype/pyi/parser.py#L315 |
pytype/pyi/parser.py
Outdated
# Visit only the annotation, not the default (which may be a | ||
# string but should not be interpreted as a type annotation). | ||
if node.annotation is not None: | ||
self.visit(node.annotation) |
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.
return self.visit ...
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.
Then we'd return a different type which seems wrong, right? I imagine I'll have to construct a new ast.arg with the visited annotation.
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.
hm, i don't think so. if you look at convert_node_annotations it's only ever modifying the annotations
field of the node, not the node itself.
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.
But then this code should be useless, because there cannot be an arg
node inside an annotation (unless there's a lambda in the annotation I suppose). Indeed, I just checked and the test I added passes even without this change. I'll have to go back and figure out the real source of the crash.
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.
ah yes :) i did wonder why the change was needed in the first place but i figured you had found some odd corner case in the ast that we weren't handling well. if you can share the file the parser crashes on i can take a look as well.
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.
Thanks! It's from python/typeshed#9501. There are a lot of crashes, but the one I posted below points to https://github.com/python/typeshed/blob/7f85a0cd903f51a63030620be8ac9e1b52a73d94/stdlib/email/header.pyi line 26 which has continuation_ws: str = " ",
in a default.
Looking at the stack trace again I do think I am approximately in the right place here, but not sure why the test doesn't reproduce the crash.
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.
okay, found the issue. here's the fix you need:
--- a/google3/third_party/py/pytype/pyi/parser.py
+++ b/google3/third_party/py/pytype/pyi/parser.py
@@ -129,6 +129,10 @@ def _attribute_to_name(node: ast3.Attrib
class AnnotationVisitor(visitor.BaseVisitor):
"""Converts typed_ast annotations to pytd."""
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._node_children[self._ast.arguments] = ["args", "kwonlyargs"]
+
def show(self, node):
it will prevent iterating over the defaults.
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.
the crash was specifically due to the empty/whitespace-only string, btw, it was trying to read it as an annotation and failing to find one. your test passed because it used "y"
as a default, which is a syntactically valid type annotation as well.
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.
Thanks, good catch! I think your proposed fix isn't quite right because it would exclude annotations on posonly args, *args, and **kwargs. I'll push a version that handles these.
Traceback I'm trying to fix for reference:
|
The CI failure seems spurious, some package failed to download. |
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.
thanks, looks good! we'll merge it internally and push back to github.
See python/typeshed#9501. Resolves #1350 PiperOrigin-RevId: 501717379
See python/typeshed#9501. Resolves #1350 PiperOrigin-RevId: 501717379
See python/typeshed#9501.