Files
semantic-kernel/python/semantic_kernel/kernel_pydantic.py
T
Eduard van Valkenburg fbb26b5cf6 Python: further improvements to checks and docs in dev_setup (#6445)
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
Added a section on documentation to the DEV_SETUP
Added a check for the copyright row to pre-commit
Closes #5629

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
2024-05-31 13:06:41 +00:00

55 lines
1.9 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
from typing import Annotated, Any, ClassVar, TypeVar
from pydantic import BaseModel, ConfigDict, UrlConstraints
from pydantic.networks import Url
from pydantic_settings import BaseSettings, SettingsConfigDict
HttpsUrl = Annotated[Url, UrlConstraints(max_length=2083, allowed_schemes=["https"])]
class KernelBaseModel(BaseModel):
"""Base class for all pydantic models in the SK."""
model_config = ConfigDict(populate_by_name=True, arbitrary_types_allowed=True, validate_assignment=True)
T = TypeVar("T", bound="KernelBaseSettings")
class KernelBaseSettings(BaseSettings):
"""Base class for all settings classes in the SK.
A subclass creates it's fields and overrides the env_prefix class variable
with the prefix for the environment variables.
In the case where a value is specified for the same Settings field in multiple ways,
the selected value is determined as follows (in descending order of priority):
- Arguments passed to the Settings class initializer.
- Environment variables, e.g. my_prefix_special_function as described above.
- Variables loaded from a dotenv (.env) file.
- Variables loaded from the secrets directory.
- The default field values for the Settings model.
"""
env_prefix: ClassVar[str] = ""
env_file_path: str | None = None
env_file_encoding: str = "utf-8"
model_config = SettingsConfigDict(
extra="ignore",
case_sensitive=False,
)
@classmethod
def create(cls: type["T"], **data: Any) -> "T":
"""Update the model_config with the prefix."""
cls.model_config["env_prefix"] = cls.env_prefix
if data.get("env_file_path"):
cls.model_config["env_file"] = data["env_file_path"]
cls.model_config["env_file_encoding"] = data.get("env_file_encoding", "utf-8")
data = {k: v for k, v in data.items() if v is not None}
return cls(**data)