Skip to content

Make Abstract Classes with Typing for Serializers and Persisters #900

Open
@tcyrus

Description

@tcyrus

I was looking into making a custom serializer and I was having trouble figuring out typing.

It's probably a good idea to make abstract classes with clear typing to make implementation easier.

Here's what I came up with:

import os
from abc import ABC, abstractmethod

from vcr.request import Request


class VcrPySerializer(ABC):
    @abstractmethod
    def deserialize(
        self,
        cassette_string: str
    ) -> dict:
        pass

    @abstractmethod
    def serialize(
        self,
        cassette_dict: dict
    ) -> str:
        pass


class VcrPyPersister(ABC):
    @abstractmethod
    @classmethod
    def load_cassette(
        cls,
        cassette_path: str | bytes | os.PathLike,
        serializer: VcrPySerializer
    )-> tuple[list[Request], list[bytes]]:
        pass

    @abstractmethod
    @staticmethod
    def save_cassette(
        cassette_path: str | bytes | os.PathLike,
        cassette_dict: dict,
        serializer: VcrPySerializer
    ) -> None:
        pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions