From 155b56e3f8c08a3b440da1e16e1ea9bc5c4f4ba0 Mon Sep 17 00:00:00 2001 From: Yannik Tausch Date: Mon, 13 May 2024 16:40:29 +0200 Subject: [PATCH] make extra fields hint configurable don't warn for AzureRunnerSettings and CondaBuildConfig --- conda_smithy/data/conda-forge.json | 3 +++ conda_smithy/lint_recipe.py | 10 +++++++--- conda_smithy/schema.py | 22 ++++++++++++++++++++-- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/conda_smithy/data/conda-forge.json b/conda_smithy/data/conda-forge.json index 8c5f9064b..248416313 100644 --- a/conda_smithy/data/conda-forge.json +++ b/conda_smithy/data/conda-forge.json @@ -822,6 +822,7 @@ "type": "object" }, "build_platform": { + "additionalProperties": true, "properties": { "emscripten_wasm32": { "anyOf": [ @@ -1026,6 +1027,7 @@ "type": "object" }, "os_version": { + "additionalProperties": true, "properties": { "linux_32": { "anyOf": [ @@ -1167,6 +1169,7 @@ "type": "object" }, "provider": { + "additionalProperties": true, "properties": { "linux": { "anyOf": [ diff --git a/conda_smithy/lint_recipe.py b/conda_smithy/lint_recipe.py index 5d1baaa49..99162da1d 100644 --- a/conda_smithy/lint_recipe.py +++ b/conda_smithy/lint_recipe.py @@ -5,7 +5,7 @@ from pydantic import BaseModel -from conda_smithy.schema import ConfigModel +from conda_smithy.schema import ConfigModel, NoExtraFieldsHint str_type = str @@ -162,8 +162,12 @@ def _forge_yaml_hint_extra_fields(forge_yaml: dict) -> List[str]: hints = [] def _find_extra_fields(model: BaseModel, prefix=""): - for extra_field in (model.__pydantic_extra__ or {}).keys(): - hints.append(f"Unexpected key {prefix + extra_field}") + if not ( + isinstance(model, NoExtraFieldsHint) + and not model.HINT_EXTRA_FIELDS + ): + for extra_field in (model.__pydantic_extra__ or {}).keys(): + hints.append(f"Unexpected key {prefix + extra_field}") for field, value in model: if isinstance(value, BaseModel): diff --git a/conda_smithy/schema.py b/conda_smithy/schema.py index d7678f91e..e875ab5f4 100644 --- a/conda_smithy/schema.py +++ b/conda_smithy/schema.py @@ -22,6 +22,15 @@ CONDA_FORGE_YAML_SCHEMA_FILE, ) +""" +Note: By default, we generate hints about additional fields the user added to the model +if extra="allow" is set. This can be disabled by inheriting from the NoExtraFieldsHint class +next to BaseModel. + +If adding new fields, you should decide between extra="forbid" and extra="allow", since +extra="ignore" (the default) will not generate hints about additional fields. +""" + class Nullable(Enum): """Created to avoid issue with schema validation of null values in lists or dicts.""" @@ -29,6 +38,15 @@ class Nullable(Enum): null = None +class NoExtraFieldsHint: + """ + Inherit from this class next to BaseModel to disable hinting about extra fields, even + if the model has `ConfigDict(extra="allow")`. + """ + + HINT_EXTRA_FIELDS = False + + ############################################# ######## Choices (Enum/Literals) definitions ######### ############################################# @@ -92,7 +110,7 @@ class BotConfigVersionUpdatesSourcesChoice(StrEnum): ############################################## -class AzureRunnerSettings(BaseModel): +class AzureRunnerSettings(BaseModel, NoExtraFieldsHint): """This is the settings for runners.""" model_config: ConfigDict = ConfigDict(extra="allow") @@ -376,7 +394,7 @@ class BotConfig(BaseModel): ) -class CondaBuildConfig(BaseModel): +class CondaBuildConfig(BaseModel, NoExtraFieldsHint): model_config: ConfigDict = ConfigDict(extra="allow") pkg_format: Optional[Literal["tar", 1, 2, "1", "2"]] = Field(