Skip to content

Commit

Permalink
chore!: update to pydantic 2.8.2
Browse files Browse the repository at this point in the history
Signed-off-by: Callahan Kovacs <[email protected]>
  • Loading branch information
mr-cal committed Aug 15, 2024
1 parent 495dc20 commit d0dec23
Show file tree
Hide file tree
Showing 26 changed files with 925 additions and 988 deletions.
5 changes: 2 additions & 3 deletions snapcraft/commands/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from typing import Dict, List

import tabulate
import yaml
from craft_application.commands import AppCommand
from craft_cli import emit
from overrides import overrides
Expand Down Expand Up @@ -135,5 +134,5 @@ def run(self, parsed_args):
# not part of the Project model
extract_parse_info(yaml_data_for_arch)

models.Project.unmarshal(yaml_data_for_arch)
emit.message(yaml.safe_dump(yaml_data_for_arch, indent=4, sort_keys=False))
project_data = models.Project.unmarshal(yaml_data_for_arch)
emit.message(project_data.to_yaml_string())
31 changes: 16 additions & 15 deletions snapcraft/linters/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2022 Canonical Ltd.
# Copyright 2022,2024 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
Expand All @@ -20,10 +20,12 @@
import enum
import fnmatch
from pathlib import Path
from typing import TYPE_CHECKING, List, Literal, Optional, Union
from typing import TYPE_CHECKING, Literal

import pydantic
from craft_application.models import base
from craft_cli import emit
from pydantic import ConfigDict

from snapcraft import elf, models

Expand Down Expand Up @@ -52,10 +54,10 @@ class LinterIssue(pydantic.BaseModel):
type: Literal["lint"]
name: str
result: LinterResult
filename: Optional[str]
filename: str | None = None
text: str
url: Optional[str]
suggested_changes: Optional[str] # XXX: pending definition
url: str | None = None
suggested_changes: str | None = None # XXX: pending definition

def __init__(self, **kwargs):
super().__init__(type="lint", **kwargs)
Expand All @@ -72,12 +74,11 @@ def __str__(self):

return msg

class Config:
"""Pydantic model configuration."""

validate_assignment = True
extra = "forbid"
alias_generator = lambda s: s.replace("_", "-") # noqa: E731
model_config = ConfigDict(
validate_assignment=True,
extra="forbid",
alias_generator=base.alias_generator,
)


class Linter(abc.ABC):
Expand All @@ -90,21 +91,21 @@ def __init__(
self,
name: str,
snap_metadata: "SnapMetadata",
lint: Optional[models.Lint],
lint: models.Lint | None,
):
self._name = name
self._snap_metadata = snap_metadata
self._lint = lint or models.Lint(ignore=[])

@abc.abstractmethod
def run(self) -> List[LinterIssue]:
def run(self) -> list[LinterIssue]:
"""Execute linting.
:return: A list of linter issues flagged by this linter.
"""

def _is_file_ignored(
self, filepath: Union[elf.ElfFile, Path], category: str = ""
self, filepath: elf.ElfFile | Path, category: str = ""
) -> bool:
"""Check if the file name matches an ignored file pattern.
Expand Down Expand Up @@ -136,7 +137,7 @@ def _is_file_ignored(
return False

@staticmethod
def get_categories() -> List[str]:
def get_categories() -> list[str]:
"""Get a list of specific subcategories that can be filtered against.
For Linter subclasses that perform multiple "kinds" of linting, this
Expand Down
6 changes: 3 additions & 3 deletions snapcraft/linters/library_linter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2022-2023 Canonical Ltd.
# Copyright 2022-2024 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
Expand All @@ -26,7 +26,7 @@
from snapcraft.elf import ElfFile, SonameCache, elf_utils
from snapcraft.elf import errors as elf_errors

from .base import Linter, LinterIssue, LinterResult, Optional
from .base import Linter, LinterIssue, LinterResult


class LibraryLinter(Linter):
Expand Down Expand Up @@ -130,7 +130,7 @@ def _generate_ld_config_cache(self) -> None:
if match:
self._ld_config_cache[match.group(1)] = Path(match.group(2))

def _find_deb_package(self, library_name: str) -> Optional[str]:
def _find_deb_package(self, library_name: str) -> str | None:
"""Find the deb package that provides a library.
:param library_name: The filename of the library to find.
Expand Down
4 changes: 2 additions & 2 deletions snapcraft/meta/component_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class ComponentMetadata(SnapcraftMetadata):

component: str
type: str
version: str | None
version: str | None = None
summary: str
description: str
provenance: str | None
provenance: str | None = None


def write(
Expand Down
46 changes: 15 additions & 31 deletions snapcraft/meta/manifest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2022 Canonical Ltd.
# Copyright 2022,2024 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
Expand All @@ -19,14 +19,14 @@
import json
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional
from typing import Any

from pydantic_yaml import YamlModel
import craft_application.models

from snapcraft import __version__, errors, models, os_release, utils


class Manifest(YamlModel):
class Manifest(craft_application.models.CraftBaseModel):
"""Manifest file for snaps."""

# Snapcraft annotations
Expand All @@ -40,41 +40,35 @@ class Manifest(YamlModel):
version: str
summary: str
description: str
base: Optional[str]
base: str | None = None
grade: str
confinement: str
apps: Optional[Dict[str, Any]]
parts: Dict[str, Any]
apps: dict[str, Any] | None = None
parts: dict[str, Any]

# TODO: add assumes, environment, hooks, slots

# Architecture
architectures: List[str]
architectures: list[str]

# Image info
image_info: Dict[str, Any]
image_info: dict[str, Any]

# Build environment
build_packages: List[str]
build_snaps: List[str]
primed_stage_packages: List

class Config:
"""Pydantic model configuration."""

allow_population_by_field_name = True
alias_generator = lambda s: s.replace("_", "-") # noqa: E731
build_packages: list[str]
build_snaps: list[str]
primed_stage_packages: list


def write( # noqa PLR0913
project: models.Project,
prime_dir: Path,
*,
arch: str,
parts: Dict[str, Any],
parts: dict[str, Any],
image_information: str,
start_time: datetime,
primed_stage_packages: List[str],
primed_stage_packages: list[str],
):
"""Create a manifest.yaml file."""
snap_dir = prime_dir / "snap"
Expand Down Expand Up @@ -117,14 +111,4 @@ def write( # noqa PLR0913
primed_stage_packages=primed_stage_packages,
)

yaml_data = manifest.yaml(
by_alias=True,
exclude_none=True,
exclude_unset=True,
allow_unicode=True,
sort_keys=False,
width=1000,
)

manifest_yaml = snap_dir / "manifest.yaml"
manifest_yaml.write_text(yaml_data)
manifest.to_yaml_file(snap_dir / "manifest.yaml")
Loading

0 comments on commit d0dec23

Please sign in to comment.