Coverage for faststream / specification / asyncapi / v2_6_0 / schema / docs.py: 88%

18 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 

7from faststream._internal.utils.data import filter_by_dict 

8from faststream.specification.schema.extra import ( 

9 ExternalDocs as SpecDocs, 

10 ExternalDocsDict, 

11) 

12 

13 

14class ExternalDocs(BaseModel): 

15 """A class to represent external documentation. 

16 

17 Attributes: 

18 url : URL of the external documentation 

19 description : optional description of the external documentation 

20 """ 

21 

22 url: AnyHttpUrl 

23 # Use default values to be able build from dict 

24 description: str | None = None 

25 

26 if PYDANTIC_V2: 26 ↛ 31line 26 didn't jump to line 31 because the condition on line 26 was always true

27 model_config = {"extra": "allow"} 

28 

29 else: 

30 

31 class Config: 

32 extra = "allow" 

33 

34 @overload 

35 @classmethod 

36 def from_spec(cls, docs: None) -> None: ... 

37 

38 @overload 

39 @classmethod 

40 def from_spec(cls, docs: SpecDocs) -> Self: ... 

41 

42 @overload 

43 @classmethod 

44 def from_spec(cls, docs: ExternalDocsDict) -> Self: ... 

45 

46 @overload 

47 @classmethod 

48 def from_spec(cls, docs: dict[str, Any]) -> dict[str, Any]: ... 

49 

50 @classmethod 

51 def from_spec( 

52 cls, 

53 docs: SpecDocs | ExternalDocsDict | dict[str, Any] | None, 

54 ) -> Self | dict[str, Any] | None: 

55 if docs is None: 

56 return None 

57 

58 if isinstance(docs, SpecDocs): 

59 return cls(url=docs.url, description=docs.description) 

60 

61 docs = cast("dict[str, Any]", docs) 

62 docs_data, custom_data = filter_by_dict(ExternalDocsDict, docs) 

63 

64 if custom_data: 

65 return docs 

66 

67 return cls(**docs_data)