Skip to content

Commit

Permalink
stubber: Also merge TypeAliases
Browse files Browse the repository at this point in the history
Signed-off-by: Jos Verlinde <Jos.Verlinde@microsoft.com>
  • Loading branch information
Josverl committed Oct 27, 2024
1 parent 9937044 commit eb6db3a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/stubber/codemod/visitors/typevars.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ def __init__(self, context: CodemodContext) -> None:
self.all_typevars: List[Union[libcst.Assign, libcst.AnnAssign]] = []

def visit_Assign(self, node: libcst.Assign) -> None:

"""
Find all TypeVar assignments in the module.
format: T = TypeVar("T", int, float, str, bytes, Tuple)
"""
# is this a TypeVar assignment?
# needs to be more robust

if isinstance(node.value, libcst.Call) and node.value.func.value == "TypeVar":
self.all_typevars.append(node)

# def visit_AnnAssign(self, node: libcst.AnnAssign) -> None:
# # Track this import statement for later analysis.
# self.all_typevars.append(node)
def visit_AnnAssign(self, node: libcst.AnnAssign) -> None:
""" "
Find all TypeAlias assignments in the module.
format: T: TypeAlias = str
"""
if isinstance(node.annotation.annotation, libcst.Name) and node.annotation.annotation.value == "TypeAlias":
self.all_typevars.append(node)


class AddTypeVarsVisitor(ContextAwareTransformer):
Expand Down
10 changes: 10 additions & 0 deletions tests/codemods/codemod_test_cases/typealias_add/before.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#fmt: off
"""
add typealias
"""


def const(): ...


def bar(): ...
21 changes: 21 additions & 0 deletions tests/codemods/codemod_test_cases/typealias_add/doc_stub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# fmt: off
"""
add typealias
"""


from typing import TypeAlias

UUID: TypeAlias = str
_Flag: TypeAlias = int
_Descriptor: TypeAlias = tuple["UUID", _Flag]
_Characteristic: TypeAlias = (tuple["UUID", _Flag] | tuple["UUID", _Flag, tuple[_Descriptor, ...]])
_Service: TypeAlias = tuple["UUID", tuple[_Characteristic, ...]]
x = 2

def bar(f:_Flag , d:_Descriptor ) -> _Service:
"""
Used to declare that the expression is a constant so that the compiler can
optimise it. The use of this function should be as follows::
"""
...
22 changes: 22 additions & 0 deletions tests/codemods/codemod_test_cases/typealias_add/expected.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# fmt: off
"""
add typealias
"""
from typing import TypeAlias

UUID: TypeAlias = str
_Flag: TypeAlias = int
_Descriptor: TypeAlias = tuple["UUID", _Flag]
_Characteristic: TypeAlias = (tuple["UUID", _Flag] | tuple["UUID", _Flag, tuple[_Descriptor, ...]])
_Service: TypeAlias = tuple["UUID", tuple[_Characteristic, ...]]


def const(): ...


def bar(f:_Flag , d:_Descriptor ) -> _Service:
"""
Used to declare that the expression is a constant so that the compiler can
optimise it. The use of this function should be as follows::
"""
...

0 comments on commit eb6db3a

Please sign in to comment.