Skip to content

Commit

Permalink
Merge pull request #21 from DavidCEllis/add_private
Browse files Browse the repository at this point in the history
Add private parameter to prefab's attribute function
  • Loading branch information
DavidCEllis authored Jul 12, 2024
2 parents e59e642 + f020d30 commit 1f7c522
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ducktools/classbuilder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

from .annotations import get_ns_annotations, is_classvar

__version__ = "v0.6.2"
__version__ = "v0.6.3"

# Change this name if you make heavy modifications
INTERNALS_DICT = "__classbuilder_internals__"
Expand Down
11 changes: 11 additions & 0 deletions src/ducktools/classbuilder/prefab.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ def attribute(
kw_only=False,
serialize=True,
exclude_field=False,
private=False,
doc=None,
type=NOTHING,
):
Expand All @@ -310,6 +311,7 @@ def attribute(
:param kw_only: Make this argument keyword only in init
:param serialize: Include this attribute in methods that serialize to dict
:param exclude_field: Shorthand for setting repr, compare, iter and serialize to False
:param private: Short for init, repr, compare, iter, serialize = False, must have default or factory
:param doc: Parameter documentation for slotted classes
:param type: Type of this attribute (for slotted classes)
Expand All @@ -321,6 +323,15 @@ def attribute(
iter = False
serialize = False

if private:
if default is NOTHING and default_factory is NOTHING:
raise AttributeError("Private attributes must have defaults or factories.")
init = False
repr = False
compare = False
iter = False
serialize = False

return Attribute(
default=default,
default_factory=default_factory,
Expand Down
1 change: 1 addition & 0 deletions src/ducktools/classbuilder/prefab.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def attribute(
kw_only: bool = False,
serialize: bool = True,
exclude_field: bool = False,
private: bool = False,
) -> Attribute: ...

def prefab_gatherer(cls_or_ns: type | MappingProxyType) -> tuple[dict[str, Attribute], dict[str, typing.Any]]: ...
Expand Down
24 changes: 24 additions & 0 deletions tests/prefab/dynamic/test_private.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from ducktools.classbuilder.prefab import Prefab, attribute, get_attributes


def test_private_attribute():
class Ex(Prefab):
_internal: "str | None" = attribute(default=None, private=True)
a: int
b: str


ex = Ex(1, "Hello")

assert ex.a == 1
assert ex.b == "Hello"

assert ex._internal is None

_internal_attrib = get_attributes(Ex)["_internal"]

assert _internal_attrib.init is False
assert _internal_attrib.repr is False
assert _internal_attrib.iter is False
assert _internal_attrib.compare is False
assert _internal_attrib.serialize is False

0 comments on commit 1f7c522

Please sign in to comment.