Skip to content

Use typing.Annotated[...] for the type-hints #344

@MicaelJarniac

Description

@MicaelJarniac

Before:

from uplink import Consumer, get, Path, Query


class GitHub(Consumer):
    """A Python Client for the GitHub API."""

    @get("users/{user}/repos")
    def get_repos(self, user: Path, sort_by: Query("sort")):
       """Get user's public repositories."""

After:

from typing import Annotated
from uplink import Consumer, get, Path, Query


class GitHub(Consumer):
    """A Python Client for the GitHub API."""

    @get("users/{user}/repos")
    def get_repos(self, user: Annotated[str, Path], sort_by: Annotated[str, Query("sort")]):
       """Get user's public repositories."""

This is similar to how Pydantic deals with fields:
https://docs.pydantic.dev/latest/concepts/fields/#the-annotated-pattern

from typing import Annotated

from pydantic import BaseModel, Field, WithJsonSchema


class Model(BaseModel):
    name: Annotated[str, Field(strict=True), WithJsonSchema({'extra': 'data'})]

I don't mean for the current method to be completely replaced by this, only for this new method to also be supported.

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