attrs icon indicating copy to clipboard operation
attrs copied to clipboard

Add "resolve_types" argument to attrs.define()

Open sscherfke opened this issue 1 year ago • 2 comments

Add a resolve_types: bool = ? argument to attrs.define() and call resolve_types() on the new class if True is passed.

Why?

According to the Typed Settings issue tracker, people seem to start playing with postponed annotations. For Typed Settings to work, annotated types need to be resolved. We try to automatically do this but it is not always possible to do it. So people need to do sth. like this:

@attrs.resolve_types
@attrs.define
class Settings:
    opt: int = 0

It would be a bit nicer if they could write

@attrs.define(resolve_types=True)
class Settings:
    opt: int = 0

instead. This would also make it easier to write custom define() partials similar to frozen().

I can create a PR if needed.

sscherfke avatar May 19 '24 21:05 sscherfke

Pydantic automatically does this so I guess users might eventually expected this behavior from attrs, too.

sscherfke avatar May 19 '24 21:05 sscherfke

I'm open to PRs!

hynek avatar Jul 24 '24 07:07 hynek