Skip to content

Commit

Permalink
renamed export into convert_class
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche committed Sep 13, 2024
1 parent d44b829 commit 61490e5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .cross_sync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ Additionally, CrossSync provides method implementations that work equivalently i

CrossSync provides a set of annotations to mark up async classes, to guide the generation of sync code.

- `@CrossSync.export_sync`
- marks classes for conversion. Unmarked classes will be droppd
- `@CrossSync.convert_sync`
- marks classes for conversion. Unmarked classes will be copied as-is
- 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
Expand Down
2 changes: 1 addition & 1 deletion .cross_sync/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import sys
# add cross_sync to path
sys.path.append("google/cloud/bigtable/data/_sync/cross_sync")
from _decorators import AstDecorator, ExportSync
from _decorators import AstDecorator, ConvertClass


class SymbolReplacer(ast.NodeTransformer):
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 @@ -182,9 +182,9 @@ def _convert_ast_to_py(cls, ast_node: ast.expr | None) -> Any:
raise ValueError(f"Unsupported type {type(ast_node)}")


class ExportSync(AstDecorator):
class ConvertClass(AstDecorator):
"""
Class decorator for marking async classes to be converted to sync classes
Class decorator for guiding generation of sync classes
Args:
sync_name: use a new name for the sync class
Expand Down Expand Up @@ -290,7 +290,7 @@ def sync_ast_transform(self, wrapped_node, transformers_globals):
return wrapped_node


class Convert(ExportSync):
class Convert(ConvertClass):
"""
Method decorator to mark async methods to be converted to sync methods
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 @@ -60,7 +60,7 @@ async def async_func(self, arg: int) -> int:
import threading
import time
from ._decorators import (
ExportSync,
ConvertClass,
Convert,
Drop,
Pytest,
Expand Down Expand Up @@ -97,7 +97,7 @@ class CrossSync(metaclass=MappingMeta):
Generator: TypeAlias = AsyncGenerator

# decorators
export_sync = ExportSync.decorator # decorate classes to convert
convert_class = ConvertClass.decorator # decorate classes to convert
convert = Convert.decorator # decorate methods to convert from async to sync
drop = Drop.decorator # decorate methods to remove from sync version
pytest = Pytest.decorator # decorate test methods to run with pytest-asyncio
Expand Down
74 changes: 51 additions & 23 deletions tests/system/cross_sync/test_cases/cross_sync_files.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ tests:
- name: CrossSyncFileProcessor
after: null

- description: "CrossSync.export_sync with default sync_name"
- description: "CrossSync.convert_class with default sync_name"
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
@CrossSync.export_sync
@CrossSync.convert_class
class MyClass:
async def my_method(self):
pass
Expand All @@ -25,10 +25,10 @@ tests:
async def my_method(self):
pass
- description: "CrossSync.export_sync with custom sync_name"
- description: "CrossSync.convert_class with custom sync_name"
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
@CrossSync.export_sync(sync_name="MyClass")
@CrossSync.convert_class(sync_name="MyClass")
class MyAsyncClass:
async def my_method(self):
pass
Expand All @@ -41,10 +41,10 @@ tests:
async def my_method(self):
pass
- description: "CrossSync.export_sync with replace_symbols"
- description: "CrossSync.convert_class with replace_symbols"
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
@CrossSync.export_sync(
@CrossSync.convert_class(
sync_name="MyClass",
replace_symbols={"AsyncBase": "SyncBase", "ParentA": "ParentB"}
)
Expand All @@ -60,10 +60,10 @@ tests:
def __init__(self, base: SyncBase):
self.base = base
- description: "CrossSync.export_sync with docstring formatting"
- description: "CrossSync.convert_class with docstring formatting"
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
@CrossSync.export_sync(
@CrossSync.convert_class(
sync_name="MyClass",
docstring_format_vars={"type": ("async", "sync")}
)
Expand All @@ -76,10 +76,10 @@ tests:
class MyClass:
"""This is a sync class."""
- description: "CrossSync.export_sync with multiple decorators and methods"
- description: "CrossSync.convert_class with multiple decorators and methods"
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
@CrossSync.export_sync(sync_name="MyClass")
@CrossSync.convert_class(sync_name="MyClass")
@some_other_decorator
class MyAsyncClass:
@CrossSync.convert
Expand Down Expand Up @@ -115,10 +115,10 @@ tests:
def fixture(self):
pass
- description: "CrossSync.export_sync with nested classes drop"
- description: "CrossSync.convert_class with nested classes drop"
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
@CrossSync.export_sync(sync_name="MyClass")
@CrossSync.convert_class(sync_name="MyClass")
class MyAsyncClass:
@CrossSync.drop
class NestedAsyncClass:
Expand All @@ -138,12 +138,12 @@ tests:
nested = self.NestedAsyncClass()
nested.nested_method()
- description: "CrossSync.export_sync with nested classes"
- description: "CrossSync.convert_class with nested classes explicit"
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
@CrossSync.export_sync(sync_name="MyClass", replace_symbols={"AsyncBase": "SyncBase"})
@CrossSync.convert_class(sync_name="MyClass", replace_symbols={"AsyncBase": "SyncBase"})
class MyAsyncClass:
@CrossSync.export_sync
@CrossSync.convert_class
class NestedClass:
async def nested_method(self, base: AsyncBase):
pass
Expand All @@ -166,10 +166,38 @@ tests:
nested = self.NestedAsyncClass()
nested.nested_method()
- description: "CrossSync.export_sync with add_mapping"
- description: "CrossSync.convert_class with nested classes implicit"
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
@CrossSync.export_sync(
@CrossSync.convert_class(sync_name="MyClass", replace_symbols={"AsyncBase": "SyncBase"})
class MyAsyncClass:
class NestedClass:
async def nested_method(self, base: AsyncBase):
pass
@CrossSync.convert
async def use_nested(self):
nested = self.NestedAsyncClass()
CrossSync.rm_aio(await nested.nested_method())
transformers:
- name: CrossSyncFileProcessor
after: |
class MyClass:
class NestedClass:
async def nested_method(self, base: SyncBase):
pass
def use_nested(self):
nested = self.NestedAsyncClass()
nested.nested_method()
- description: "CrossSync.convert_class with add_mapping"
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
@CrossSync.convert_class(
sync_name="MyClass",
add_mapping_for_name="MyClass"
)
Expand All @@ -186,10 +214,10 @@ tests:
async def my_method(self):
pass
- description: "CrossSync.export_sync with rm_aio"
- description: "CrossSync.convert_class with rm_aio"
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
@CrossSync.export_sync(rm_aio=True)
@CrossSync.convert_class(rm_aio=True)
class MyClass:
async def my_method(self):
async for item in self.items:
Expand All @@ -202,10 +230,10 @@ tests:
for item in self.items:
self.process(item)
- description: "CrossSync.export_sync with CrossSync calls"
- description: "CrossSync.convert_class with CrossSync calls"
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
@CrossSync.export_sync(sync_name="MyClass")
@CrossSync.convert_class(sync_name="MyClass")
class MyAsyncClass:
@CrossSync.convert
async def my_method(self):
Expand Down Expand Up @@ -365,7 +393,7 @@ tests:
- description: "Convert method with multiple stacked decorators in class"
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
@CrossSync.export_sync
@CrossSync.convert_class
class MyClass:
@CrossSync.convert(sync_name="sync_multi_decorated")
@CrossSync.pytest
Expand Down Expand Up @@ -427,7 +455,7 @@ tests:
before: |
__CROSS_SYNC_OUTPUT__ = "out.path"
CrossSync.sleep(1)
@CrossSync.export_sync
@CrossSync.convert_class
class MyClass:
event = CrossSync.Event()
def my_method(self):
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/data/_sync/test_cross_sync_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from unittest import mock
from google.cloud.bigtable.data._sync.cross_sync.cross_sync import CrossSync
from google.cloud.bigtable.data._sync.cross_sync._decorators import (
ExportSync,
ConvertClass,
Convert,
Drop,
Pytest,
Expand All @@ -37,9 +37,9 @@ def globals_mock():
return global_dict


class TestExportSyncDecorator:
class TestConvertClassDecorator:
def _get_class(self):
return ExportSync
return ConvertClass

def test_ctor_defaults(self):
"""
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_class_decorator_docstring_update(self, docstring, format_vars, expected
of the class being decorated
"""

@ExportSync.decorator(sync_name="s", docstring_format_vars=format_vars)
@ConvertClass.decorator(sync_name="s", docstring_format_vars=format_vars)
class Class:
__doc__ = docstring

Expand Down

0 comments on commit 61490e5

Please sign in to comment.