MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / CddlTypeDefinition

Class CddlTypeDefinition

py/generate_bidi.py:422–530  ·  view source on GitHub ↗

Represents a CDDL type definition.

Source from the content-addressed store, hash-verified

420
421@dataclass
422class CddlTypeDefinition:
423 """Represents a CDDL type definition."""
424
425 module: str
426 name: str
427 fields: dict[str, str] = field(default_factory=dict)
428 description: str = ""
429
430 def to_python_dataclass(self, enhancements: dict[str, Any] | None = None) -> str:
431 """Generate Python dataclass code for this type.
432
433 Args:
434 enhancements: Dictionary containing dataclass_methods and method_docstrings
435 """
436 enhancements = enhancements or {}
437 dataclass_methods = enhancements.get("dataclass_methods", {})
438 method_docstrings = enhancements.get("method_docstrings", {})
439
440 # Generate class name from type name (keep it as-is, don't split on underscores)
441 class_name = self.name
442 code = "@dataclass\n"
443 code += f"class {class_name}:\n"
444 class_docstrings = enhancements.get("class_docstrings", {})
445 class_doc = _docstring_text(class_docstrings.get(class_name), class_name, self.description)
446 code += _emit_docstring(class_doc, 4)
447 code += "\n"
448
449 if not self.fields:
450 code += " pass\n"
451 else:
452 for field_name, field_type in self.fields.items():
453 # Convert CDDL type to Python type
454 python_type = self._get_python_type(field_type)
455 snake_name = CddlCommand._camel_to_snake(field_name)
456
457 # Check if the CDDL field type is a quoted string literal (e.g., type: "key")
458 # These are discriminant fields: auto-populate and exclude from __init__
459 # so callers don't need to pass them as positional or keyword arguments.
460 literal_match = re.match(r'^"([^"]+)"$', field_type.strip())
461 if literal_match:
462 literal_value = literal_match.group(1)
463 code += f' {snake_name}: str = field(default="{literal_value}", init=False)\n'
464 # Check if this field is a list type (using lowercase 'list[' from Python 3.10+ syntax)
465 elif python_type.startswith("list["):
466 # Remove the trailing ' | None' from list types since default_factory=list ensures non-None
467 type_annotation = python_type.replace(" | None", "")
468 code += f" {snake_name}: {type_annotation} = field(default_factory=list)\n"
469 # Check if this field is a dict type (using lowercase 'dict[' from Python 3.10+ syntax)
470 elif python_type.startswith("dict["):
471 # Remove the trailing ' | None' from dict types since default_factory=dict ensures non-None
472 type_annotation = python_type.replace(" | None", "")
473 code += f" {snake_name}: {type_annotation} = field(default_factory=dict)\n"
474 else:
475 code += f" {snake_name}: {python_type} = None\n"
476
477 # Add custom methods if defined for this class
478 if class_name in dataclass_methods:
479 code += "\n"

Callers 1

_extract_typesMethod · 0.85

Calls 1

fieldFunction · 0.50

Tested by

no test coverage detected