Skip to content

Runtime final

runtime_final

C = TypeVar('C', bound=type) module-attribute

runtime_final(cls: C) -> C

Prevent a class from being subclassed at runtime.

Source code in xdsl/utils/runtime_final.py
14
15
16
17
18
19
20
21
22
23
def runtime_final(cls: C) -> C:
    """Prevent a class from being subclassed at runtime."""

    # It is safe to discard the previous __init_subclass__ method as anyway
    # the new one will raise an error.
    setattr(cls, "__init_subclass__", classmethod(_init_subclass))

    # This is a marker to check if a class is final or not.
    setattr(cls, "__final__", True)
    return cls

is_runtime_final(cls: type) -> bool

Check if a class is final.

Source code in xdsl/utils/runtime_final.py
26
27
28
def is_runtime_final(cls: type) -> bool:
    """Check if a class is final."""
    return hasattr(cls, "__final__")