Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci(pre-commit.ci): autoupdate #25

Merged
merged 3 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ ci:

repos:
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.19
rev: v0.23
hooks:
- id: validate-pyproject

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.0
rev: v0.9.1
hooks:
- id: ruff
args: [--fix]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.1
rev: v1.14.1
hooks:
- id: mypy
files: "^src/"
Expand Down
4 changes: 2 additions & 2 deletions src/fieldz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

__all__ = [
"Adapter",
"asdict",
"astuple",
"Constraints",
"DataclassParams",
"Field",
"asdict",
"astuple",
"fields",
"get_adapter",
"params",
Expand Down
2 changes: 1 addition & 1 deletion src/fieldz/_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
try:
from typing import _TypingBase # type: ignore[attr-defined]
except ImportError:
from typing import _Final as _TypingBase # type: ignore[attr-defined]
from typing import _Final as _TypingBase

typing_base = _TypingBase

Expand Down
9 changes: 7 additions & 2 deletions src/fieldz/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,16 @@ class Field(Generic[_T]):
MISSING: ClassVar[Literal[_MISSING_TYPE.MISSING]] = _MISSING_TYPE.MISSING

name: str
type: type[_T] | None = None
type: type[_T] | str | Any = None
description: str | None = None
title: str | None = None
default: _T | Literal[_MISSING_TYPE.MISSING] = MISSING
default_factory: Callable[[], _T] | Literal[_MISSING_TYPE.MISSING] = MISSING
# both attrs and pydantic allow a default factory that takes the
# partially constructed object (attrs) or the init kwargs (pydantic)
# as an argument.
default_factory: (
Callable[[], _T] | Callable[[Any], _T] | Literal[_MISSING_TYPE.MISSING]
) = MISSING
repr: bool = True
hash: bool | None = None
init: bool = True
Expand Down
15 changes: 10 additions & 5 deletions src/fieldz/adapters/_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import sys
from typing import TYPE_CHECKING, Any, ClassVar, Protocol, cast, overload

from fieldz._types import DataclassParams, Field
from fieldz._types import _MISSING_TYPE, DataclassParams, Field

if TYPE_CHECKING:
from typing import Callable

import attrs
from typing_extensions import TypeGuard
from typing_extensions import Literal, TypeGuard

class AttrsInstance(Protocol):
__attrs_attrs__: ClassVar[tuple[attrs.Attribute, ...]]
Expand All @@ -25,7 +27,7 @@ def is_attrs_class(obj: object) -> bool:
"""Return True if the class is an attrs class."""
attr = sys.modules.get("attr", None)
cls = obj if isinstance(obj, type) else type(obj)
return attr.has(cls) if attr is not None else False # type: ignore [no-any-return]
return attr.has(cls) if attr is not None else False


is_instance = is_attrs_class
Expand Down Expand Up @@ -62,9 +64,12 @@ def fields(class_or_instance: Any | type) -> tuple[Field, ...]:
for f in attrs.fields(cls):
f = cast(attrs.Attribute, f)
default = Field.MISSING if f.default is attrs.NOTHING else f.default
default_factory = Field.MISSING
default_factory: (
Callable[[], Any] | Callable[[Any], Any] | Literal[_MISSING_TYPE.MISSING]
) = Field.MISSING
# attrs typing lies about the type of attrs.Factory
if isinstance(default, attrs.Factory): # type: ignore [arg-type]
default_factory, default = default.factory, Field.MISSING
default_factory, default = default.factory, Field.MISSING # type: ignore [union-attr]
fields.append(
Field(
name=f.name,
Expand Down
2 changes: 1 addition & 1 deletion src/fieldz/adapters/_dataclassy.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def fields(obj: Any | type) -> tuple:
import dataclassy

defaults = getattr(obj, "__defaults__", None) or getattr(obj, "__dict__", {})
fields = []
fields: list[Field] = []
for name, type_ in dataclassy.fields(obj).items():
default = defaults.get(name, Field.MISSING)
default_factory: Any = Field.MISSING
Expand Down