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

refactor: reorganize message handling for better type safety and clarity #239

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

dsp-ant
Copy link
Member

@dsp-ant dsp-ant commented Mar 3, 2025

Summary

  • Reorganizes message handling in the codebase by introducing MessageFrame (previously ParsedMessage) and improving type definitions
  • Enhances code organization by moving stream type definitions to more appropriate locations

Test plan

  • Ensure all existing tests pass
  • Verify type checking completes without errors
  • Check that renamed components maintain backward compatibility

GitHub-Issue:#201

Move memory stream type definitions to models.py and use them throughout
the codebase for better type safety and maintainability.

GitHub-Issue:#201
Updates test files to work with the ParsedMessage stream type aliases
and fixes a line length issue in test_201_client_hangs_on_logging.py.

Github-Issue:#201
@dsp-ant dsp-ant force-pushed the davidsp/raw-request branch from 2e3388b to 0d651d5 Compare March 3, 2025 20:09
@dsp-ant dsp-ant requested review from jerome3o-anthropic, jspahrsummers and bhosmer-ant and removed request for jerome3o-anthropic March 4, 2025 10:44
Copy link

@bhosmer-ant bhosmer-ant left a comment

Choose a reason for hiding this comment

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

Couple minor observations inline but LGTM!

@@ -22,10 +22,10 @@
@pytest.mark.anyio
async def test_client_session_initialize():
client_to_server_send, client_to_server_receive = anyio.create_memory_object_stream[
JSONRPCMessage
MessageFrame[None]

Choose a reason for hiding this comment

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

Should it be MessageFrame[NoneType]?

RawT = TypeVar("RawT")


class MessageFrame(BaseModel, Generic[RawT]):

Choose a reason for hiding this comment

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

Don't know how fastidious we are about this in general, but fwiw the other classes here are docstringed up pretty thoroughly (and the point of raw in particular might be worth noting here)


class MessageFrame(BaseModel, Generic[RawT]):
root: JSONRPCMessage
raw: RawT | None = None

Choose a reason for hiding this comment

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

Looks like all the uses of MessageFrame pass raw=None explicitly - personally seems nice to require that but maybe there are ergo concerns I'm not thinking of? (Minor side benefit to removing the default would be avoiding the usual union-vs-sum confusion when RawT = T | None...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants