Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- update mypy to 1.13
- fix typings

Note: somehow does mypy crash with the new pydantic, clear the cache for
preventing crashes
  • Loading branch information
devkral committed Nov 21, 2024
1 parent 437e471 commit b86c296
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 12 deletions.
9 changes: 6 additions & 3 deletions edgy/core/db/fields/file_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
if TYPE_CHECKING:
from edgy.core.db.fields.types import BaseFieldType
from edgy.core.db.models.types import BaseModelType
from edgy.core.db.querysets import QuerySet
from edgy.core.files.storage import Storage

IGNORED = ["cls", "__class__", "kwargs", "generate_name_fn"]
Expand All @@ -42,7 +43,7 @@ class ConcreteFileField(BaseCompositeField):
multi_process_safe: bool = True
field_file_class: type[FieldFile]
_generate_name_fn: Optional[
Callable[[Optional["BaseModelType"], Union[File, BinaryIO], str, bool], str]
Callable[[Union["BaseModelType", None, "QuerySet"], Union[File, BinaryIO], str, bool], str]
] = None

def modify_input(self, name: str, kwargs: dict[str, Any]) -> None:
Expand All @@ -64,7 +65,7 @@ def modify_input(self, name: str, kwargs: dict[str, Any]) -> None:

def generate_name_fn(
self,
instance: Optional["BaseModelType"],
instance: Union["BaseModelType", "QuerySet", None],
name: str,
file: Union[File, BinaryIO],
direct_name: bool,
Expand Down Expand Up @@ -257,7 +258,9 @@ def __new__( # type: ignore
mime_use_magic: bool = False,
field_file_class: type[FieldFile] = FieldFile,
generate_name_fn: Optional[
Callable[[Optional["BaseModelType"], Union[File, BinaryIO], str, bool], str]
Callable[
[Union["BaseModelType", None, "QuerySet"], Union[File, BinaryIO], str, bool], str
]
] = None,
**kwargs: Any,
) -> "BaseFieldType":
Expand Down
6 changes: 4 additions & 2 deletions edgy/core/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
from .types import BaseModelType

if TYPE_CHECKING:
from pydantic.fields import FieldInfo

from edgy.core.connection.database import Database
from edgy.core.db.fields.types import BaseFieldType
from edgy.core.db.models.model import Model
Expand Down Expand Up @@ -431,14 +433,14 @@ def __setattr__(self, key: str, value: Any) -> None:
field.__set__(self, value)
else:
for k, v in field.to_model(key, value).items():
if k in self.__class__.model_fields:
if k in cast(dict[str, "FieldInfo"], self.__class__.model_fields):
# __dict__ is updated and validator is executed
super().__setattr__(k, v)
else:
# bypass __setattr__ method
# ensures, __dict__ is updated
object.__setattr__(self, k, v)
elif key in self.__class__.model_fields:
elif key in cast(dict[str, "FieldInfo"], self.__class__.model_fields):
# __dict__ is updated and validator is executed
super().__setattr__(key, value)
else:
Expand Down
2 changes: 1 addition & 1 deletion edgy/core/db/models/metaclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ def __new__(
new_class.__pydantic_fields__ = model_fields
# error since pydantic 2.10
with contextlib.suppress(AttributeError):
new_class.model_fields = model_fields
new_class.model_fields = model_fields # type: ignore
new_class._db_schemas = {}

# Set the owner of the field, must be done as early as possible
Expand Down
3 changes: 2 additions & 1 deletion edgy/core/files/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

from edgy.core.db.fields.types import BaseFieldType
from edgy.core.db.models.types import BaseModelType
from edgy.core.db.querysets import QuerySet


P = ParamSpec("P")
Expand Down Expand Up @@ -271,7 +272,7 @@ def close(self, keep_size: bool = False) -> None:
class FieldFile(File):
operation: Literal["none", "save", "save_delete", "delete"] = "none"
old: Optional[tuple["Storage", str, bool]] = None
instance: Optional["BaseModelType"] = None
instance: Union["BaseModelType", None, "QuerySet"] = None
# can extract metadata
approved: bool
metadata: dict[str, Any]
Expand Down
6 changes: 3 additions & 3 deletions edgy/core/marshalls/metaclasses.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import contextlib
from typing import TYPE_CHECKING, Any, Union
from typing import TYPE_CHECKING, Any, Union, cast

from monkay import load
from pydantic._internal._model_construction import ModelMetaclass
Expand Down Expand Up @@ -107,7 +107,7 @@ def __new__(cls, name: str, bases: tuple[type, ...], attrs: dict[str, Any]) -> A
model_class.__pydantic_fields__ = model_fields
# error since pydantic 2.10
with contextlib.suppress(AttributeError):
model_class.model_fields = model_fields
model_class.model_fields = model_fields # type: ignore

# Handle annotations
annotations: dict[str, Any] = handle_annotations(bases, base_annotations, attrs)
Expand All @@ -122,7 +122,7 @@ def __new__(cls, name: str, bases: tuple[type, ...], attrs: dict[str, Any]) -> A
required_fields: set[str] = {
f"'{k}'" for k, v in model.model_fields.items() if v.is_required()
}
if any(value not in model_class.model_fields for value in required_fields):
if any(value not in cast(dict, model_class.model_fields) for value in required_fields):
fields = ", ".join(sorted(required_fields))
raise MarshallFieldDefinitionError(
f"'{model.__name__}' model requires the following mandatory fields: [{fields}]."
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ testing = [
"pytest-timeout",
"ipdb",
"pytest-mock",
"mypy==1.10",
"mypy[faster-cache]==1.13",
"httpx",
"types-orjson==3.6.2",
]
Expand Down Expand Up @@ -136,7 +136,7 @@ EDGY_TESTCLIENT_USE_EXISTING = "true"


[tool.hatch.envs.test.scripts]
check_types = "mypy -p edgy"
check_types = "mypy -p edgy {args}"

[tool.hatch.envs.hatch-test]
# needs docker services running
Expand Down

0 comments on commit b86c296

Please sign in to comment.