@@ -708,9 +708,10 @@ def _astuple_inner(obj, tuple_factory):
708708def make_dataclass (cls_name , fields , * , bases = (), namespace = None ):
709709 """Return a new dynamically created dataclass.
710710
711- The dataclass name will be 'cls_name'. 'fields' is an interable
712- of either (name, type) or (name, type, Field) objects. Field
713- objects are created by calling 'field(name, type [, Field])'.
711+ The dataclass name will be 'cls_name'. 'fields' is an iterable
712+ of either (name), (name, type) or (name, type, Field) objects. If type is
713+ omitted, use the string 'typing.Any'. Field objects are created by
714+ calling 'field(name, type [, Field])'.
714715
715716 C = make_class('C', [('a', int', ('b', int, Field(init=False))], bases=Base)
716717
@@ -730,12 +731,19 @@ class C(Base):
730731 # Copy namespace since we're going to mutate it.
731732 namespace = namespace .copy ()
732733
733- anns = collections .OrderedDict ((name , tp ) for name , tp , * _ in fields )
734- namespace ['__annotations__' ] = anns
734+ anns = collections .OrderedDict ()
735735 for item in fields :
736- if len (item ) == 3 :
736+ if isinstance (item , str ):
737+ name = item
738+ tp = 'typing.Any'
739+ elif len (item ) == 2 :
740+ name , tp , = item
741+ elif len (item ) == 3 :
737742 name , tp , spec = item
738743 namespace [name ] = spec
744+ anns [name ] = tp
745+
746+ namespace ['__annotations__' ] = anns
739747 cls = type (cls_name , bases , namespace )
740748 return dataclass (cls )
741749
0 commit comments