Skip to content
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

Update the current module when a new file is parsed #99

Merged
merged 2 commits into from Apr 16, 2020

Conversation

chadrik
Copy link
Contributor

@chadrik chadrik commented Apr 13, 2020

FixAnnotateJson.current_module is currently a constant value set in __main__, but this causes bugs when pyannotate is run on a package directory, where the current module should be updated for each file that is parsed.

To fix this, I implemented set_filename, which is inherited from BaseFix, to update the current module each time the file changes.

To test, I removed the annotation from parse_type_comment and created a type_info.json file to provide the type data:

[
  {
    "func_name": "parse_type_comment", "line": 213, 
    "path": "/Users/chad/dev/pyannotate/pyannotate_tools/annotations/parse.py", 
    "samples": 0, 
    "signature": {
      "arg_types": ["str"], 
      "return_type": "Tuple[pyannotate_tools.annotations.parse:List[pyannotate_tools.annotations.parse.Argument], pyannotate_tools.annotations.parse.AbstractType]"
    }
  }
]

I saw two types of bugs with the current implementation.

  1. imports statements were added that imported types from the current module:
pyannotate --uses-signature pyannotate_tools pyannotate_runtime
Refactored pyannotate_tools/annotations/parse.py
--- pyannotate_tools/annotations/parse.py	(original)
+++ pyannotate_tools/annotations/parse.py	(refactored)
@@ -10,6 +10,9 @@
 import sys
 
 from typing import Any, List, Mapping, Set, Tuple
+from pyannotate_tools.annotations.parse import AbstractType
+from pyannotate_tools.annotations.parse import Argument
+from pyannotate_tools.annotations.parse import List
 try:
     from typing import Text
 except ImportError:
@@ -211,6 +214,7 @@
 
 
 def parse_type_comment(comment):
+    # type: (str) -> Tuple[List[Argument], AbstractType]
     """Parse a type comment of form '(arg1, ..., argN) -> ret'."""
     return Parser(comment).parse()
  1. If I passed two packages on the command line the second would be completely ignored.
pyannotate --uses-signature pyannotate_runtime pyannotate_tools
No files need to be modified.

FixAnnotateJson.current_module is currently a constant value, which causes bugs when pyannotate is run on a package directory.
Copy link
Contributor

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

Excellent! Would it be possible to add a unit test?

@chadrik
Copy link
Contributor Author

chadrik commented Apr 15, 2020

I looked into writing tests for this, but the test case base class from lib2to3 that’s in use here appears to be designed to test before-and-after code snippets. We would need something higher level that tested runs across multiple files. Having tests at that level would be great but it’s an order of magnitude more work, and I’m not sure I’ll have time for it. I’ll see if there is an existing test case from lib2to3 that we can use for this purpose, just to get a sense of the scope.

@chadrik
Copy link
Contributor Author

chadrik commented Apr 15, 2020

Alternately I can write a super simple test function that demonstrates that calling set_filename on an instance of the fixer class updates the current module. Is that what you had in mind?

@gvanrossum
Copy link
Contributor

Yes please, we can still write regular tests.

@chadrik
Copy link
Contributor Author

chadrik commented Apr 16, 2020

Ok, added a test that confirms that our overridden set_filename is called. That should give us a reasonable assurance that the module will be set.

@gvanrossum
Copy link
Contributor

Hm, it looks like I'm no longer on the team that can merge PRs on this repo. Hopefully @msullivan or @JukkaL can still do it?

@ilevkivskyi ilevkivskyi merged commit 2b06f54 into dropbox:master Apr 16, 2020
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.

None yet

3 participants