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
« 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
7from faststream._internal.utils.data import filter_by_dict
8from faststream.specification.schema.extra import (
9 License as SpecLicense,
10 LicenseDict,
11)
14class License(BaseModel):
15 """A class to represent a license.
17 Attributes:
18 name : name of the license
19 url : URL of the license (optional)
21 Config:
22 extra : allow additional attributes in the model (PYDANTIC_V2)
23 """
25 name: str
26 # Use default values to be able build from dict
27 url: AnyHttpUrl | None = None
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"}
32 else:
34 class Config:
35 extra = "allow"
37 @overload
38 @classmethod
39 def from_spec(cls, license: None) -> None: ...
41 @overload
42 @classmethod
43 def from_spec(cls, license: SpecLicense) -> Self: ...
45 @overload
46 @classmethod
47 def from_spec(cls, license: LicenseDict) -> Self: ...
49 @overload
50 @classmethod
51 def from_spec(cls, license: dict[str, Any]) -> dict[str, Any]: ...
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
61 if isinstance(license, SpecLicense):
62 return cls(
63 name=license.name,
64 url=license.url,
65 )
67 license = cast("dict[str, Any]", license)
68 license_data, custom_data = filter_by_dict(LicenseDict, license)
70 if custom_data:
71 return license
73 return cls(**license_data)