Skip to content

Commit

Permalink
fix: charmlib typing
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau committed Dec 11, 2024
1 parent bdc9a0a commit b9a05ad
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions charmcraft/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class CharmLib(models.CraftBaseModel):
title="Version filter for the charm. Either an API version or a specific [api].[patch].",
pattern=r"[0-9]+(\.[0-9]+)?",
coerce_numbers_to_str=False,
strict=True,
)

@pydantic.field_validator("lib", mode="before")
Expand Down Expand Up @@ -128,9 +129,11 @@ def _validate_api_version(cls, value: str) -> str:
return str(value)

@pydantic.field_validator("version", mode="before")
def _validate_patch_version(cls, value: str) -> str:
def _validate_patch_version(cls, value: str | float) -> str:
"""Validate the optional patch version, providing a useful error message."""
api, separator, patch = str(value).partition(".")
if not isinstance(value, str):
raise ValueError("Input should be a valid string") # noqa: TRY004
api, separator, patch = value.partition(".")
if not separator:
return value
try:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/models/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,10 @@ def test_invalid_patch_version(version: str):


@pytest.mark.parametrize("version", [1, 1.1])
def test_bad_version_type(version: str):
def test_bad_version_type(version: Any):
with pytest.raises(
pydantic.ValidationError,
match="1 validation error for CharmLib\nversion\n Input should be a valid string",
match="1 validation error for CharmLib\nversion\n.+Input should be a valid string",
):
project.CharmLib(lib="charm_name.lib_name", version=version)

Expand Down

0 comments on commit b9a05ad

Please sign in to comment.