Skip to content

Async refactoring: reduce code duplication #665

@tobixen

Description

@tobixen

I do want a library that can be used both with sync and async users, with as little code duplication as possible, and without generated code. Perhaps it was silly, perhaps "async first, autogenerated sync code" is the best solution. Anyway, Claude claimed that the "Sans-IO" design pattern would work well. Problem is that the library absolutely wasn't made Sans-IO in the first place.

We should think throughly on how to make version 4.0 more "sans-io". It's probably worth a separate ticket on "long term sans-io plans".. Right now I'm mostly concerned with "make it possible to use the library in an async way" and "refactor to reduce code duplication", and I think this is the way to go:

  • All methods doing I/O should be identified. The type hints should be updated to indicate that those methods may return an async coroutine.
  • Methods that first prepares something, then fires off some API call, and then processes the results should be refactored - the common code should not be duplicated. I've dealt with this some places (i.e. load_by_multiget()) by first doing the preparation, then if self.is_async_client: return self._async_load_by_multiget(xxx) and finally self._post_load_by_multiget(xxx) for post-processing.

Is this refactoring sane? Any suggestions on how to do it more properly is welcome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions