Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Protocol interfaces and static duck typing design pattern #1547

Open
francbartoli opened this issue Feb 23, 2024 · 3 comments
Open

Use Protocol interfaces and static duck typing design pattern #1547

francbartoli opened this issue Feb 23, 2024 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@francbartoli
Copy link
Contributor

francbartoli commented Feb 23, 2024

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.

@francbartoli francbartoli added the enhancement New feature or request label Feb 23, 2024
@francbartoli francbartoli changed the title WIP: Use Protocol classes for Tile Provider implementations WIP: Use Protocol classes for Tile Provider interfaces Feb 23, 2024
@francbartoli francbartoli changed the title WIP: Use Protocol classes for Tile Provider interfaces Use Protocol interfaces and static duck typing design pattern Feb 29, 2024
Copy link

github-actions bot commented Jun 2, 2024

This Issue has been inactive for 90 days. As per RFC4, in order to manage maintenance burden, it will be automatically closed in 7 days.

@github-actions github-actions bot added the stale Issue marked stale by stale-bot label Jun 2, 2024
@francbartoli francbartoli self-assigned this Jun 3, 2024
@francbartoli francbartoli removed the stale Issue marked stale by stale-bot label Jun 3, 2024
@vvmruder
Copy link
Contributor

vvmruder commented Sep 2, 2024

I would be interested to see the move to plain dataclasses as well. Since they are part of python this should be more stable than using pydantic. Currently I'm facing the same problem as described here #1664

Is there a roadmap when this could happen? I could support working on that.

@francbartoli
Copy link
Contributor Author

@ricardogsilva can you please share the WIP that you developed during the sprint?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants