Skip to content

Commit b8494d9

Browse files
committed
Remove redundant GraphQL MRO sort (handled by base.py)
1 parent fed7732 commit b8494d9

File tree

1 file changed

+1
-39
lines changed

1 file changed

+1
-39
lines changed

src/datamodel_code_generator/parser/graphql.py

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -421,43 +421,6 @@ def parse_field(
421421
use_serialization_alias=self.use_serialization_alias,
422422
)
423423

424-
def _sort_interfaces_for_mro( # noqa: PLR6301
425-
self,
426-
interfaces: list[graphql.GraphQLInterfaceType],
427-
) -> list[graphql.GraphQLInterfaceType]:
428-
"""Sort interfaces so that subclasses come before their parent classes.
429-
430-
This ensures valid Python MRO (Method Resolution Order) when a class
431-
implements multiple interfaces where some interfaces extend others.
432-
433-
For example, if Notification implements Node, and a class implements
434-
both Node and Notification, the order should be [Notification, Node]
435-
not [Node, Notification].
436-
"""
437-
if len(interfaces) <= 1:
438-
return interfaces
439-
440-
interface_names = {i.name for i in interfaces}
441-
442-
def get_ancestors(iface: graphql.GraphQLInterfaceType) -> set[str]:
443-
"""Get all ancestor interface names that are in our interface list."""
444-
ancestors: set[str] = set()
445-
to_visit = list(getattr(iface, "interfaces", []))
446-
while to_visit:
447-
parent = to_visit.pop()
448-
if parent.name in interface_names and parent.name not in ancestors:
449-
ancestors.add(parent.name)
450-
to_visit.extend(getattr(parent, "interfaces", []))
451-
return ancestors
452-
453-
ancestor_map = {i.name: get_ancestors(i) for i in interfaces}
454-
455-
def sort_key(iface: graphql.GraphQLInterfaceType) -> tuple[int, str]:
456-
ancestor_count = sum(1 for other in interfaces if iface.name in ancestor_map[other.name])
457-
return (ancestor_count, iface.name)
458-
459-
return sorted(interfaces, key=sort_key)
460-
461424
def parse_object_like(
462425
self,
463426
obj: graphql.GraphQLInterfaceType | graphql.GraphQLObjectType | graphql.GraphQLInputObjectType,
@@ -485,8 +448,7 @@ def parse_object_like(
485448

486449
base_classes = []
487450
if hasattr(obj, "interfaces"):
488-
sorted_interfaces = self._sort_interfaces_for_mro(list(obj.interfaces)) # ty: ignore
489-
base_classes = [self.references[i.name] for i in sorted_interfaces]
451+
base_classes = [self.references[i.name] for i in obj.interfaces] # ty: ignore
490452

491453
data_model_type = self._create_data_model(
492454
reference=self.references[obj.name],

0 commit comments

Comments
 (0)