helpers
Class info¶
🛈 DocStrings¶
Helpers for BaseModels.
get_object_name
¶
Get the name of a function.
Source code in src/schemez/helpers.py
get_object_qualname
¶
Get the qualified name of a function.
Source code in src/schemez/helpers.py
import_callable
¶
Import a callable from a dotted path.
Supports both dot and colon notation: - Dot notation: module.submodule.Class.method - Colon notation: module.submodule:Class.method
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Import path using dots and/or colon |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If path cannot be imported or result isn't callable |
Source code in src/schemez/helpers.py
import_class
¶
Import a class from a dotted path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Dot-separated path to the class |
required |
Returns:
| Type | Description |
|---|---|
type
|
The imported class |
Raises:
| Type | Description |
|---|---|
ValueError
|
If path is invalid or doesn't point to a class |
Source code in src/schemez/helpers.py
iter_submodel_types
¶
iter_submodel_types(
model_cls: type[BaseModel], *, recursive: bool = True, include_union_members: bool = True
) -> Iterator[tuple[str, type[BaseModel]]]
Iterate through all nested BaseModel types in field annotations.
Supports field types: - BaseModel (direct type) - list[BaseModel] - dict[str, BaseModel] - BaseModel | OtherModel (unions, when include_union_members=True)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_cls
|
type[BaseModel]
|
The BaseModel class to iterate |
required |
recursive
|
bool
|
If True, also iterate through submodel types of submodels |
True
|
include_union_members
|
bool
|
If True, yield each union member separately |
True
|
Yields:
| Type | Description |
|---|---|
tuple[str, type[BaseModel]]
|
Tuples of (path, model_type) where path is like "field", "field[]", "field{}" |
Source code in src/schemez/helpers.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
iter_submodels
¶
Iterate through all nested BaseModel instances in fields.
Supports field types: - BaseModel (direct instance) - list[BaseModel] - dict[str, BaseModel]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
BaseModel
|
The BaseModel instance to iterate |
required |
recursive
|
bool
|
If True, also iterate through submodels of submodels |
True
|
Yields:
| Type | Description |
|---|---|
tuple[str, BaseModel]
|
Tuples of (path, submodel) where path is like "field", "field[0]", "field['key']" |
Source code in src/schemez/helpers.py
json_schema_to_pydantic_code
¶
json_schema_to_pydantic_code(
schema: str | dict[str, Any],
*,
class_name: str = "Model",
target_python_version: PythonVersionStr | None = None,
base_class: str = "pydantic.BaseModel"
) -> str
Generate Pydantic model code from a JSON schema using datamodel-codegen.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
str | dict[str, Any]
|
JSON schema as string or dict |
required |
class_name
|
str
|
Name for the generated class |
'Model'
|
target_python_version
|
PythonVersionStr | None
|
Python version target (3.12, 3.13, 3.14) |
None
|
base_class
|
str
|
Base class for generated model |
'pydantic.BaseModel'
|
Returns:
| Type | Description |
|---|---|
str
|
Generated Python code string |
Source code in src/schemez/helpers.py
jsonschema_to_code
¶
jsonschema_to_code(
schema_json: str, class_name: str | None = None, input_path: str | None = None
) -> str
Generate Pydantic model code from a JSON schema.
Source code in src/schemez/helpers.py
merge_models
¶
Deep merge two Pydantic models.
Source code in src/schemez/helpers.py
model_to_python_code
¶
model_to_python_code(
model: type[BaseModel] | Mapping[str, Any],
*,
class_name: str | None = None,
target_python_version: PythonVersionStr | None = None,
model_type: str = "pydantic.BaseModel"
) -> str
Convert a BaseModel or schema dict to Python code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[BaseModel] | Mapping[str, Any]
|
The BaseModel class or schema mapping to convert (a dict, or a
TypedDict like |
required |
class_name
|
str | None
|
Optional custom class name for the generated code |
None
|
target_python_version
|
PythonVersionStr | None
|
Target Python version for code generation. Defaults to current system Python version. |
None
|
model_type
|
str
|
Type of the generated model. Defaults to "pydantic.BaseModel". |
'pydantic.BaseModel'
|
Returns:
| Type | Description |
|---|---|
str
|
Generated Python code as string |
Raises:
| Type | Description |
|---|---|
ValueError
|
If schema parsing fails |
Source code in src/schemez/helpers.py
models_to_markdown_docs
¶
models_to_markdown_docs(
*models: type[BaseModel],
header_level: int = 2,
expand_mode: Literal["minimal", "maximal", "default"] = "default",
seed: int = 0,
validate: bool = False,
exclude_none: bool = True,
exclude_defaults: bool = False,
exclude_unset: bool = False,
indent: int = 2,
sort_keys: bool = True
) -> str
Generate markdown documentation with commented YAML examples for BaseModel classes.
Creates markdown chapters for each model with: - Model name as header - Model docstring - Commented YAML example with test data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
models
|
type[BaseModel]
|
One or more BaseModel classes to document |
()
|
header_level
|
int
|
Starting header level (2 = h2, 3 = h3, etc.) |
2
|
expand_mode
|
Literal['minimal', 'maximal', 'default']
|
YAML generation mode - "minimal", "maximal", or "default" |
'default'
|
seed
|
int
|
Seed for deterministic YAML generation |
0
|
validate
|
bool
|
Whether to validate the generated test data |
False
|
exclude_none
|
bool
|
Exclude fields with None values in YAML |
True
|
exclude_defaults
|
bool
|
Exclude fields with default values in YAML |
False
|
exclude_unset
|
bool
|
Exclude fields that are not set in YAML |
False
|
indent
|
int
|
Indentation level for YAML output |
2
|
sort_keys
|
bool
|
Sort keys in the YAML output |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Markdown string with chapters for each model |
Example
from pydantic import BaseModel, Field
class Person(BaseModel):
'''A person with basic information.'''
name: str = Field(description='Full name')
age: int = Field(description='Age in years')
email: str | None = Field(default=None, description='Email address')
class Company(BaseModel):
'''A company entity.'''
name: str = Field(description='Company name')
founded: int = Field(description='Year founded')
# Generate documentation
docs = models_to_markdown_docs(Person, Company)
print(docs)
Output:
Company¶
A company entity.
```Source code in src/schemez/helpers.py
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 | |
openapi_to_code
¶
Generate Pydantic model code from an OpenAPI specification URL.
Source code in src/schemez/helpers.py
resolve_type_string
¶
Convert a string representation to an actual Python type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_string
|
str
|
String representation of a type (e.g. "list[str]", "int") |
required |
safe
|
bool
|
If True, uses a limited set of allowed types. If False, allows any valid Python type expression but has potential security implications if input is untrusted |
True
|
Returns:
| Type | Description |
|---|---|
type
|
The corresponding Python type |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the type string cannot be resolved |