Skip to content

Add to_field() to Attribute #1429

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions src/attr/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class Attribute(Generic[_T]):
alias: str | None

def evolve(self, **changes: Any) -> "Attribute[Any]": ...
def to_field(self) -> Any: ...

# NOTE: We had several choices for the annotation to use for type arg:
# 1) Type[_T]
Expand Down
74 changes: 71 additions & 3 deletions src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def attrib(
order=None,
on_setattr=None,
alias=None,
inherited=False,
):
"""
Create a new field / attribute on a class.
Expand Down Expand Up @@ -206,6 +207,7 @@ def attrib(
order_key=order_key,
on_setattr=on_setattr,
alias=alias,
inherited=inherited,
)


Expand Down Expand Up @@ -434,17 +436,33 @@ def _transform_attrs(

if collect_by_mro:
base_attrs, base_attr_map = _collect_base_attrs(
cls, {a.name for a in own_attrs}
cls, {a.name for a in own_attrs if a.inherited is False}
)
else:
base_attrs, base_attr_map = _collect_base_attrs_broken(
cls, {a.name for a in own_attrs}
cls, {a.name for a in own_attrs if a.inherited is False}
)

if kw_only:
own_attrs = [a.evolve(kw_only=True) for a in own_attrs]
base_attrs = [a.evolve(kw_only=True) for a in base_attrs]

own_attr_map = {attr.name: attr for attr in own_attrs}

# Overwrite explicitly inherited attributes in `base` with their versions in `own`
base_attrs = [
base_attr
if base_attr.name not in own_attr_map
else own_attr_map[base_attr.name]
for base_attr in base_attrs
]
# Strip explicitly inherited attributes from `own`, as they now live in `base`
own_attrs = [
own_attr
for own_attr in own_attrs
if own_attr.name not in base_attr_map
]

attrs = base_attrs + own_attrs

if field_transformer is not None:
Expand Down Expand Up @@ -2501,7 +2519,7 @@ def from_counting_attr(cls, name: str, ca: _CountingAttr, type=None):
None,
ca.hash,
ca.init,
False,
ca.inherited,
ca.metadata,
type,
ca.converter,
Expand Down Expand Up @@ -2532,6 +2550,35 @@ def evolve(self, **changes):

return new

def to_field(self):
"""
Converts this attribute back into a raw :py:class:`_CountingAttr` object,
such that it can be used to annotate newly `define`d classes. This is
useful if you want to reuse part (or all) of fields defined in other
attrs classes that have already been resolved into their finalized state.

.. versionadded:: 25.4.0
"""
return _CountingAttr(
default=self.default,
validator=self.validator,
repr=self.repr,
cmp=None,
hash=self.hash,
init=self.init,
converter=self.converter,
metadata=self.metadata,
type=self.type,
kw_only=self.kw_only,
eq=self.eq,
eq_key=self.eq_key,
order=self.order,
order_key=self.order_key,
on_setattr=self.on_setattr,
alias=self.alias,
inherited=self.inherited,
)

# Don't use _add_pickle since fields(Attribute) doesn't work
def __getstate__(self):
"""
Expand Down Expand Up @@ -2608,6 +2655,7 @@ class _CountingAttr:
"eq",
"eq_key",
"hash",
"inherited",
"init",
"kw_only",
"metadata",
Expand Down Expand Up @@ -2646,6 +2694,7 @@ class _CountingAttr:
"init",
"on_setattr",
"alias",
"inherited",
)
),
Attribute(
Expand All @@ -2665,6 +2714,23 @@ class _CountingAttr:
inherited=False,
on_setattr=None,
),
# Attribute(
# name="inherited",
# alias="inherited",
# default=None,
# validator=None,
# repr=True,
# cmp=None,
# hash=True,
# init=True,
# kw_only=False,
# eq=True,
# eq_key=None,
# order=False,
# order_key=None,
# inherited=False,
# on_setattr=None,
# ),
)
cls_counter = 0

Expand All @@ -2686,6 +2752,7 @@ def __init__(
order_key,
on_setattr,
alias,
inherited,
):
_CountingAttr.cls_counter += 1
self.counter = _CountingAttr.cls_counter
Expand All @@ -2704,6 +2771,7 @@ def __init__(
self.kw_only = kw_only
self.on_setattr = on_setattr
self.alias = alias
self.inherited = inherited

def validator(self, meth):
"""
Expand Down
8 changes: 8 additions & 0 deletions src/attr/_next_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ def field(
order=None,
on_setattr=None,
alias=None,
inherited=None,
):
"""
Create a new :term:`field` / :term:`attribute` on a class.
Expand Down Expand Up @@ -565,13 +566,19 @@ def field(
``__init__`` method. If left None, default to ``name`` stripped
of leading underscores. See `private-attributes`.

inherited (bool | None):
Ensure this attribute inherits the ordering of the parent attribute
with the same name. If no parent attribute with the same name
exists, this field is treated as normal.

.. versionadded:: 20.1.0
.. versionchanged:: 21.1.0
*eq*, *order*, and *cmp* also accept a custom callable
.. versionadded:: 22.2.0 *alias*
.. versionadded:: 23.1.0
The *type* parameter has been re-added; mostly for `attrs.make_class`.
Please note that type checkers ignore this metadata.
.. versionadded:: 25.4.0 *inherited*

.. seealso::

Expand All @@ -592,6 +599,7 @@ def field(
order=order,
on_setattr=on_setattr,
alias=alias,
inherited=inherited,
)


Expand Down
Loading
Loading