Skip to content

Commit

Permalink
Add tests verifying TypeAnnotationsVisitor current behaviors (#502)
Browse files Browse the repository at this point in the history
* Add tests to verify that LibCST handles string annotations.

This is an important property for certain use cases, so it
makes sense to verify it in tests so that we can safely
depend on it.

At present, the reason we want to be able to rely on this is:
- at the moment, imports added by infer can make pysa traces
  hard to understand, because the line numbers are off
- if we add the ability to use fully-qualified string annotations
  for the stubs from infer, then we can do so without adding
  any import lines and pyre will understand the types.

* ApplyTypeAnnotations: add unit test of how import statments are merged

Add a unit test illustrating how the codemod handles various cases
of import statments in the stub file.

Explicitly call out each of the unsupported patterns:
- bare imports (we probably should support this)
- relative imports (we probably should support this)
 star imports (we probably don't want to support this)

* Add .python-version to .gitignore

This will be helpful for anyone using pyenv (I accidentally committed
my python version file in a draft branch).
  • Loading branch information
stroxler authored Aug 6, 2021
1 parent c7f2f57 commit 3009ec9
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ build/
.coverage
.hypothesis/
.pyre_configuration
.python-version
111 changes: 95 additions & 16 deletions libcst/codemod/visitors/tests/test_apply_type_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,76 @@
class TestApplyAnnotationsVisitor(CodemodTest):
TRANSFORM: Type[Codemod] = ApplyTypeAnnotationsVisitor

@data_provider(
(
(
"""
from __future__ import annotations
from foo import Foo
from baz import Baz
""",
"""
from foo import Bar
import bar
""",
"""
from __future__ import annotations
from foo import Foo, Bar
import bar
from baz import Baz
""",
),
(
# Missing feature: ignore aliased imports
"""
from Foo import foo as bar
""",
"""
from Foo import bar
""",
"""
from Foo import bar
""",
),
(
# Missing feature: ignore bare imports
"""
import foo
""",
"""
""",
"""
""",
),
(
# Missing feature: ignore relative imports
"""
from .. import foo
""",
"""
""",
"""
""",
),
(
# Missing feature: ignore star imports
"""
from foo import *
""",
"""
""",
"""
""",
),
)
)
def test_merge_module_imports(self, stub: str, before: str, after: str) -> None:
context = CodemodContext()
ApplyTypeAnnotationsVisitor.store_stub_in_context(
context, parse_module(textwrap.dedent(stub.rstrip()))
)
self.assertCodemod(before, after, context_override=context)

@data_provider(
(
(
Expand Down Expand Up @@ -608,22 +678,6 @@ def foo() -> None:
pass
""",
),
# Sanity check that we don't fail when the stub has relative imports.
# We don't do anything with those imports, though.
(
"""
from .. import hello
def foo() -> typing.Sequence[int]: ...
""",
"""
def foo():
return []
""",
"""
def foo() -> typing.Sequence[int]:
return []
""",
),
(
"""
from typing import Dict
Expand Down Expand Up @@ -669,6 +723,31 @@ class A:
def foo(self, atticus, b: Optional[int] = None, c: bool = False): ...
""",
),
# Make sure we handle string annotations well
(
"""
def f(x: "typing.Union[int, str]") -> "typing.Union[int, str]": ...
class A:
def f(self: "A") -> "A": ...
""",
"""
def f(x):
return x
class A:
def f(self):
return self
""",
"""
def f(x: "typing.Union[int, str]") -> "typing.Union[int, str]":
return x
class A:
def f(self: "A") -> "A":
return self
""",
),
)
)
def test_annotate_functions(self, stub: str, before: str, after: str) -> None:
Expand Down

0 comments on commit 3009ec9

Please sign in to comment.