Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions ccflow/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,38 @@

_SEPARATOR = ","

# Starting 0.8.0 Nullcontext is an alias to ContextBase
NullContext = ContextBase

class _NullContextMeta(type(ContextBase)):
"""Metaclass that makes ``NullContext`` (and only ``NullContext``) a virtual superclass
of every ``ContextBase`` subclass.

A model annotated ``context: NullContext`` should accept any context. We achieve that
by lying in ``isinstance`` / ``issubclass`` while keeping ``NullContext`` a distinct
class so that introspection (``.context_type.__name__``, docstring, schema title)
reports ``NullContext`` rather than ``ContextBase``.

The lie applies only when the comparison target is ``NullContext`` itself; user-defined
subclasses of ``NullContext`` fall through to normal class-hierarchy rules.
"""

def __instancecheck__(cls, obj):
if cls is globals().get("NullContext"):
return isinstance(type(obj), type) and ContextBase in type(obj).__mro__
return super().__instancecheck__(obj)

def __subclasscheck__(cls, sub):
if cls is globals().get("NullContext"):
return isinstance(sub, type) and ContextBase in sub.__mro__
return super().__subclasscheck__(sub)


class NullContext(ContextBase, metaclass=_NullContextMeta):
"""A Null Context, used when no context is required.

Any context can be passed where a NullContext is expected, but the model
should not rely on any of its fields.
"""


C = TypeVar("C", bound=Hashable)

Expand Down
Loading