Unverified Commit f929462b authored by Thibault FEVRY's avatar Thibault FEVRY Committed by GitHub
Browse files

Import check_inits handling of duplicate definitions. (#12467)

* Import fix_inits handling of duplicate definitions.

* Style fix
parent 7f87bfc9
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import collections
import os import os
import re import re
...@@ -154,12 +155,23 @@ def analyze_results(import_dict_objects, type_hint_objects): ...@@ -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. 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()): if list(import_dict_objects.keys()) != list(type_hint_objects.keys()):
return ["Both sides of the init do not have the same backends!"] return ["Both sides of the init do not have the same backends!"]
errors = [] errors = []
for key in import_dict_objects.keys(): 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" name = "base imports" if key == "none" else f"{key} backend"
errors.append(f"Differences for {name}:") errors.append(f"Differences for {name}:")
for a in type_hint_objects[key]: for a in type_hint_objects[key]:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment