Skip to content

Commit

Permalink
Applying PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zaicruvoir1rominet committed Aug 12, 2024
1 parent cad0347 commit eb3f2a6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
6 changes: 3 additions & 3 deletions libcst/codemod/visitors/_add_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def add_needed_import(
"""

if module == "__future__" and obj is None:
raise ImportError("Cannot import __future__ directly!")
raise ValueError("Cannot import __future__ directly!")

Check warning on line 139 in libcst/codemod/visitors/_add_imports.py

View check run for this annotation

Codecov / codecov/patch

libcst/codemod/visitors/_add_imports.py#L139

Added line #L139 was not covered by tests
imports = AddImportsVisitor._get_imports_from_context(context)
imports.append(ImportItem(module, obj, asname, relative))
context.scratch[AddImportsVisitor.CONTEXT_KEY] = imports
Expand All @@ -157,9 +157,9 @@ def __init__(
# Verify that the imports are valid
for imp in imps:
if imp.module == "__future__" and imp.obj_name is None:
raise ImportError("Cannot import __future__ directly!")
raise ValueError("Cannot import __future__ directly!")

Check warning on line 160 in libcst/codemod/visitors/_add_imports.py

View check run for this annotation

Codecov / codecov/patch

libcst/codemod/visitors/_add_imports.py#L160

Added line #L160 was not covered by tests
if imp.module == "__future__" and imp.alias is not None:
raise ImportError("Cannot import __future__ objects with aliases!")
raise ValueError("Cannot import __future__ objects with aliases!")

Check warning on line 162 in libcst/codemod/visitors/_add_imports.py

View check run for this annotation

Codecov / codecov/patch

libcst/codemod/visitors/_add_imports.py#L162

Added line #L162 was not covered by tests

# Resolve relative imports if we have a module name
imps = [imp.resolve_relative(self.context.full_package_name) for imp in imps]
Expand Down
8 changes: 3 additions & 5 deletions libcst/codemod/visitors/_remove_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _remove_imports_from_importfrom_stmt(
self.context.full_package_name, import_node
)
if module_name is None:
raise ImportError("Cannot look up absolute module from relative import!")
raise ValueError("Cannot look up absolute module from relative import!")

Check warning on line 49 in libcst/codemod/visitors/_remove_imports.py

View check run for this annotation

Codecov / codecov/patch

libcst/codemod/visitors/_remove_imports.py#L49

Added line #L49 was not covered by tests

# We know any local names will refer to this as an alias if
# there is one, and as the original name if there is not one
Expand All @@ -73,7 +73,7 @@ def _visit_name_attr_alike(self, node: Union[cst.Name, cst.Attribute]) -> None:
# Look up the scope for this node, remove the import that caused it to exist.
metadata_wrapper = self.context.wrapper
if metadata_wrapper is None:
raise ImportError(
raise ValueError(

Check warning on line 76 in libcst/codemod/visitors/_remove_imports.py

View check run for this annotation

Codecov / codecov/patch

libcst/codemod/visitors/_remove_imports.py#L76

Added line #L76 was not covered by tests
"Cannot look up import, metadata is not computed for node!"
)
scope_provider = metadata_wrapper.resolve(ScopeProvider)
Expand Down Expand Up @@ -258,9 +258,7 @@ def remove_unused_import_by_node(
context.full_package_name, node
)
if module_name is None:
raise ImportError(
"Cannot look up absolute module from relative import!"
)
raise ValueError("Cannot look up absolute module from relative import!")

Check warning on line 261 in libcst/codemod/visitors/_remove_imports.py

View check run for this annotation

Codecov / codecov/patch

libcst/codemod/visitors/_remove_imports.py#L261

Added line #L261 was not covered by tests
for import_alias in names:
RemoveImportsVisitor.remove_unused_import(
context,
Expand Down
19 changes: 14 additions & 5 deletions libcst/metadata/type_inference_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
from libcst.metadata.position_provider import PositionProvider


class TypeInferenceError(Exception):
"""An attempt to access inferred type annotation
(through Pyre Query API) failed."""


class Position(TypedDict):
line: int
column: int
Expand Down Expand Up @@ -61,14 +66,18 @@ def gen_cache(
params = ",".join(f"path='{root_path / path}'" for path in paths)
cmd_args = ["pyre", "--noninteractive", "query", f"types({params})"]

result = subprocess.run(cmd_args, capture_output=True, timeout=timeout)
result.check_returncode()
stdout, stderr = result.stdout.decode(), result.stderr.decode()
result = subprocess.run(
cmd_args, capture_output=True, timeout=timeout, text=True
)

try:
resp = json.loads(stdout)["response"]
result.check_returncode()
resp = json.loads(result.stdout)["response"]
except Exception as e:
raise Exception(f"{e}\n\nstderr:\n {stderr}\nstdout:\n {stdout}") from e
raise TypeInferenceError(

Check warning on line 77 in libcst/metadata/type_inference_provider.py

View check run for this annotation

Codecov / codecov/patch

libcst/metadata/type_inference_provider.py#L77

Added line #L77 was not covered by tests
f"{e}\n\nstderr:\n {result.stderr}\nstdout:\n {result.stdout}"
) from e

return {path: _process_pyre_data(data) for path, data in zip(paths, resp)}

def __init__(self, cache: PyreData) -> None:
Expand Down

0 comments on commit eb3f2a6

Please sign in to comment.