Skip to content

Can only populate attrs classes with _CountingAttr instances #1424

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

Open
redruin1 opened this issue Apr 6, 2025 · 1 comment
Open

Can only populate attrs classes with _CountingAttr instances #1424

redruin1 opened this issue Apr 6, 2025 · 1 comment

Comments

@redruin1
Copy link

redruin1 commented Apr 6, 2025

A user might want to use attributes from an already @defined class to populate a new attrs class. However, _ClassBuilder expects all given attributes to be instances of _CountingAttr instead of Attribute instances, which raises an exception when provided:

import attrs

@attrs.define
class Example:
    a: int

ExampleCopy = attrs.make_class("ExampleCopy", attrs={"a": attrs.fields(Example).a})
# AttributeError: 'Attribute' object has no attribute '_validator'. Did you mean: 'validator'?

@attrs.define(these={"a": attrs.fields(Example).a})
class ExampleCopy:
    pass
# AttributeError: 'Attribute' object has no attribute '_validator'. Did you mean: 'validator'?

I would either expect _ClassBuilder to handle both kinds of attributes, or there be an officially sanctioned function allowing you to convert an Attribute to a _CountingAttr for cases like this. You can write this conversion function manually, something like:

def convert_to_countingattr(attr):
    """
    Convert an `Attribute` instance to an equivalent `_CountingAttr` instance.
    """
    return attrs.field(**{
        slot: getattr(attr, slot) 
        for slot in attr.__slots__ 
        if slot not in {"name", "eq_key", "order_key", "inherited"}
    })

But this feels janky and prone to breakage.

@redruin1
Copy link
Author

This seems related to #637; the proposed to_field() method would make this trivial.

The utility of this behavior is that it would make it much easier to manipulate the user class before it is passed to attrs plumbing, so you could do things like custom attribute ordering or similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant