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

Add type back to attrs.field() for use in attrs.make_class() #1082

Closed
rouge8 opened this issue Jan 7, 2023 · 8 comments · Fixed by #1107
Closed

Add type back to attrs.field() for use in attrs.make_class() #1082

rouge8 opened this issue Jan 7, 2023 · 8 comments · Fixed by #1107
Labels

Comments

@rouge8
Copy link
Contributor

rouge8 commented Jan 7, 2023

I'm using attrs.make_class() to dynamically construct a class based on other attrs classes, and it would be nice to be able to specify the type of the fields on the generated class using attrs.field() instead of attr.ib(). In other words, this works:

fields = {
    field.name: attr.ib(type=field.type)
    for field in itertools.chain.from_iterable(
        attrs.fields(t) for t in SOME_ATTRS_CLASSES
    )
    if not field.name.startswith("_")
}

MyClass = attrs.make_class("MyClass", fields)

but this does not:

fields = {
    field.name: attrs.field(type=field.type)
    for field in itertools.chain.from_iterable(
        attrs.fields(t) for t in SOME_ATTRS_CLASSES
    )
    if not field.name.startswith("_")
}

MyClass = attrs.make_class("MyClass", fields)
@hynek hynek added the Feature label Jan 9, 2023
@hynek
Copy link
Member

hynek commented Jan 9, 2023

Aw man yeah when I celebrated to get rid of that, I forgot about make_class. 🙈

@chrysle
Copy link
Contributor

chrysle commented Feb 20, 2023

I'm working on the implementation.

@hynek
Copy link
Member

hynek commented Apr 3, 2023

Given that no type checker cares about make_class() as far as I can tell (I tried to get it to fail by passing wrong arguments to a make_class-borne class), would you mind sharing the utility you're getting out of this?

@rouge8
Copy link
Contributor Author

rouge8 commented Apr 3, 2023

I ended up not needing make_class(), but I wanted type for our in-house structuring library, similar to cattrs.

@hynek
Copy link
Member

hynek commented Apr 3, 2023

So metadata, gotcha!

I'm asking because I want to put a fat warning next to the argument.

@sscherfke
Copy link
Contributor

This would also be nice for Typed Settings :-)

@hynek
Copy link
Member

hynek commented Apr 3, 2023

@sscherfke well good news in #1107 ;)

@sscherfke
Copy link
Contributor

woop woop \o/

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

Successfully merging a pull request may close this issue.

4 participants