@@ -495,6 +495,35 @@ def _build_property_definitions(
495495 )
496496 return result
497497
498+ @staticmethod
499+ def _normalize_schema_mode (schema_mode : str | int , mode_map : dict [str , int ]) -> int :
500+ """Normalize schema_mode (str or int) to a proto SchemaMode enum value.
501+
502+ Shared by :meth:`create_label` and :meth:`create_edge_type` to avoid
503+ duplicating the validation and normalisation logic.
504+
505+ Accepts:
506+ - ``str`` — case-insensitive, leading/trailing whitespace stripped.
507+ - ``int`` — must be one of the values in *mode_map*; allows round-tripping
508+ ``LabelInfo.schema_mode`` / ``EdgeTypeInfo.schema_mode`` back into the call.
509+ """
510+ if isinstance (schema_mode , int ):
511+ # Accept int to allow round-tripping LabelInfo/EdgeTypeInfo.schema_mode.
512+ valid_ints = set (mode_map .values ())
513+ if schema_mode not in valid_ints :
514+ raise ValueError (
515+ f"schema_mode integer { schema_mode !r} is not a valid SchemaMode value; "
516+ f"expected one of { sorted (valid_ints )} or a string { list (mode_map )!r} "
517+ )
518+ return schema_mode
519+ elif isinstance (schema_mode , str ):
520+ normalized = schema_mode .strip ().lower ()
521+ if normalized not in mode_map :
522+ raise ValueError (f"schema_mode must be one of { list (mode_map )} , got { schema_mode !r} " )
523+ return mode_map [normalized ]
524+ else :
525+ raise ValueError (f"schema_mode must be a str or int, got { type (schema_mode ).__name__ !r} " )
526+
498527 async def create_label (
499528 self ,
500529 name : str ,
@@ -527,22 +556,7 @@ async def create_label(
527556 "validated" : SchemaMode .SCHEMA_MODE_VALIDATED ,
528557 "flexible" : SchemaMode .SCHEMA_MODE_FLEXIBLE ,
529558 }
530- if isinstance (schema_mode , int ):
531- # Accept int to allow round-tripping LabelInfo.schema_mode back into this call.
532- valid_ints = set (_mode_map .values ())
533- if schema_mode not in valid_ints :
534- raise ValueError (
535- f"schema_mode integer { schema_mode !r} is not a valid SchemaMode value; "
536- f"expected one of { sorted (valid_ints )} or a string { list (_mode_map )!r} "
537- )
538- proto_schema_mode = schema_mode
539- elif isinstance (schema_mode , str ):
540- schema_mode_normalized = schema_mode .strip ().lower ()
541- if schema_mode_normalized not in _mode_map :
542- raise ValueError (f"schema_mode must be one of { list (_mode_map )} , got { schema_mode !r} " )
543- proto_schema_mode = _mode_map [schema_mode_normalized ]
544- else :
545- raise ValueError (f"schema_mode must be a str or int, got { type (schema_mode ).__name__ !r} " )
559+ proto_schema_mode = self ._normalize_schema_mode (schema_mode , _mode_map )
546560
547561 proto_props = self ._build_property_definitions (properties , PropertyType , PropertyDefinition )
548562 req = CreateLabelRequest (
@@ -584,22 +598,7 @@ async def create_edge_type(
584598 "validated" : SchemaMode .SCHEMA_MODE_VALIDATED ,
585599 "flexible" : SchemaMode .SCHEMA_MODE_FLEXIBLE ,
586600 }
587- if isinstance (schema_mode , int ):
588- # Accept int to allow round-tripping EdgeTypeInfo.schema_mode back into this call.
589- valid_ints = set (_mode_map .values ())
590- if schema_mode not in valid_ints :
591- raise ValueError (
592- f"schema_mode integer { schema_mode !r} is not a valid SchemaMode value; "
593- f"expected one of { sorted (valid_ints )} or a string { list (_mode_map )!r} "
594- )
595- proto_schema_mode = schema_mode
596- elif isinstance (schema_mode , str ):
597- schema_mode_normalized = schema_mode .strip ().lower ()
598- if schema_mode_normalized not in _mode_map :
599- raise ValueError (f"schema_mode must be one of { list (_mode_map )} , got { schema_mode !r} " )
600- proto_schema_mode = _mode_map [schema_mode_normalized ]
601- else :
602- raise ValueError (f"schema_mode must be a str or int, got { type (schema_mode ).__name__ !r} " )
601+ proto_schema_mode = self ._normalize_schema_mode (schema_mode , _mode_map )
603602
604603 proto_props = self ._build_property_definitions (properties , PropertyType , PropertyDefinition )
605604 req = CreateEdgeTypeRequest (
0 commit comments