{"record":{"id":"73f9c6c571e63548","repo":"pola-rs/polars","slug":"it-is-not-allowed-to-create-a-compatlevel-object","errorCode":null,"errorMessage":"it is not allowed to create a CompatLevel object","messagePattern":"it is not allowed to create a CompatLevel object","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/interchange/protocol.py","lineNumber":16,"sourceCode":"from __future__ import annotations\n\nfrom polars._utils.unstable import issue_unstable_warning\n\n# TODO: Interchange support was removed, but for stability reasons, `CompatLevel` has\n# remained here. However, we should probably move it somewhere else.\n\n\nclass CompatLevel:\n    \"\"\"Data structure compatibility level.\"\"\"\n\n    _version: int\n\n    def __init__(self) -> None:\n        msg = \"it is not allowed to create a CompatLevel object\"\n        raise TypeError(msg)\n\n    @staticmethod\n    def _with_version(version: int) -> CompatLevel:\n        compat_level = CompatLevel.__new__(CompatLevel)\n        compat_level._version = version\n        return compat_level\n\n    @staticmethod\n    def _newest() -> CompatLevel:\n        return CompatLevel._future1  # type: ignore[attr-defined]\n\n    @staticmethod\n    def newest() -> CompatLevel:\n        \"\"\"\n        Get the highest supported compatibility level.\n\n        .. warning::\n            Highest compatibility level is considered **unstable**. It may be changed","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/interchange/protocol.py#L1-L34","documentation":"`CompatLevel` is a sentinel/enum-like holder that must only be obtained via its static factories (e.g. `CompatLevel.oldest()`/`newest()`), which use `__new__` internally. Direct instantiation is blocked to keep the set of versions controlled.","triggerScenarios":"Calling `CompatLevel()` directly; passing the class where a CompatLevel instance is expected somewhere that instantiates it.","commonSituations":"Trying to define a custom compatibility level; mistaking CompatLevel for a regular options class when writing serialization code.","solutions":["Use the provided static constructors instead of instantiating","If a specific version is needed, pass the integer version to the API that accepts it rather than constructing CompatLevel yourself"],"exampleFix":"// before\nlevel = CompatLevel()\n// after\nlevel = CompatLevel.newest()  # or CompatLevel.oldest()","handlingStrategy":"type-guard","validationCode":"if compat_level is None or isinstance(compat_level, type):\n    compat_level = CompatLevel.newest()","typeGuard":"def is_compat_level(x) -> bool:\n    return isinstance(x, CompatLevel)","tryCatchPattern":"try:\n    level = CompatLevel()\nexcept TypeError:\n    level = CompatLevel.newest()","preventionTips":["Only obtain CompatLevel via its static factory methods","Never instantiate sentinel/enum-like classes directly"],"tags":["python","polars","compat-level","type-error"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}