From f929462b258d406bf7577bf11a0a1a14dfb6b50a Mon Sep 17 00:00:00 2001 From: Thibault FEVRY Date: Thu, 1 Jul 2021 12:52:00 -0400 Subject: [PATCH] Import check_inits handling of duplicate definitions. (#12467) * Import fix_inits handling of duplicate definitions. * Style fix --- utils/check_inits.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/utils/check_inits.py b/utils/check_inits.py index 1e4baa5feb..d336a6fd93 100644 --- a/utils/check_inits.py +++ b/utils/check_inits.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import collections import os import re @@ -154,12 +155,23 @@ def analyze_results(import_dict_objects, type_hint_objects): """ Analyze the differences between _import_structure objects and TYPE_CHECKING objects found in an init. """ + + def find_duplicates(seq): + return [k for k, v in collections.Counter(seq).items() if v > 1] + if list(import_dict_objects.keys()) != list(type_hint_objects.keys()): return ["Both sides of the init do not have the same backends!"] errors = [] for key in import_dict_objects.keys(): - if sorted(import_dict_objects[key]) != sorted(type_hint_objects[key]): + duplicate_imports = find_duplicates(import_dict_objects[key]) + if duplicate_imports: + errors.append(f"Duplicate _import_structure definitions for: {duplicate_imports}") + duplicate_type_hints = find_duplicates(type_hint_objects[key]) + if duplicate_type_hints: + errors.append(f"Duplicate TYPE_CHECKING objects for: {duplicate_type_hints}") + + if sorted(set(import_dict_objects[key])) != sorted(set(type_hint_objects[key])): name = "base imports" if key == "none" else f"{key} backend" errors.append(f"Differences for {name}:") for a in type_hint_objects[key]: