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

1from typing import Any, cast, overload 

2 

3from pydantic import 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.asyncapi.v2_6_0.schema.docs import ExternalDocs 

9from faststream.specification.schema.extra import ( 

10 Tag as SpecTag, 

11 TagDict, 

12) 

13 

14 

15class Tag(BaseModel): 

16 """A class to represent a tag. 

17 

18 Attributes: 

19 name : name of the tag 

20 description : description of the tag (optional) 

21 externalDocs : external documentation for the tag (optional) 

22 """ 

23 

24 name: str 

25 # Use default values to be able build from dict 

26 description: str | None = None 

27 externalDocs: ExternalDocs | None = None 

28 

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

31 

32 else: 

33 

34 class Config: 

35 extra = "allow" 

36 

37 @overload 

38 @classmethod 

39 def from_spec(cls, tag: SpecTag) -> Self: ... 

40 

41 @overload 

42 @classmethod 

43 def from_spec(cls, tag: TagDict) -> Self: ... 

44 

45 @overload 

46 @classmethod 

47 def from_spec(cls, tag: dict[str, Any]) -> dict[str, Any]: ... 

48 

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 ) 

57 

58 tag = cast("dict[str, Any]", tag) 

59 tag_data, custom_data = filter_by_dict(TagDict, tag) 

60 

61 if custom_data: 

62 return tag 

63 

64 return cls( 

65 name=tag_data.get("name"), 

66 description=tag_data.get("description"), 

67 externalDocs=tag_data.get("external_docs"), 

68 )