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

Fix pyre command for type inference provider #523

Merged
merged 8 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions libcst/metadata/tests/test_type_inference_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@


import json
import os
import subprocess
import sys
from pathlib import Path
from unittest import skipIf

import libcst as cst
from libcst import MetadataWrapper
Expand Down Expand Up @@ -35,24 +39,49 @@ def _test_simple_class_helper(test: UnitTest, wrapper: MetadataWrapper) -> None:
test.assertEqual(types[value], "int")

# self
test.assertEqual(
types[self_number_attr.value], "libcst.tests.pyre.simple_class.Item"
)
test.assertEqual(types[self_number_attr.value], "simple_class.Item")
collector_assign = cst.ensure_type(
cst.ensure_type(m.body[3], cst.SimpleStatementLine).body[0], cst.Assign
)
collector = collector_assign.targets[0].target
test.assertEqual(types[collector], "libcst.tests.pyre.simple_class.ItemCollector")
test.assertEqual(types[collector], "simple_class.ItemCollector")
items_assign = cst.ensure_type(
cst.ensure_type(m.body[4], cst.SimpleStatementLine).body[0], cst.AnnAssign
)
items = items_assign.target
test.assertEqual(
types[items], "typing.Sequence[libcst.tests.pyre.simple_class.Item]"
)
test.assertEqual(types[items], "typing.Sequence[simple_class.Item]")


@skipIf(
sys.version_info < (3, 7), "TypeInferenceProvider doesn't support 3.6 and below"
)
@skipIf(sys.platform == "win32", "TypeInferenceProvider doesn't support windows")
class TypeInferenceProviderTest(UnitTest):
@classmethod
def setUpClass(cls):
os.chdir(TEST_SUITE_PATH)
try:
subprocess.run(["pyre", "-n", "start", "--no-watchman"])
except subprocess.TimeoutExpired as exc:
raise exc

@classmethod
def tearDownClass(cls):
try:
subprocess.run(["pyre", "-n", "stop"], cwd=TEST_SUITE_PATH)
except subprocess.TimeoutExpired as exc:
raise exc

@data_provider(
((TEST_SUITE_PATH / "simple_class.py", TEST_SUITE_PATH / "simple_class.json"),)
)
def test_gen_cache(self, source_path: Path, data_path: Path) -> None:
cache = TypeInferenceProvider.gen_cache(
root_path=source_path.parent, paths=[source_path.name], timeout=None
)
data: PyreData = json.loads(data_path.read_text())
self.assertEqual(cache[source_path.name], data)

