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

Fix PackageMetadata validation error with extra provided field #256

Merged
merged 5 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion npe2/manifest/_package_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ class Config:
def _validate_root(cls, values):
if "metadata_version" not in values:
fields = cls.__fields__
mins = {fields[n].field_info.extra.get("min_ver", "1.0") for n in values}
mins = {
fields[n].field_info.extra.get("min_ver", "1.0")
for n in values
if n in fields
}
values["metadata_version"] = str(max(float(x) for x in mins))
return values

Expand Down
21 changes: 21 additions & 0 deletions tests/test_package_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,24 @@ def test_package_metadata_version():

def test_hashable():
hash(PackageMetadata(name="test", version="1.0"))


def test_package_metadata_extra_field():
pkg = {
"name": "test",
"version": "1.0",
"maintainer": "bob",
"extra_field_that_is_definitely_not_in_the_model": False,
aganders3 marked this conversation as resolved.
Show resolved Hide resolved
}

try:
p = PackageMetadata(**pkg)
aganders3 marked this conversation as resolved.
Show resolved Hide resolved
except Exception:
raise AssertionError(
"failed to parse PackageMetadata from a dict with an extra field"
)

assert p.name == "test"
assert p.version == "1.0"
assert p.maintainer == "bob"
assert not hasattr(p, "extra_field_that_is_definitely_not_in_the_model")