From 99ad742ea913e421d012c1a623029eac31bdfe85 Mon Sep 17 00:00:00 2001 From: "Eric V. Smith" Date: Mon, 3 May 2021 03:24:53 -0400 Subject: bpo-44015: dataclasses should allow KW_ONLY to be specified only once per class (GH-25841) bpo-44015: Raise a TypeError if KW_ONLY is specified more than once. --- Lib/dataclasses.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'Lib/dataclasses.py') diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 363d0b66d2..cbba320e01 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -930,6 +930,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, # we can. cls_fields = [] # Get a reference to this module for the _is_kw_only() test. + KW_ONLY_seen = False dataclasses = sys.modules[__name__] for name, type in cls_annotations.items(): # See if this is a marker to change the value of kw_only. @@ -939,6 +940,10 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, _is_kw_only))): # Switch the default to kw_only=True, and ignore this # annotation: it's not a real field. + if KW_ONLY_seen: + raise TypeError(f'{name!r} is KW_ONLY, but KW_ONLY ' + 'has already been specified') + KW_ONLY_seen = True kw_only = True else: # Otherwise it's a field of some type. -- cgit v1.2.1