Skip to content

type-safe metadata protocol #3786

@d-v-b

Description

@d-v-b

To make our to-json and from-json serialization type-safe, we need a class like this:

class Metadata(Generic[FromType, ToType]):

  @classmethod
  def from_json(cls, data: FromType) -> Self:
    ...

  def to_json(self) -> ToType:
    ...

This violates liskov because FromType gets narrower in subclasses. ToType and FromType effectively impose opposite variance requirements on Metadata here.

But it does work if we use generic protocols instead! So that's what we should do. I think it would be really nice to make all of our JSON serialization type-safe this way.

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