Coverage for faststream / specification / asyncapi / v2_6_0 / schema / tag.py: 87%
17 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-08 01:48 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-08 01:48 +0000
1from typing import Any, cast, overload
3from pydantic import BaseModel
4from typing_extensions import Self
6from faststream._internal._compat import PYDANTIC_V2
7from faststream._internal.utils.data import filter_by_dict
8from faststream.specification.asyncapi.v2_6_0.schema.docs import ExternalDocs
9from faststream.specification.schema.extra import (
10 Tag as SpecTag,
11 TagDict,
12)
15class Tag(BaseModel):
16 """A class to represent a tag.
18 Attributes:
19 name : name of the tag
20 description : description of the tag (optional)
21 externalDocs : external documentation for the tag (optional)
22 """
24 name: str
25 # Use default values to be able build from dict
26 description: str | None = None
27 externalDocs: ExternalDocs | None = None
29 if PYDANTIC_V2: 29 ↛ 34line 29 didn't jump to line 34 because the condition on line 29 was always true
30 model_config = {"extra": "allow"}
32 else:
34 class Config:
35 extra = "allow"
37 @overload
38 @classmethod
39 def from_spec(cls, tag: SpecTag) -> Self: ...
41 @overload
42 @classmethod
43 def from_spec(cls, tag: TagDict) -> Self: ...
45 @overload
46 @classmethod
47 def from_spec(cls, tag: dict[str, Any]) -> dict[str, Any]: ...
49 @classmethod
50 def from_spec(cls, tag: SpecTag | TagDict | dict[str, Any]) -> Self | dict[str, Any]:
51 if isinstance(tag, SpecTag):
52 return cls(
53 name=tag.name,
54 description=tag.description,
55 externalDocs=ExternalDocs.from_spec(tag.external_docs),
56 )
58 tag = cast("dict[str, Any]", tag)
59 tag_data, custom_data = filter_by_dict(TagDict, tag)
61 if custom_data:
62 return tag
64 return cls(
65 name=tag_data.get("name"),
66 description=tag_data.get("description"),
67 externalDocs=tag_data.get("external_docs"),
68 )