Skip to content

Commit 603f1b3

Browse files
committed
Proper type overload for the prefab decorator, and some other linter appeasal and type fixes
1 parent c9e4e6e commit 603f1b3

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

src/ducktools/classbuilder/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import os
3434

3535
from .annotations import get_ns_annotations, is_classvar
36-
from ._version import __version__, __version_tuple__
36+
from ._version import __version__, __version_tuple__ # noqa: F401
3737

3838
# Change this name if you make heavy modifications
3939
INTERNALS_DICT = "__classbuilder_internals__"
@@ -280,7 +280,7 @@ def cls_init_maker(cls, funcname="__init__"):
280280

281281
assigns = "\n ".join(assignments) if assignments else "pass\n"
282282
code = (
283-
f"def {funcname}(self, {args}):\n"
283+
f"def {funcname}(self, {args}):\n"
284284
f" {assigns}\n"
285285
)
286286
# Handle additional function calls
@@ -663,10 +663,10 @@ def validate_field(self):
663663
def from_field(cls, fld, /, **kwargs):
664664
"""
665665
Create an instance of field or subclass from another field.
666-
667-
This is intended to be used to convert a base
666+
667+
This is intended to be used to convert a base
668668
Field into a subclass.
669-
669+
670670
:param fld: field class to convert
671671
:param kwargs: Additional keyword arguments for subclasses
672672
:return: new field subclass instance
@@ -762,7 +762,9 @@ def field_annotation_gatherer(cls_or_ns):
762762
else:
763763
cls_dict = cls_or_ns.__dict__
764764

765-
cls_fields: dict[str, field_type] = {}
765+
# This should really be dict[str, field_type] but static analysis
766+
# doesn't understand this.
767+
cls_fields: dict[str, Field] = {}
766768
modifications = {}
767769

768770
cls_annotations = get_ns_annotations(cls_dict)

src/ducktools/classbuilder/annotations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
class _LazyAnnotationLib:
2626
def __getattr__(self, item):
2727
global _lazyannotationlib
28-
import annotationlib
28+
import annotationlib # type: ignore - this is a Python 3.14 library
2929
_lazyannotationlib = annotationlib
3030
return getattr(annotationlib, item)
3131

src/ducktools/classbuilder/prefab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
# These aren't used but are re-exported for ease of use
3838
# noinspection PyUnresolvedReferences
39-
from . import SlotFields, KW_ONLY
39+
from . import SlotFields, KW_ONLY # noqa: F401
4040

4141
PREFAB_FIELDS = "PREFAB_FIELDS"
4242
PREFAB_INIT_FUNC = "__prefab_init__"

src/ducktools/classbuilder/prefab.pyi

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,39 @@ class Prefab(metaclass=SlotMakerMeta):
128128

129129
# For some reason PyCharm can't see 'attribute'?!?
130130
# noinspection PyUnresolvedReferences
131+
@typing.overload
131132
@dataclass_transform(field_specifiers=(Attribute, attribute))
132133
def prefab(
133-
cls: type[_T] | None = None,
134+
cls: type[_T],
135+
/,
134136
*,
135-
init: bool = True,
136-
repr: bool = True,
137-
eq: bool = True,
138-
iter: bool = False,
139-
match_args: bool = True,
140-
kw_only: bool = False,
141-
frozen: bool = False,
142-
dict_method: bool = False,
143-
recursive_repr: bool = False,
144-
) -> type[_T] | Callable[[type[_T]], type[_T]]: ...
137+
init: bool = ...,
138+
repr: bool = ...,
139+
eq: bool = ...,
140+
iter: bool = ...,
141+
match_args: bool = ...,
142+
kw_only: bool = ...,
143+
frozen: bool = ...,
144+
dict_method: bool = ...,
145+
recursive_repr: bool = ...,
146+
) -> type[_T]: ...
147+
148+
@typing.overload
149+
@dataclass_transform(field_specifiers=(Attribute, attribute))
150+
def prefab(
151+
cls: None = None,
152+
/,
153+
*,
154+
init: bool = ...,
155+
repr: bool = ...,
156+
eq: bool = ...,
157+
iter: bool = ...,
158+
match_args: bool = ...,
159+
kw_only: bool = ...,
160+
frozen: bool = ...,
161+
dict_method: bool = ...,
162+
recursive_repr: bool = ...,
163+
) -> Callable[[type[_T]], type[_T]]: ...
145164

146165
def build_prefab(
147166
class_name: str,

0 commit comments

Comments
 (0)