Skip to content

Commit

Permalink
make extra fields hint configurable
Browse files Browse the repository at this point in the history
don't warn for AzureRunnerSettings
and CondaBuildConfig
  • Loading branch information
ytausch committed Jul 10, 2024
1 parent 1068125 commit 8a429ed
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
3 changes: 3 additions & 0 deletions conda_smithy/data/conda-forge.json
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@
"type": "object"
},
"build_platform": {
"additionalProperties": true,
"properties": {
"emscripten_wasm32": {
"anyOf": [
Expand Down Expand Up @@ -1042,6 +1043,7 @@
"type": "object"
},
"os_version": {
"additionalProperties": true,
"properties": {
"linux_32": {
"anyOf": [
Expand Down Expand Up @@ -1183,6 +1185,7 @@
"type": "object"
},
"provider": {
"additionalProperties": true,
"properties": {
"linux": {
"anyOf": [
Expand Down
10 changes: 7 additions & 3 deletions conda_smithy/lint_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pydantic import BaseModel

from conda_smithy.schema import ConfigModel
from conda_smithy.schema import ConfigModel, NoExtraFieldsHint

str_type = str

Expand Down Expand Up @@ -163,8 +163,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):
Expand Down
22 changes: 20 additions & 2 deletions conda_smithy/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,31 @@
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."""

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 #########
#############################################
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -383,7 +401,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(
Expand Down

0 comments on commit 8a429ed

Please sign in to comment.