Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Test all coroutine patterns and refactor op call #2684

Merged
merged 6 commits into from
Oct 11, 2024

Conversation

tssweeney
Copy link
Collaborator

@tssweeney tssweeney commented Oct 11, 2024

I am working on another feature and needed to use the Op.call pattern. I realized that the typing for this was not exactly correct AND we did not use the same code as the wrapper logic.

So, this PR:

  1. Adds a bunch of tests to ensure we do the correct thing for coroutines
  2. Shares as much code as possible between call, async wrapper and wrapper
  3. Improves typing in various locations

@circle-job-mirror
Copy link

circle-job-mirror bot commented Oct 11, 2024

@@ -715,6 +715,8 @@ def finish_call(
*,
op: Optional[Op] = None,
) -> None:
ended_at = datetime.datetime.now(tz=datetime.timezone.utc)
call.ended_at = ended_at
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small bug i noticed - we don't update ended at!

@tssweeney tssweeney marked this pull request as ready for review October 11, 2024 22:43
@tssweeney tssweeney requested a review from a team as a code owner October 11, 2024 22:43
Comment on lines 563 to 565
res, _ = await do_call_async(
cast(Op, wrapper), *args, __should_raise=True, **kwargs
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am I correct in reading that you extracted this into do_call_async? Is anything else happening here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, no logical changes. But the key now is that def call can dispatch to each of these

Comment on lines 571 to 573
res, _ = do_call(
cast(Op, wrapper), *args, __should_raise=True, **kwargs
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question here

)


def do_call(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are private, so should be underscored

return res, call


async def do_call_async(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are private, so should be underscored

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack - done

@@ -141,7 +141,7 @@ def test_sync_method_call(client, weave_obj, py_obj):
weave_obj_method2 = weave_obj_method_ref.get()

with pytest.raises(errors.OpCallError):
res2, call2 = py_obj.amethod.call(1)
res2, call2 = py_obj.method.call(1)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was a small but in the test suite actually. This should have been testing method not amethod

from weave.trace.weave_client import Call


def test_non_async_non_coro(client):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just call this test_sync

assert res == 1


def test_non_async_non_coro_method(client):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_sync_method

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done



@pytest.mark.asyncio
async def test_non_async_coro(client):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_sync_in_coroutine

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opposite, but yeah - changed



@pytest.mark.asyncio
async def test_non_async_coro_method(client):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_sync_method_in_coroutine

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed everything to test_[Op_type]_[return_type]

@pytest.mark.asyncio
async def test_async_coro(client):
@weave.op()
async def async_coro():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when would you write code like this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not normally, but better to test

assert res == 1


def test_non_async_with_exception(client):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah basically anything that is marked "non_async" should just be called "sync"

res, call = test_inst.non_async_with_exception.call(test_inst)
assert isinstance(call, Call)
assert call.exception is not None
assert res == None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all None checks should be res is None

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, changed

@tssweeney tssweeney merged commit e8b1f40 into master Oct 11, 2024
98 checks passed
@tssweeney tssweeney deleted the tim/op_coroutines branch October 11, 2024 23:16
@github-actions github-actions bot locked and limited conversation to collaborators Oct 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants