Skip to content

Commit

Permalink
made drop_method into drop, with support for classes
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche committed Sep 13, 2024
1 parent 54ee3be commit d44b829
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .cross_sync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ CrossSync provides a set of annotations to mark up async classes, to guide the g
- if add_mapping is included, the async and sync classes can be accessed using a shared CrossSync.X alias
- `@CrossSync.convert`
- marks async functions for conversion. Unmarked methods will be copied as-is
- `@CrossSync.drop_method`
- marks functions that should not be included in sync output
- `@CrossSync.drop`
- marks functions or classes that should not be included in sync output
- `@CrossSync.pytest`
- marks test functions. Test functions automatically have all async keywords stripped (i.e., rm_aio is unneeded)
- `CrossSync.add_mapping`
Expand Down
12 changes: 5 additions & 7 deletions .cross_sync/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,16 @@ def visit_ClassDef(self, node):
Called for each class in file. If class has a CrossSync decorator, it will be transformed
according to the decorator arguments. Otherwise, class is returned unchanged
"""
for decorator in node.decorator_list:
orig_decorators = node.decorator_list
for decorator in orig_decorators:
try:
handler = AstDecorator.get_for_node(decorator)
if isinstance(handler, ExportSync):
# transformation is handled in sync_ast_transform method of the decorator
after_export = handler.sync_ast_transform(node, globals())
return self.generic_visit(after_export)
# transformation is handled in sync_ast_transform method of the decorator
node = handler.sync_ast_transform(node, globals())
except ValueError:
# not cross_sync decorator
continue
# cross_sync decorator not found. Drop from sync version
return None
return self.generic_visit(node) if node else None

def visit_If(self, node):
"""
Expand Down
6 changes: 3 additions & 3 deletions google/cloud/bigtable/data/_sync/cross_sync/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,14 @@ def sync_ast_transform(self, wrapped_node, transformers_globals):
return super().sync_ast_transform(converted, transformers_globals)


class DropMethod(AstDecorator):
class Drop(AstDecorator):
"""
Method decorator to drop async methods from the sync output
Method decorator to drop methods or classes from the sync output
"""

def sync_ast_transform(self, wrapped_node, transformers_globals):
"""
Drop method from sync output
Drop from sync output
"""
return None

Expand Down
4 changes: 2 additions & 2 deletions google/cloud/bigtable/data/_sync/cross_sync/cross_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async def async_func(self, arg: int) -> int:
from ._decorators import (
ExportSync,
Convert,
DropMethod,
Drop,
Pytest,
PytestFixture,
)
Expand Down Expand Up @@ -99,7 +99,7 @@ class CrossSync(metaclass=MappingMeta):
# decorators
export_sync = ExportSync.decorator # decorate classes to convert
convert = Convert.decorator # decorate methods to convert from async to sync
drop_method = DropMethod.decorator # decorate methods to remove from sync version
drop = Drop.decorator # decorate methods to remove from sync version
pytest = Pytest.decorator # decorate test methods to run with pytest-asyncio
pytest_fixture = (
PytestFixture.decorator
Expand Down
18 changes: 16 additions & 2 deletions tests/system/cross_sync/test_cases/cross_sync_files.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ tests:
async with self.base.connection():
return await self.base.my_method()
@CrossSync.drop_method
@CrossSync.drop
async def async_only_method(self):
await self.async_operation()
Expand Down Expand Up @@ -120,6 +120,7 @@ tests:
__CROSS_SYNC_OUTPUT__ = "out.path"
@CrossSync.export_sync(sync_name="MyClass")
class MyAsyncClass:
@CrossSync.drop
class NestedAsyncClass:
async def nested_method(self, base: AsyncBase):
pass
Expand Down Expand Up @@ -263,14 +264,27 @@ tests:
def keep_method(self):
pass
@CrossSync.drop_method
@CrossSync.drop
async def async_only_method(self):
await self.async_operation()
transformers: [CrossSyncFileProcessor]
after: |
def keep_method(self):
pass
- description: "Drop class from sync version"
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
@CrossSync.drop
class DropMe:
pass
class Keeper:
pass
transformers: [CrossSyncFileProcessor]
after: |
class Keeper:
pass
- description: "Convert.pytest"
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/data/_sync/test_cross_sync_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from google.cloud.bigtable.data._sync.cross_sync._decorators import (
ExportSync,
Convert,
DropMethod,
Drop,
Pytest,
PytestFixture,
)
Expand Down Expand Up @@ -412,9 +412,9 @@ def test_sync_ast_transform_add_docstring_format(
assert result.body[0].value.value == expected


class TestDropMethodDecorator:
class TestDropDecorator:
def _get_class(self):
return DropMethod
return Drop

def test_decorator_functionality(self):
"""
Expand Down

0 comments on commit d44b829

Please sign in to comment.