@@ -442,13 +442,11 @@ def _field_init(f, frozen, globals, self_name):
442442 # This field does not need initialization. Signify that
443443 # to the caller by returning None.
444444 return None
445-
446445 # Only test this now, so that we can create variables for the
447446 # default. However, return None to signify that we're not going
448447 # to actually do the assignment statement for InitVars.
449448 if f ._field_type == _FIELD_INITVAR :
450449 return None
451-
452450 # Now, actually generate the field assignment.
453451 return _field_assign (frozen , f .name , value , self_name )
454452
@@ -490,7 +488,6 @@ def _init_fn(fields, frozen, has_post_init, self_name):
490488 raise TypeError (
491489 f"non-default argument { f .name !r} " "follows default argument"
492490 )
493-
494491 globals = {"MISSING" : MISSING , "_HAS_DEFAULT_FACTORY" : _HAS_DEFAULT_FACTORY }
495492
496493 body_lines = []
@@ -500,16 +497,13 @@ def _init_fn(fields, frozen, has_post_init, self_name):
500497 # initialization (it's a pseudo-field). Just skip it.
501498 if line :
502499 body_lines .append (line )
503-
504500 # Does this class have a post-init function?
505501 if has_post_init :
506502 params_str = "," .join (f .name for f in fields if f ._field_type is _FIELD_INITVAR )
507503 body_lines .append (f"{ self_name } .{ _POST_INIT_NAME } ({ params_str } )" )
508-
509504 # If no body lines, use 'pass'.
510505 if not body_lines :
511506 body_lines = ["pass" ]
512-
513507 locals = {f"_type_{ f .name } " : f .type for f in fields }
514508 return _create_fn (
515509 "__init__" ,
@@ -674,7 +668,6 @@ def _get_field(cls, a_name, a_type):
674668 # This is a field in __slots__, so it has no default value.
675669 default = MISSING
676670 f = field (default = default )
677-
678671 # Only at this point do we know the name and the type. Set them.
679672 f .name = a_name
680673 f .type = a_type
@@ -705,7 +698,6 @@ def _get_field(cls, a_name, a_type):
705698 and _is_type (f .type , cls , typing , typing .ClassVar , _is_classvar )
706699 ):
707700 f ._field_type = _FIELD_CLASSVAR
708-
709701 # If the type is InitVar, or if it's a matching string annotation,
710702 # then it's an InitVar.
711703 if f ._field_type is _FIELD :
@@ -717,7 +709,6 @@ def _get_field(cls, a_name, a_type):
717709 and _is_type (f .type , cls , dataclasses , dataclasses .InitVar , _is_initvar )
718710 ):
719711 f ._field_type = _FIELD_INITVAR
720-
721712 # Validations for individual fields. This is delayed until now,
722713 # instead of in the Field() constructor, since only here do we
723714 # know the field name, which allows for better error reporting.
@@ -731,14 +722,12 @@ def _get_field(cls, a_name, a_type):
731722 # example, how about init=False (or really,
732723 # init=<not-the-default-init-value>)? It makes no sense for
733724 # ClassVar and InitVar to specify init=<anything>.
734-
735725 # For real fields, disallow mutable defaults for known types.
736726 if f ._field_type is _FIELD and isinstance (f .default , (list , dict , set )):
737727 raise ValueError (
738728 f"mutable default { type (f .default )} for field "
739729 f"{ f .name } is not allowed: use default_factory"
740730 )
741-
742731 return f
743732
744733
@@ -827,7 +816,6 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
827816 fields [f .name ] = f
828817 if getattr (b , _PARAMS ).frozen :
829818 any_frozen_base = True
830-
831819 # Annotations that are defined in this class (not in base
832820 # classes). If __annotations__ isn't present, then this class
833821 # adds no new annotations. We use this to compute fields that are
@@ -866,22 +854,18 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
866854 delattr (cls , f .name )
867855 else :
868856 setattr (cls , f .name , f .default )
869-
870857 # Do we have any Field members that don't also have annotations?
871858 for name , value in cls .__dict__ .items ():
872859 if isinstance (value , Field ) and not name in cls_annotations :
873860 raise TypeError (f"{ name !r} is a field but has no type annotation" )
874-
875861 # Check rules that apply if we are derived from any dataclasses.
876862 if has_dataclass_bases :
877863 # Raise an exception if any of our bases are frozen, but we're not.
878864 if any_frozen_base and not frozen :
879865 raise TypeError ("cannot inherit non-frozen dataclass from a " "frozen one" )
880-
881866 # Raise an exception if we're frozen, but none of our bases are.
882867 if not any_frozen_base and frozen :
883868 raise TypeError ("cannot inherit frozen dataclass from a " "non-frozen one" )
884-
885869 # Remember all of the fields on our class (including bases). This
886870 # also marks this class as being a dataclass.
887871 setattr (cls , _FIELDS , fields )
@@ -900,7 +884,6 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
900884 # eq methods.
901885 if order and not eq :
902886 raise ValueError ("eq must be true if order is true" )
903-
904887 if init :
905888 # Does this class have a post-init function?
906889 has_post_init = hasattr (cls , _POST_INIT_NAME )
@@ -920,15 +903,13 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
920903 "__dataclass_self__" if "self" in fields else "self" ,
921904 ),
922905 )
923-
924906 # Get the fields as a list, and include only real fields. This is
925907 # used in all of the following methods.
926908 field_list = [f for f in fields .values () if f ._field_type is _FIELD ]
927909
928910 if repr :
929911 flds = [f for f in field_list if f .repr ]
930912 _set_new_attribute (cls , "__repr__" , _repr_fn (flds ))
931-
932913 if eq :
933914 # Create _eq__ method. There's no need for a __ne__ method,
934915 # since python will call __eq__ and negate it.
@@ -938,7 +919,6 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
938919 _set_new_attribute (
939920 cls , "__eq__" , _cmp_fn ("__eq__" , "==" , self_tuple , other_tuple )
940921 )
941-
942922 if order :
943923 # Create and set the ordering methods.
944924 flds = [f for f in field_list if f .compare ]
@@ -958,15 +938,13 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
958938 f"in class { cls .__name__ } . Consider using "
959939 "functools.total_ordering"
960940 )
961-
962941 if frozen :
963942 for fn in _frozen_get_del_attr (cls , field_list ):
964943 if _set_new_attribute (cls , fn .__name__ , fn ):
965944 raise TypeError (
966945 f"Cannot overwrite attribute { fn .__name__ } "
967946 f"in class { cls .__name__ } "
968947 )
969-
970948 # Decide if/how we're going to create a hash function.
971949 hash_action = _hash_action [
972950 bool (unsafe_hash ), bool (eq ), bool (frozen ), has_explicit_hash
@@ -975,11 +953,9 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen):
975953 # No need to call _set_new_attribute here, since by the time
976954 # we're here the overwriting is unconditional.
977955 cls .__hash__ = hash_action (cls , field_list )
978-
979956 if not getattr (cls , "__doc__" ):
980957 # Create a class doc-string.
981958 cls .__doc__ = cls .__name__ + str (inspect .signature (cls )).replace (" -> None" , "" )
982-
983959 return cls
984960
985961
@@ -1015,7 +991,6 @@ def wrap(cls):
1015991 if _cls is None :
1016992 # We're called with parens.
1017993 return wrap
1018-
1019994 # We're called as @dataclass without parens.
1020995 return wrap (_cls )
1021996
@@ -1032,7 +1007,6 @@ def fields(class_or_instance):
10321007 fields = getattr (class_or_instance , _FIELDS )
10331008 except AttributeError :
10341009 raise TypeError ("must be called with a dataclass type or instance" )
1035-
10361010 # Exclude pseudo-fields. Note that fields is sorted by insertion
10371011 # order, so the order of the tuple is as the fields were defined.
10381012 return tuple (f for f in fields .values () if f ._field_type is _FIELD )
@@ -1174,7 +1148,6 @@ class C(Base):
11741148 else :
11751149 # Copy namespace since we're going to mutate it.
11761150 namespace = namespace .copy ()
1177-
11781151 # While we're looking through the field names, validate that they
11791152 # are identifiers, are not keywords, and not duplicates.
11801153 seen = set ()
@@ -1184,23 +1157,23 @@ class C(Base):
11841157 name = item
11851158 tp = "typing.Any"
11861159 elif len (item ) == 2 :
1187- name , tp , = item
1160+ (
1161+ name ,
1162+ tp ,
1163+ ) = item
11881164 elif len (item ) == 3 :
11891165 name , tp , spec = item
11901166 namespace [name ] = spec
11911167 else :
11921168 raise TypeError (f"Invalid field: { item !r} " )
1193-
11941169 if not isinstance (name , str ) or not name .isidentifier ():
11951170 raise TypeError (f"Field names must be valid identifers: { name !r} " )
11961171 if keyword .iskeyword (name ):
11971172 raise TypeError (f"Field names must not be keywords: { name !r} " )
11981173 if name in seen :
11991174 raise TypeError (f"Field name duplicated: { name !r} " )
1200-
12011175 seen .add (name )
12021176 anns [name ] = tp
1203-
12041177 namespace ["__annotations__" ] = anns
12051178 # We use `types.new_class()` instead of simply `type()` to allow dynamic creation
12061179 # of generic dataclassses.
@@ -1229,14 +1202,13 @@ class C:
12291202 c = C(1, 2)
12301203 c1 = replace(c, x=3)
12311204 assert c1.x == 3 and c1.y == 2
1232- """
1205+ """
12331206
12341207 # We're going to mutate 'changes', but that's okay because it's a
12351208 # new dict, even if called with 'replace(obj, **my_changes)'.
12361209
12371210 if not _is_dataclass_instance (obj ):
12381211 raise TypeError ("replace() should be called on dataclass instances" )
1239-
12401212 # It's an error to have init=False fields in 'changes'.
12411213 # If a field is not in 'changes', read its value from the provided obj.
12421214
@@ -1250,10 +1222,8 @@ class C:
12501222 "replace()"
12511223 )
12521224 continue
1253-
12541225 if f .name not in changes :
12551226 changes [f .name ] = getattr (obj , f .name )
1256-
12571227 # Create the new object, which calls __init__() and
12581228 # __post_init__() (if defined), using all of the init fields we've
12591229 # added and/or left in 'changes'. If there are values supplied in
0 commit comments