Skip to content

Commit

Permalink
Import check_inits handling of duplicate definitions. (huggingface#12467
Browse files Browse the repository at this point in the history
)

* Import fix_inits handling of duplicate definitions.

* Style fix
  • Loading branch information
Iwontbecreative authored Jul 1, 2021
1 parent 7f87bfc commit f929462
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion utils/check_inits.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import collections
import os
import re

Expand Down Expand Up @@ -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]:
Expand Down

0 comments on commit f929462

Please sign in to comment.