mirror of
https://github.com/python/cpython.git
synced 2025-10-06 23:21:06 +00:00
Allow dynamic creation of generic dataclasses (GH-6319) (GH-6320)
(cherry picked from commit 5a7092de12
)
Co-authored-by: Ivan Levkivskyi <levkivskyi@gmail.com>
This commit is contained in:
parent
9e96e7b24e
commit
d063ad8962
2 changed files with 21 additions and 2 deletions
|
@ -1004,7 +1004,9 @@ def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
|
|||
anns[name] = tp
|
||||
|
||||
namespace['__annotations__'] = anns
|
||||
cls = type(cls_name, bases, namespace)
|
||||
# We use `types.new_class()` instead of simply `type()` to allow dynamic creation
|
||||
# of generic dataclassses.
|
||||
cls = types.new_class(cls_name, bases, {}, lambda ns: ns.update(namespace))
|
||||
return dataclass(cls, init=init, repr=repr, eq=eq, order=order,
|
||||
unsafe_hash=unsafe_hash, frozen=frozen)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue