Skip to content

Commit

Permalink
fix(api): ensure that deferred objects cannot be converted into literals
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Sep 6, 2023
1 parent 977b6d2 commit b37804a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ibis/expr/operations/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import abc
import itertools
from typing import Any, Optional, Union
from typing import Annotated, Any, Optional, Union
from typing import Literal as LiteralType

from public import public
Expand All @@ -14,7 +14,9 @@
import ibis.expr.rules as rlz
from ibis.common.annotations import attribute
from ibis.common.grounds import Singleton
from ibis.common.patterns import InstanceOf # noqa: TCH001
from ibis.common.typing import VarTuple # noqa: TCH001
from ibis.expr.deferred import Deferred # noqa: TCH001
from ibis.expr.operations.core import Named, Scalar, Unary, Value
from ibis.expr.operations.relations import Relation # noqa: TCH001

Expand Down Expand Up @@ -193,7 +195,7 @@ class Least(Value):

@public
class Literal(Scalar[T]):
value: Any
value: Annotated[Any, ~InstanceOf(Deferred)]
dtype: T

shape = ds.scalar
Expand Down
9 changes: 9 additions & 0 deletions ibis/tests/test_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import hypothesis as h
import hypothesis.strategies as st
import numpy as np
import pytest

import ibis
import ibis.expr.datatypes as dt
import ibis.expr.schema as sch
import ibis.expr.types as ir
import ibis.tests.strategies as its
from ibis.common.annotations import ValidationError


@h.given(its.null_dtype)
Expand Down Expand Up @@ -169,4 +172,10 @@ def test_memtable(memtable):
assert isinstance(memtable.schema(), sch.Schema)


@h.given(its.all_dtypes())
def test_deferred_literal(dtype):
with pytest.raises(ValidationError):
ibis.literal(ibis._.a, type=dtype)


# TODO(kszucs): we enforce field name uniqueness in the schema, but we don't for Struct datatype

0 comments on commit b37804a

Please sign in to comment.