Skip to content

Commit bf92f4a

Browse files
committed
refactor(client): extract _normalize_schema_mode static helper
Deduplicate schema_mode validation from create_label and create_edge_type into a shared AsyncCoordinodeClient._normalize_schema_mode() staticmethod. Both callers now pass their local _mode_map and receive the proto int back. Also remove unused langchain-coordinode from 03_langgraph_agent install cell — the notebook uses client.cypher() directly and never imports langchain_coordinode.
1 parent ab3c078 commit bf92f4a

2 files changed

Lines changed: 31 additions & 33 deletions

File tree

coordinode/coordinode/client.py

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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(

demo/notebooks/03_langgraph_agent.ipynb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
" \"install\",\n",
9595
" \"-q\",\n",
9696
" \"coordinode\",\n",
97-
" \"langchain-coordinode\",\n",
9897
" \"langchain-community\",\n",
9998
" \"langchain-openai\",\n",
10099
" \"langgraph\",\n",

0 commit comments

Comments
 (0)