Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ceec98e

Browse files
mjpieterszzzeek
authored andcommitted
Define type hints for remaining column operators
Added typing information for recently added operators :meth:`.ColumnOperators.icontains`, :meth:`.ColumnOperators.istartswith`, :meth:`.ColumnOperators.iendswith`, and bitwise operators :meth:`.ColumnOperators.bitwise_and`, :meth:`.ColumnOperators.bitwise_or`, :meth:`.ColumnOperators.bitwise_xor`, :meth:`.ColumnOperators.bitwise_not`, :meth:`.ColumnOperators.bitwise_lshift` :meth:`.ColumnOperators.bitwise_rshift`. Pull request courtesy Martijn Pieters. Fixes: sqlalchemy#9650 Closes: sqlalchemy#9652 Pull-request: sqlalchemy#9652 Pull-request-sha: 005c568 Change-Id: I2fa06eb42ce668df9d9c760d233906f87484dd12
1 parent 0112b3e commit ceec98e

File tree

3 files changed

+104
-17
lines changed

3 files changed

+104
-17
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.. change::
2+
:tags: bug, typing
3+
:tickets: 9650
4+
5+
Added typing information for recently added operators
6+
:meth:`.ColumnOperators.icontains`, :meth:`.ColumnOperators.istartswith`,
7+
:meth:`.ColumnOperators.iendswith`, and bitwise operators
8+
:meth:`.ColumnOperators.bitwise_and`, :meth:`.ColumnOperators.bitwise_or`,
9+
:meth:`.ColumnOperators.bitwise_xor`, :meth:`.ColumnOperators.bitwise_not`,
10+
:meth:`.ColumnOperators.bitwise_lshift`
11+
:meth:`.ColumnOperators.bitwise_rshift`. Pull request courtesy Martijn
12+
Pieters.
13+

lib/sqlalchemy/sql/elements.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,12 @@ def __contains__(self, other: Any) -> ColumnElement[bool]:
833833
def __getitem__(self, index: Any) -> ColumnElement[Any]:
834834
...
835835

836+
def __lshift__(self, other: Any) -> ColumnElement[Any]:
837+
...
838+
839+
def __rshift__(self, other: Any) -> ColumnElement[Any]:
840+
...
841+
836842
@overload
837843
def concat(self: _SQO[str], other: Any) -> ColumnElement[str]:
838844
...
@@ -854,6 +860,24 @@ def ilike(
854860
) -> BinaryExpression[bool]:
855861
...
856862

863+
def bitwise_xor(self, other: Any) -> BinaryExpression[Any]:
864+
...
865+
866+
def bitwise_or(self, other: Any) -> BinaryExpression[Any]:
867+
...
868+
869+
def bitwise_and(self, other: Any) -> BinaryExpression[Any]:
870+
...
871+
872+
def bitwise_not(self) -> UnaryExpression[_T]:
873+
...
874+
875+
def bitwise_lshift(self, other: Any) -> BinaryExpression[Any]:
876+
...
877+
878+
def bitwise_rshift(self, other: Any) -> BinaryExpression[Any]:
879+
...
880+
857881
def in_(
858882
self,
859883
other: Union[
@@ -915,6 +939,14 @@ def startswith(
915939
) -> ColumnElement[bool]:
916940
...
917941

942+
def istartswith(
943+
self,
944+
other: Any,
945+
escape: Optional[str] = None,
946+
autoescape: bool = False,
947+
) -> ColumnElement[bool]:
948+
...
949+
918950
def endswith(
919951
self,
920952
other: Any,
@@ -923,9 +955,20 @@ def endswith(
923955
) -> ColumnElement[bool]:
924956
...
925957

958+
def iendswith(
959+
self,
960+
other: Any,
961+
escape: Optional[str] = None,
962+
autoescape: bool = False,
963+
) -> ColumnElement[bool]:
964+
...
965+
926966
def contains(self, other: Any, **kw: Any) -> ColumnElement[bool]:
927967
...
928968

969+
def icontains(self, other: Any, **kw: Any) -> ColumnElement[bool]:
970+
...
971+
929972
def match(self, other: Any, **kwargs: Any) -> ColumnElement[bool]:
930973
...
931974

test/ext/mypy/plain_files/sql_operations.py

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,54 @@
6767

6868
expr9 = c1.bool_op("@@")(func.to_tsquery("some & query"))
6969

70-
# add tests for #9148
71-
and_(c1.is_(q))
72-
and_(c1.is_not(q))
73-
and_(c1.isnot(q))
74-
and_(c1.not_in(["x"]))
75-
and_(c1.notin_(["x"]))
76-
and_(c1.not_like("x"))
77-
and_(c1.notlike("x"))
78-
and_(c1.not_ilike("x"))
79-
and_(c1.notilike("x"))
80-
81-
# issue #9451
82-
s1 = c1.cast(Integer)
83-
s2 = c1.cast(Float)
84-
s3 = c1.op("foobar")("operand").cast(DateTime)
85-
s4 = cast(c1, Float)
86-
s5 = cast(c1.op("foobar")("operand"), DateTime)
70+
71+
def test_issue_9418() -> None:
72+
and_(c1.is_(q))
73+
and_(c1.is_not(q))
74+
and_(c1.isnot(q))
75+
and_(c1.not_in(["x"]))
76+
and_(c1.notin_(["x"]))
77+
and_(c1.not_like("x"))
78+
and_(c1.notlike("x"))
79+
and_(c1.not_ilike("x"))
80+
and_(c1.notilike("x"))
81+
82+
83+
def test_issue_9451() -> None:
84+
# issue #9451
85+
c1.cast(Integer)
86+
c1.cast(Float)
87+
c1.op("foobar")("operand").cast(DateTime)
88+
cast(c1, Float)
89+
cast(c1.op("foobar")("operand"), DateTime)
90+
91+
92+
def test_issue_9650_char() -> None:
93+
and_(c1.contains("x"))
94+
and_(c1.startswith("x"))
95+
and_(c1.endswith("x"))
96+
and_(c1.icontains("x"))
97+
and_(c1.istartswith("x"))
98+
and_(c1.iendswith("x"))
99+
100+
101+
def test_issue_9650_bitwise() -> None:
102+
# EXPECTED_TYPE: BinaryExpression[Any]
103+
reveal_type(c2.bitwise_and(5))
104+
# EXPECTED_TYPE: BinaryExpression[Any]
105+
reveal_type(c2.bitwise_or(5))
106+
# EXPECTED_TYPE: BinaryExpression[Any]
107+
reveal_type(c2.bitwise_xor(5))
108+
# EXPECTED_TYPE: UnaryExpression[int]
109+
reveal_type(c2.bitwise_not())
110+
# EXPECTED_TYPE: BinaryExpression[Any]
111+
reveal_type(c2.bitwise_lshift(5))
112+
# EXPECTED_TYPE: BinaryExpression[Any]
113+
reveal_type(c2.bitwise_rshift(5))
114+
# EXPECTED_TYPE: ColumnElement[Any]
115+
reveal_type(c2 << 5)
116+
# EXPECTED_TYPE: ColumnElement[Any]
117+
reveal_type(c2 >> 5)
87118

88119

89120
if typing.TYPE_CHECKING:

0 commit comments

Comments
 (0)