Skip to content

comicbox.formats.base.schemas.cache ΒΆ

Cache for marshmallow schema instances.

get_schema(cls, path=None, exclude=_EMPTY_FROZENSET) ΒΆ

Get a cached schema instance, creating one if needed.

Source code in comicbox/formats/base/schemas/cache.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def get_schema(
    cls: type[_SchemaT],
    path: Path | str | None = None,
    exclude: frozenset | tuple | set = _EMPTY_FROZENSET,
) -> _SchemaT:
    """Get a cached schema instance, creating one if needed."""
    key = (cls, frozenset(exclude) if exclude else frozenset())
    if key not in _schema_cache:
        _schema_cache[key] = cls(path=path, exclude=exclude)
    # The key names the class, so the instance filed under it is one.
    schema = cast("_SchemaT", _schema_cache[key])
    # Safe on a shared cached instance: set_path writes a thread-local
    # ContextVar (the warning-prefix path), not instance state, so
    # concurrent -j N workers don't relabel each other's warnings.
    schema.set_path(path)
    return schema