Skip to content

Commit

Permalink
Update the current module when a new file is parsed (#99)
Browse files Browse the repository at this point in the history
* Update the current module when a new file is parsed

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

* Add a test
  • Loading branch information
chadrik committed Apr 16, 2020
1 parent ae8802c commit 2b06f54
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pyannotate_tools/fixes/fix_annotate_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ def patch_imports(self, types, node):
touch_import(mod, name, node)
self.needed_imports = None

def set_filename(self, filename):
super(FixAnnotateJson, self).set_filename(filename)
self._current_module = crawl_up(filename)[1]

def current_module(self):
return self._current_module

Expand Down Expand Up @@ -208,7 +212,7 @@ def max_line_drift_set(cls, max_drift):
@classmethod
def init_stub_json_from_data(cls, data, filename):
cls.stub_json = data
cls.top_dir, cls._current_module = crawl_up(os.path.abspath(filename))
cls.top_dir = crawl_up(os.path.abspath(filename))[0]

def init_stub_json(self):
with open(self.__class__.stub_json_file) as f:
Expand Down
11 changes: 11 additions & 0 deletions pyannotate_tools/fixes/tests/test_annotate_json_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import tempfile
import unittest
import sys
from mock import patch

from lib2to3.tests.test_fixers import FixerTestCase

Expand Down Expand Up @@ -632,3 +633,13 @@ async def foo(x: str) -> int:
return 42
"""
self.check(a, b)

@patch('pyannotate_tools.fixes.fix_annotate_json.FixAnnotateJson.set_filename')
def test_set_filename(self, mocked_set_filename):
self.filename = "/path/to/fileA.py"
self.unchanged("")
mocked_set_filename.assert_called_with("/path/to/fileA.py")

self.filename = "/path/to/fileB.py"
self.unchanged("")
mocked_set_filename.assert_called_with("/path/to/fileB.py")

0 comments on commit 2b06f54

Please sign in to comment.