Skip to content

Commit

Permalink
Merge pull request #31 from piercefreeman/feature/typehint-lists
Browse files Browse the repository at this point in the history
Typehint lists comparisons
  • Loading branch information
piercefreeman authored Dec 3, 2024
2 parents 1bf0f9a + 7702ee2 commit 768f29a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
51 changes: 50 additions & 1 deletion iceaxe/__tests__/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def test_comparison_with_different_types(db_field: DBFieldClassDefinition, value
#


def test_typehint_ilike():
def test_typehint_like():
class UserDemo(TableBase):
id: int
value_str: str
Expand All @@ -187,9 +187,58 @@ class UserDemo(TableBase):
assert_type(int_col, DBFieldClassDefinition[int])

assert_type(str_col.ilike("test"), bool)
assert_type(str_col.not_ilike("test"), bool)
assert_type(str_col.like("test"), bool)
assert_type(str_col.not_like("test"), bool)

with pyright_raises(
"reportAttributeAccessIssue",
matches=re_compile('Cannot access attribute "ilike"'),
):
int_col.ilike(5) # type: ignore

with pyright_raises(
"reportAttributeAccessIssue",
matches=re_compile('Cannot access attribute "ilike"'),
):
int_col.not_ilike(5) # type: ignore

with pyright_raises(
"reportAttributeAccessIssue",
matches=re_compile('Cannot access attribute "ilike"'),
):
int_col.like(5) # type: ignore

with pyright_raises(
"reportAttributeAccessIssue",
matches=re_compile('Cannot access attribute "ilike"'),
):
int_col.not_like(5) # type: ignore


def test_typehint_in():
class UserDemo(TableBase):
id: int
value_str: str
value_int: int

str_col = column(UserDemo.value_str)
int_col = column(UserDemo.value_int)

assert_type(str_col.in_(["test"]), bool)
assert_type(int_col.in_([5]), bool)

assert_type(str_col.not_in(["test"]), bool)
assert_type(int_col.not_in([5]), bool)

with pyright_raises(
"reportArgumentType",
matches=re_compile('cannot be assigned to parameter "other"'),
):
str_col.in_(["test", 5]) # type: ignore

with pyright_raises(
"reportArgumentType",
matches=re_compile('cannot be assigned to parameter "other"'),
):
str_col.not_in(["test", 5]) # type: ignore
6 changes: 3 additions & 3 deletions iceaxe/comparison.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass
from enum import StrEnum
from typing import Any, Generic, Self, TypeVar
from typing import Any, Generic, Self, Sequence, TypeVar

from iceaxe.queries_str import QueryElementBase, QueryLiteral
from iceaxe.typing import is_column, is_comparison, is_comparison_group
Expand Down Expand Up @@ -124,10 +124,10 @@ def __gt__(self, other):
def __ge__(self, other):
return self._compare(ComparisonType.GE, other)

def in_(self, other) -> bool:
def in_(self, other: Sequence[J]) -> bool:
return self._compare(ComparisonType.IN, other) # type: ignore

def not_in(self, other) -> bool:
def not_in(self, other: Sequence[J]) -> bool:
return self._compare(ComparisonType.NOT_IN, other) # type: ignore

def like(
Expand Down

0 comments on commit 768f29a

Please sign in to comment.