Coverage for faststream / specification / asyncapi / v2_6_0 / schema / license.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 License as SpecLicense, 

10 LicenseDict, 

11) 

12 

13 

14class License(BaseModel): 

15 """A class to represent a license. 

16 

17 Attributes: 

18 name : name of the license 

19 url : URL of the license (optional) 

20 

21 Config: 

22 extra : allow additional attributes in the model (PYDANTIC_V2) 

23 """ 

24 

25 name: str 

26 # Use default values to be able build from dict 

27 url: AnyHttpUrl | 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, license: None) -> None: ... 

40 

41 @overload 

42 @classmethod 

43 def from_spec(cls, license: SpecLicense) -> Self: ... 

44 

45 @overload 

46 @classmethod 

47 def from_spec(cls, license: LicenseDict) -> Self: ... 

48 

49 @overload 

50 @classmethod 

51 def from_spec(cls, license: dict[str, Any]) -> dict[str, Any]: ... 

52 

53 @classmethod 

54 def from_spec( 

55 cls, 

56 license: SpecLicense | LicenseDict | dict[str, Any] | None, 

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

58 if license is None: 

59 return None 

60 

61 if isinstance(license, SpecLicense): 

62 return cls( 

63 name=license.name, 

64 url=license.url, 

65 ) 

66 

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

68 license_data, custom_data = filter_by_dict(LicenseDict, license) 

69 

70 if custom_data: 

71 return license 

72 

73 return cls(**license_data)