Coverage for faststream / specification / asyncapi / v2_6_0 / schema / contact.py: 89%
19 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 AnyHttpUrl, BaseModel
4from typing_extensions import Self
6from faststream._internal._compat import PYDANTIC_V2, EmailStr
7from faststream._internal.utils.data import filter_by_dict
8from faststream.specification.schema.extra import (
9 Contact as SpecContact,
10 ContactDict,
11)
14class Contact(BaseModel):
15 """A class to represent a contact.
17 Attributes:
18 name : name of the contact (str)
19 url : URL of the contact (Optional[AnyHttpUrl])
20 email : email of the contact (Optional[EmailStr])
21 """
23 name: str
24 # Use default values to be able build from dict
25 url: AnyHttpUrl | None = None
26 email: EmailStr | None = None
28 if PYDANTIC_V2: 28 ↛ 33line 28 didn't jump to line 33 because the condition on line 28 was always true
29 model_config = {"extra": "allow"}
31 else:
33 class Config:
34 extra = "allow"
36 @overload
37 @classmethod
38 def from_spec(cls, contact: None) -> None: ...
40 @overload
41 @classmethod
42 def from_spec(cls, contact: SpecContact) -> Self: ...
44 @overload
45 @classmethod
46 def from_spec(cls, contact: ContactDict) -> Self: ...
48 @overload
49 @classmethod
50 def from_spec(cls, contact: dict[str, Any]) -> dict[str, Any]: ...
52 @classmethod
53 def from_spec(
54 cls,
55 contact: SpecContact | ContactDict | dict[str, Any] | None,
56 ) -> Self | dict[str, Any] | None:
57 if contact is None:
58 return None
60 if isinstance(contact, SpecContact):
61 return cls(
62 name=contact.name,
63 url=contact.url,
64 email=contact.email,
65 )
67 contact = cast("dict[str, Any]", contact)
68 contact_data, custom_data = filter_by_dict(ContactDict, contact)
70 if custom_data:
71 return contact
73 return cls(**contact_data)