forked from mirrors/cbz
fix: allow Comic.number to be a float
This commit is contained in:
+30
-5
@@ -1,5 +1,19 @@
|
||||
from cbz.constants import COMIC_FIELDS, PAGE_FIELDS, Format, YesNo, Manga, AgeRating, LanguageISO, Rating, PageType
|
||||
from cbz.constants import (
|
||||
COMIC_FIELDS,
|
||||
PAGE_FIELDS,
|
||||
Format,
|
||||
YesNo,
|
||||
Manga,
|
||||
AgeRating,
|
||||
LanguageISO,
|
||||
Rating,
|
||||
PageType,
|
||||
)
|
||||
from cbz.utils import verify_attr, default_attr
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import Any
|
||||
|
||||
|
||||
class BaseModel:
|
||||
@@ -21,7 +35,7 @@ class BaseModel:
|
||||
# Set default values for each attribute based on its type
|
||||
setattr(self, key, kwargs.get(key, default_attr(field_type)))
|
||||
|
||||
def __setattr__(self, key: str, value: any) -> None:
|
||||
def __setattr__(self, key: str, value: "Any") -> None:
|
||||
"""
|
||||
Sets the value of an attribute and verifies its type.
|
||||
|
||||
@@ -39,6 +53,9 @@ class BaseModel:
|
||||
value = field_type(value)
|
||||
# Verify that the assigned value matches the expected type
|
||||
verify_attr(field_type, key, value)
|
||||
if isinstance(value, float):
|
||||
if value.is_integer():
|
||||
value = int(value)
|
||||
except (AttributeError, KeyError):
|
||||
pass
|
||||
super().__setattr__(key, value)
|
||||
@@ -50,9 +67,15 @@ class BaseModel:
|
||||
Returns:
|
||||
str: A string representation of the object, displaying its class name and attribute key-value pairs.
|
||||
"""
|
||||
return '{name}({items})'.format(
|
||||
return "{name}({items})".format(
|
||||
name=self.__class__.__name__,
|
||||
items=', '.join([f'{k}={repr(v)}' for k, v in self.__dict__.items() if not k.startswith('_')])
|
||||
items=", ".join(
|
||||
[
|
||||
f"{k}={repr(v)}"
|
||||
for k, v in self.__dict__.items()
|
||||
if not k.startswith("_")
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@@ -60,9 +83,10 @@ class ComicModel(BaseModel):
|
||||
"""
|
||||
Model for representing comic book metadata.
|
||||
"""
|
||||
|
||||
title: str
|
||||
series: str
|
||||
number: int
|
||||
number: float
|
||||
count: int
|
||||
volume: int
|
||||
alternate_series: str
|
||||
@@ -124,6 +148,7 @@ class PageModel(BaseModel):
|
||||
"""
|
||||
Model for representing comic book pages.
|
||||
"""
|
||||
|
||||
type: PageType
|
||||
double: bool
|
||||
image_size: int
|
||||
|
||||
Reference in New Issue
Block a user