modal-labs / synchronicity

Synchronicity lets you interoperate with asynchronous Python APIs.
Apache License 2.0
80 stars 3 forks source link

Adds support for transferring dataclass_transform decorators to type stubs #147

Closed freider closed 1 month ago

freider commented 1 month ago

Ensures that functions like this:

@typing{_extensions}.dataclass_transform(field_specifiers=(custom_field,))
def decorator():
    ...

maintain their dataclass_transform decorator in a generated type stub

freider commented 1 month ago

@prbot approve

freider commented 1 month ago

Ah, good question! We are in some other cases, but I don't think we are here, so it may have been a lucky coincidence that it's actually imported and works for me when I just tested this. Will add it to self.imports in a followup PR to make sure it's always imported if this code path is used

On Wed, Aug 14, 2024 at 3:27 PM Michael Waskom @.***> wrote:

@.**** commented on this pull request.

In synchronicity/type_stubs.py https://github.com/modal-labs/synchronicity/pull/147#discussion_r1716926776 :

@@ -781,6 +781,22 @@ def _get_function_source( body_indent: str, transform_signature=None, ) -> str:

  • maybe_decorators = ""
  • if hasattr(func, "__dataclass_transform__"):
  • dt_spec = func.__dataclass_transform__
  • if dt_spec["field_specifiers"]:
  • refs = ""
  • for field_spec_entity in dt_spec["field_specifiers"]:
  • if field_spec_entity.module == self.target_module:
  • ref = field_spec_entity.qualname
  • else:
  • self.imports.add(field_spec_entity.module)
  • ref = f"{field_spec_entity.module}.{field_spec_entity.qualname}"
  • refs += ref + ", "
  • args = f"field_specifiers=({refs})"
  • maybe_decorators = @.***_extensions.dataclass_transform({args})\n"

That makes sense — but are we guaranteed to have typing_extensions imported in the type stubs we emit?

— Reply to this email directly, view it on GitHub https://github.com/modal-labs/synchronicity/pull/147#discussion_r1716926776, or unsubscribe https://github.com/notifications/unsubscribe-auth/AASII2S6V52ZG2ND4CIJY6LZRNLKPAVCNFSM6AAAAABMQD7C4KVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDEMZYGE3DOMBVHA . You are receiving this because you modified the open/close state.Message ID: @.***>