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

1from typing import Any, cast, overload 

2 

3from pydantic import AnyHttpUrl, BaseModel 

4from typing_extensions import Self 

5 

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) 

12 

13 

14class Contact(BaseModel): 

15 """A class to represent a contact. 

16 

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 """ 

22 

23 name: str 

24 # Use default values to be able build from dict 

25 url: AnyHttpUrl | None = None 

26 email: EmailStr | None = None 

27 

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"} 

30 

31 else: 

32 

33 class Config: 

34 extra = "allow" 

35 

36 @overload 

37 @classmethod 

38 def from_spec(cls, contact: None) -> None: ... 

39 

40 @overload 

41 @classmethod 

42 def from_spec(cls, contact: SpecContact) -> Self: ... 

43 

44 @overload 

45 @classmethod 

46 def from_spec(cls, contact: ContactDict) -> Self: ... 

47 

48 @overload 

49 @classmethod 

50 def from_spec(cls, contact: dict[str, Any]) -> dict[str, Any]: ... 

51 

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 

59 

60 if isinstance(contact, SpecContact): 

61 return cls( 

62 name=contact.name, 

63 url=contact.url, 

64 email=contact.email, 

65 ) 

66 

67 contact = cast("dict[str, Any]", contact) 

68 contact_data, custom_data = filter_by_dict(ContactDict, contact) 

69 

70 if custom_data: 

71 return contact 

72 

73 return cls(**contact_data)