Skip to content

Commit

Permalink
Migrate collection config and links to pydantic 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Sep 15, 2024
1 parent 911d64e commit 6aeab3a
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 191 deletions.
30 changes: 11 additions & 19 deletions src/antsibull_docs/collection_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from collections.abc import Mapping

import asyncio_pool # type: ignore[import]
import pydantic as p
from antsibull_core import app_context
from antsibull_core.logging import log
from antsibull_core.pydantic import forbid_extras, get_formatted_error_messages
from antsibull_fileutils.yaml import load_yaml_file

from antsibull_docs._pydantic_compat import v1

from .schemas.collection_config import CollectionConfig

mlog = log.fields(mod=__name__)
Expand All @@ -27,7 +27,7 @@


def get_ansible_core_config() -> CollectionConfig:
return CollectionConfig.parse_obj(_ANSIBLE_CORE_CONFIG)
return CollectionConfig.model_validate(_ANSIBLE_CORE_CONFIG)


async def load_collection_config(
Expand All @@ -50,10 +50,10 @@ async def load_collection_config(
config_path = os.path.join(collection_path, "docs", "docsite", "config.yml")
if os.path.isfile(config_path):
try:
return CollectionConfig.parse_obj(load_yaml_file(config_path))
except v1.ValidationError:
return CollectionConfig.model_validate(load_yaml_file(config_path))
except p.ValidationError:
pass
return CollectionConfig.parse_obj({})
return CollectionConfig.model_validate({})
finally:
flog.debug("Leave")

Expand Down Expand Up @@ -99,8 +99,7 @@ def lint_collection_config(collection_path: str) -> list[tuple[str, int, int, st

result: list[tuple[str, int, int, str]] = []

for cls in (CollectionConfig,):
cls.__config__.extra = v1.Extra.forbid # type: ignore[attr-defined]
forbid_extras(CollectionConfig)

try:
config_path = os.path.join(collection_path, "docs", "docsite", "config.yml")
Expand All @@ -109,17 +108,10 @@ def lint_collection_config(collection_path: str) -> list[tuple[str, int, int, st

config_data = load_yaml_file(config_path)
try:
CollectionConfig.parse_obj(config_data)
except v1.ValidationError as exc:
for error in exc.errors():
result.append(
(
config_path,
0,
0,
v1.error_wrappers.display_errors([error]).replace("\n ", ":"),
)
)
CollectionConfig.model_validate(config_data)
except p.ValidationError as exc:
for message in get_formatted_error_messages(exc):
result.append((config_path, 0, 0, message))

return result
finally:
Expand Down
50 changes: 22 additions & 28 deletions src/antsibull_docs/collection_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
from collections.abc import Mapping

import asyncio_pool # type: ignore[import]
import pydantic as p
from antsibull_core import app_context
from antsibull_core.logging import log
from antsibull_core.pydantic import forbid_extras, get_formatted_error_messages
from antsibull_fileutils.yaml import load_yaml_file

from antsibull_docs._pydantic_compat import v1

from .schemas.collection_links import (
CollectionEditOnGitHub,
CollectionLinks,
Expand Down Expand Up @@ -99,7 +99,7 @@ def extract(
if data.get(other_key) == url:
return
if isinstance(url, str):
result.append(Link.parse_obj({"description": desc, "url": url}))
result.append(Link.model_validate({"description": desc, "url": url}))

# extract('documentation', 'Documentation')
extract("issues", "Issue Tracker")
Expand Down Expand Up @@ -130,9 +130,9 @@ def load(
else:
ld = {}
try:
result = CollectionLinks.parse_obj(ld)
except v1.ValidationError:
result = CollectionLinks.parse_obj({})
result = CollectionLinks.model_validate(ld)
except p.ValidationError:
result = CollectionLinks.model_validate({})

# Parse MANIFEST or galaxy data
issue_tracker = None
Expand Down Expand Up @@ -170,7 +170,7 @@ async def load_collection_links(
flog.debug("Enter")

if collection_name == "ansible.builtin":
return CollectionLinks.parse_obj(_ANSIBLE_CORE_METADATA)
return CollectionLinks.model_validate(_ANSIBLE_CORE_METADATA)

try:
# Load links data
Expand Down Expand Up @@ -274,16 +274,17 @@ def lint_collection_links(collection_path: str) -> list[tuple[str, int, int, str

result: list[tuple[str, int, int, str]] = []

for cls in (
CollectionEditOnGitHub,
Link,
IRCChannel,
MatrixRoom,
MailingList,
Communication,
CollectionLinks,
):
cls.__config__.extra = v1.Extra.forbid # type: ignore[attr-defined]
forbid_extras(
[
CollectionEditOnGitHub,
Link,
IRCChannel,
MatrixRoom,
MailingList,
Communication,
CollectionLinks,
]
)

try:
index_path = os.path.join(collection_path, "docs", "docsite", "links.yml")
Expand All @@ -297,18 +298,11 @@ def lint_collection_links(collection_path: str) -> list[tuple[str, int, int, str
(index_path, 0, 0, f"The key '{forbidden_key}' must not be used")
)
try:
parsed_data = CollectionLinks.parse_obj(links_data)
parsed_data = CollectionLinks.model_validate(links_data)
_check_default_values(parsed_data, index_path, result)
except v1.ValidationError as exc:
for error in exc.errors():
result.append(
(
index_path,
0,
0,
v1.error_wrappers.display_errors([error]).replace("\n ", ":"),
)
)
except p.ValidationError as exc:
for message in get_formatted_error_messages(exc):
result.append((index_path, 0, 0, message))

return result
finally:
Expand Down
2 changes: 1 addition & 1 deletion src/antsibull_docs/schemas/collection_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# SPDX-FileCopyrightText: 2023, Ansible Project
"""Schemas for collection config files."""

from antsibull_docs._pydantic_compat import v1 as p
import pydantic as p


class ChangelogConfig(p.BaseModel):
Expand Down
12 changes: 6 additions & 6 deletions src/antsibull_docs/schemas/collection_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import typing as t

from antsibull_docs._pydantic_compat import v1 as p
import pydantic as p

_SENTINEL = object()

Expand All @@ -26,8 +26,8 @@ class CollectionEditOnGitHub(p.BaseModel):
# is inside a subdirectory ansible_collections/community/general/.
path_prefix: str = ""

@p.validator("path_prefix", pre=True)
# pylint:disable=no-self-argument
@p.field_validator("path_prefix", mode="before")
@classmethod
def ensure_trailing_slash(cls, obj):
if isinstance(obj, str):
obj = obj.rstrip("/")
Expand Down Expand Up @@ -57,12 +57,12 @@ class MailingList(p.BaseModel):
url: str
subscribe: t.Optional[str] = None

@p.root_validator(pre=True)
# pylint:disable=no-self-argument
@p.model_validator(mode="before")
@classmethod
def add_subscribe(cls, values):
"""If 'subscribe' is not provided, try to deduce it from the URL."""

if values.get("subscribe", _SENTINEL) is _SENTINEL:
if isinstance(values, dict) and values.get("subscribe", _SENTINEL) is _SENTINEL:
url = str(values.get("url"))
if url.startswith(GOOGLE_GROUPS_PREFIX):
name = url[len(GOOGLE_GROUPS_PREFIX) :]
Expand Down
Loading

0 comments on commit 6aeab3a

Please sign in to comment.