@data_provider(
((TEST_SUITE_PATH / "simple_class.py", TEST_SUITE_PATH / "simple_class.json"),)
)
Expand Down
2 changes: 1 addition & 1 deletion libcst/metadata/type_inference_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def gen_cache(
root_path: Path, paths: List[str], timeout: Optional[int]
) -> Mapping[str, object]:
params = ",".join(f"path='{root_path / path}'" for path in paths)
cmd_args = ["pyre", "--noninteractive", "query", f'"types({params})"']
cmd_args = ["pyre", "--noninteractive", "query", f"types({params})"]
try:
stdout, stderr, return_code = run_command(cmd_args, timeout=timeout)
except subprocess.TimeoutExpired as exc:
Expand Down
7 changes: 7 additions & 0 deletions libcst/tests/pyre/.pyre_configuration
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"source_directories": [
"."
],
"search_path": [],
"workers": 1
}
46 changes: 23 additions & 23 deletions libcst/tests/pyre/simple_class.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"column": 10
}
},
"annotation": "typing.Type[libcst.tests.pyre.simple_class.Item]"
"annotation": "typing.Type[simple_class.Item]"
},
{
"location": {
Expand All @@ -37,7 +37,7 @@
"column": 16
}
},
"annotation": "typing.Callable(libcst.tests.pyre.simple_class.Item.__init__)[[Named(self, libcst.tests.pyre.simple_class.Item), Named(n, int)], None]"
"annotation": "typing.Callable(simple_class.Item.__init__)[[Named(self, simple_class.Item), Named(n, int)], None]"
},
{
"location": {
Expand All @@ -50,7 +50,7 @@
"column": 21
}
},
"annotation": "libcst.tests.pyre.simple_class.Item"
"annotation": "simple_class.Item"
},
{
"location": {
Expand Down Expand Up @@ -102,7 +102,7 @@
"column": 12
}
},
"annotation": "libcst.tests.pyre.simple_class.Item"
"annotation": "simple_class.Item"
},
{
"location": {
Expand Down Expand Up @@ -154,7 +154,7 @@
"column": 19
}
},
"annotation": "typing.Type[libcst.tests.pyre.simple_class.ItemCollector]"
"annotation": "typing.Type[simple_class.ItemCollector]"
},
{
"location": {
Expand All @@ -167,7 +167,7 @@
"column": 17
}
},
"annotation": "typing.Callable(libcst.tests.pyre.simple_class.ItemCollector.get_items)[[Named(self, libcst.tests.pyre.simple_class.ItemCollector), Named(n, int)], typing.Sequence[libcst.tests.pyre.simple_class.Item]]"
"annotation": "typing.Callable(simple_class.ItemCollector.get_items)[[Named(self, simple_class.ItemCollector), Named(n, int)], typing.Sequence[simple_class.Item]]"
},
{
"location": {
Expand All @@ -180,7 +180,7 @@
"column": 22
}
},
"annotation": "libcst.tests.pyre.simple_class.ItemCollector"
"annotation": "simple_class.ItemCollector"
},
{
"location": {
Expand Down Expand Up @@ -232,7 +232,7 @@
"column": 49
}
},
"annotation": "typing.Type[typing.Sequence[libcst.tests.pyre.simple_class.Item]]"
"annotation": "typing.Type[typing.Sequence[simple_class.Item]]"
},
{
"location": {
Expand All @@ -245,7 +245,7 @@
"column": 48
}
},
"annotation": "typing.Type[libcst.tests.pyre.simple_class.Item]"
"annotation": "typing.Type[simple_class.Item]"
},
{
"location": {
Expand All @@ -258,7 +258,7 @@
"column": 42
}
},
"annotation": "typing.List[libcst.tests.pyre.simple_class.Item]"
"annotation": "typing.List[simple_class.Item]"
},
{
"location": {
Expand All @@ -271,7 +271,7 @@
"column": 20
}
},
"annotation": "typing.Type[libcst.tests.pyre.simple_class.Item]"
"annotation": "typing.Type[simple_class.Item]"
},
{
"location": {
Expand All @@ -284,7 +284,7 @@
"column": 23
}
},
"annotation": "libcst.tests.pyre.simple_class.Item"
"annotation": "simple_class.Item"
},
{
"location": {
Expand Down Expand Up @@ -349,7 +349,7 @@
"column": 9
}
},
"annotation": "libcst.tests.pyre.simple_class.ItemCollector"
"annotation": "simple_class.ItemCollector"
},
{
"location": {
Expand All @@ -362,7 +362,7 @@
"column": 25
}
},
"annotation": "typing.Type[libcst.tests.pyre.simple_class.ItemCollector]"
"annotation": "typing.Type[simple_class.ItemCollector]"
},
{
"location": {
Expand All @@ -375,7 +375,7 @@
"column": 27
}
},
"annotation": "libcst.tests.pyre.simple_class.ItemCollector"
"annotation": "simple_class.ItemCollector"
},
{
"location": {
Expand All @@ -388,7 +388,7 @@
"column": 5
}
},
"annotation": "typing.Sequence[libcst.tests.pyre.simple_class.Item]"
"annotation": "typing.Sequence[simple_class.Item]"
},
{
"location": {
Expand All @@ -401,7 +401,7 @@
"column": 21
}
},
"annotation": "typing.Type[typing.Sequence[libcst.tests.pyre.simple_class.Item]]"
"annotation": "typing.Type[typing.Sequence[simple_class.Item]]"
},
{
"location": {
Expand All @@ -414,7 +414,7 @@
"column": 33
}
},
"annotation": "libcst.tests.pyre.simple_class.ItemCollector"
"annotation": "simple_class.ItemCollector"
},
{
"location": {
Expand All @@ -427,7 +427,7 @@
"column": 43
}
},
"annotation": "BoundMethod[typing.Callable(libcst.tests.pyre.simple_class.ItemCollector.get_items)[[Named(self, libcst.tests.pyre.simple_class.ItemCollector), Named(n, int)], typing.Sequence[libcst.tests.pyre.simple_class.Item]], libcst.tests.pyre.simple_class.ItemCollector]"
"annotation": "BoundMethod[typing.Callable(simple_class.ItemCollector.get_items)[[Named(self, simple_class.ItemCollector), Named(n, int)], typing.Sequence[simple_class.Item]], simple_class.ItemCollector]"
},
{
"location": {
Expand All @@ -440,7 +440,7 @@
"column": 46
}
},
"annotation": "typing.Sequence[libcst.tests.pyre.simple_class.Item]"
"annotation": "typing.Sequence[simple_class.Item]"
},
{
"location": {
Expand All @@ -466,7 +466,7 @@
"column": 8
}
},
"annotation": "libcst.tests.pyre.simple_class.Item"
"annotation": "simple_class.Item"
},
{
"location": {
Expand All @@ -479,7 +479,7 @@
"column": 17
}
},
"annotation": "typing.Sequence[libcst.tests.pyre.simple_class.Item]"
"annotation": "typing.Sequence[simple_class.Item]"
},
{
"location": {
Expand All @@ -492,7 +492,7 @@
"column": 8
}
},
"annotation": "libcst.tests.pyre.simple_class.Item"
"annotation": "simple_class.Item"
},
{
"location": {
Expand Down
2 changes: 2 additions & 0 deletions libcst/tests/test_pyre_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


import json
import os
from pathlib import Path
from typing import Dict, List, Mapping, Optional, Tuple, Union

Expand Down Expand Up @@ -122,6 +123,7 @@ def test_type_availability(self, source_path: Path, data_path: Path) -> None:
stdout: str
stderr: str
return_code: int
os.chdir(TEST_SUITE_PATH)
stdout, stderr, return_code = run_command(["pyre", "start", "--no-watchman"])
if return_code != 0:
print(stdout)
Expand Down