Skip to content

Question: make_class equivalent of the "new API" - defining fields using type annotations #869

Open
@burnpanck

Description

@burnpanck

I have been trying to use attr.make_class to create a class based on type annotations. This is exemplified by the following code:

import attr
import inspect

class Test:
    a: int
    b: str

aTest = attr.frozen(Test)

sig = inspect.get_annotations(Test)
print(sig) # {"a": int, "b": str}

# fails with AttributeError: type object 'int' has no attribute 'type'
bTest = attr.make_class("Test", sig, frozen=True, on_setattr=True, auto_attribs=True)

The intent were for bTest to be equivalent to aTest, but generated programatically. However, that doesn't seem to work. What is the standard way to create an attr class programmatically from type annotations?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions