Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linting conda-forge.yml does not diagnose (some) schema violations #2152

Open
h-vetinari opened this issue Nov 21, 2024 · 2 comments
Open

Linting conda-forge.yml does not diagnose (some) schema violations #2152

h-vetinari opened this issue Nov 21, 2024 · 2 comments

Comments

@h-vetinari
Copy link
Member

h-vetinari commented Nov 21, 2024

The following file as conda-forge.yml yields a clean slate from the linter

build_platform:
  wrong: a
os_version:
  wrong: b
provider:
  wrong: c

despite the fact that, based on schema.py (or at least the obvious intent there), it should fail:

BuildPlatform = create_model(
"build_platform",
**{
platform.value: (Optional[Platforms], Field(default=platform.value))
for platform in Platforms
},
)
OSVersion = create_model(
"os_version",
**{
platform.value: (Optional[Union[str, Nullable]], Field(default=None))
for platform in Platforms
if platform.value.startswith("linux")
},
)

All three of those are using pydantic's create_model, which I presume is not working as intended here.

@h-vetinari h-vetinari changed the title Linting CBC does not diagnose wrong usage correctly Linting conda-forge.yml does not diagnose (some) schema violations Nov 21, 2024
@h-vetinari
Copy link
Member Author

h-vetinari commented Nov 21, 2024

With an addition of ConfigDict(extra='forbid'), I can get the following

from conda.base.constants import KNOWN_SUBDIRS
from pydantic import ConfigDict, Field, create_model
from enum import StrEnum

dirs = [subdir.replace("-", "_") for subdir in KNOWN_SUBDIRS if "-" in subdir]
Platforms = StrEnum("Platforms", dirs)

BuildPlatform = create_model(
    "build_platform",
    __config__=ConfigDict(extra='forbid'),
    **{
        platform.value: (Platforms, Field(default=platform.value))
        for platform in Platforms
    },
)

to yield

>>> BuildPlatform(wrong="linux_64")
pydantic_core._pydantic_core.ValidationError: 1 validation error for build_platform
wrong
  Extra inputs are not permitted [type=extra_forbidden, input_value='linux_64', input_type=str]
    For further information visit https://errors.pydantic.dev/2.9/v/extra_forbidden

However, if I add ConfigDict(extra='forbid') to schema.py and lint an affected recipe, nothing triggers. 🤔

@h-vetinari
Copy link
Member Author

I found that the work for this has already been done by @ytausch in #1920. I merged main in that PR and verified that it correctly diagnoses wrong values (as hints), and with a pretty nice message too:

recipe has some suggestions:
  Unexpected key build_platform.wrong
  Unexpected key os_version.wrong
  Unexpected key provider.wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant