Skip to content

Use Protocol interfaces and static duck typing design pattern #1547

@francbartoli

Description

@francbartoli

Is your feature request related to a problem? Please describe.
In order to improve flexibility and composability over inheritance it would be beneficial to improve the design using static duck typing PEP 544

Describe the solution you'd like
This is a very basic example how we can use it to adopt standard dataclasses as replacement of pydantic data models but at the same type leave the flexibility to use them for data validation in a fancy way without touching the core:

from typing import Any, Protocol

from pydantic import RootModel
from pydantic.dataclasses import dataclass as p_dataclass
from dataclasses import dataclass

from pygeoapi.models.openapi import SupportedFormats


class OAPIFormat(Protocol):
    """Interface for OpenAPI Format"""

    root: SupportedFormats


class OAPIPydantic(RootModel):
    root: SupportedFormats = SupportedFormats.YAML.value


@dataclass
class OAPI(OAPIFormat):
    """OpenAPI definition"""

    root:SupportedFormats


@p_dataclass
class OAPI_PydanticDataclass(OAPIFormat):
    """OpenAPI definition"""
    root:SupportedFormats


def check_openapi_format(format: OAPIFormat):
    """Check OpenAPI Format"""
    print(format)

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requeststaleIssue marked stale by stale-bot

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions