6565from numbers import Number
6666from typing import Callable
6767
68+ from typing_extensions import TypedDict
69+
6870
6971@dataclass
7072class SQLNone :
@@ -82,7 +84,9 @@ def __repr__(self: SQLNone) -> str:
8284class SQLExpression :
8385 """SQL expression base class."""
8486
85- __hash__ = None
87+ def __hash__ (self : SQLExpression ) -> int :
88+ """Return hash of the object (Not used)."""
89+ return hash (str (self )) # pragma: no cover
8690
8791 def __repr__ (self : SQLExpression ) -> str :
8892 """Return a string representation of the object."""
@@ -156,19 +160,23 @@ def __abs__(self: SQLExpression) -> SQLTriplet:
156160 """Return the absolute value of the object."""
157161 return SQLTriplet (self , operator .abs )
158162
159- def __eq__ (self : SQLExpression , other : SQLExpression ) -> SQLTriplet :
163+ def __eq__ ( # type: ignore[override]
164+ self : SQLExpression , other : object
165+ ) -> SQLTriplet :
160166 """Define how the object is compared for equality."""
161167 return SQLTriplet (self , operator .eq , other )
162168
163- def __ne__ (self : SQLExpression , other : object ) -> SQLTriplet :
169+ def __ne__ ( # type: ignore[override]
170+ self : SQLExpression , other : object
171+ ) -> SQLTriplet :
164172 """Define how the object is compared for equality (not equal to)."""
165173 return SQLTriplet (self , operator .ne , other )
166174
167175 def __neg__ (self : SQLExpression ) -> SQLTriplet :
168176 """Define how the object is compared for negation (not equal to)."""
169177 return SQLTriplet (self , operator .neg )
170178
171- def __contains__ (self : SQLExpression , other : object ) -> bool :
179+ def __contains__ (self : SQLExpression , other : object ) -> SQLTriplet :
172180 """Test whether the object contains the specified object or not."""
173181 return SQLTriplet (self , "contains" , other )
174182
@@ -209,9 +217,9 @@ class SQLTriplet(SQLExpression):
209217
210218 def __init__ (
211219 self : SQLExpression ,
212- lhs : SQLTriplet | str ,
220+ lhs : SQLTriplet | str | SQLExpression | Number | bool | object ,
213221 op : Callable | str | None = None ,
214- rhs : SQLTriplet | str | None = None ,
222+ rhs : SQLTriplet | str | SQLExpression | Number | SQLNone | object | None = None ,
215223 ) -> None :
216224 """Initialize :class:`SQLTriplet`."""
217225 self .lhs = lhs
@@ -244,7 +252,7 @@ def __init__(
244252 "bool" : lambda x , _ : f"({ x } != 0)" ,
245253 }
246254
247- def __str__ (self : SQLExpression ) -> str :
255+ def __str__ (self : SQLTriplet ) -> str :
248256 """Return a human-readable, or informal, string representation of an object."""
249257 lhs = self .lhs
250258 rhs = self .rhs
@@ -268,7 +276,7 @@ def __str__(self: SQLJSONDictionary) -> str:
268276 """Return a human-readable, or informal, string representation of an object."""
269277 return f"json_extract(properties, '$.{ self .acc } ')"
270278
271- def __getitem__ (self : SQLJSONDictionary , key : str ) -> SQLJSONDictionary :
279+ def __getitem__ (self : SQLJSONDictionary , key : str | int ) -> SQLJSONDictionary :
272280 """Get an item from the dataset."""
273281 key_str = f"[{ key } ]" if isinstance (key , (int ,)) else f'"{ key } "'
274282
@@ -424,11 +432,14 @@ def sql_has_key(dictionary: SQLJSONDictionary, key: str | int) -> SQLTriplet:
424432
425433# Constants defining the global variables for use in eval() when
426434# evaluating expressions.
427-
428- _COMMON_GLOBALS = {
435+ COMMON_GLOBALS_Type = TypedDict (
436+ "COMMON_GLOBALS_Type" , {"__builtins__" : dict [str , Callable ], "re" : object }
437+ )
438+ _COMMON_GLOBALS : COMMON_GLOBALS_Type = {
429439 "__builtins__" : {"abs" : abs },
430440 "re" : re .RegexFlag ,
431441}
442+
432443SQL_GLOBALS = {
433444 "__builtins__" : {** _COMMON_GLOBALS ["__builtins__" ], "sum" : sql_list_sum },
434445 "props" : SQLJSONDictionary (),
0 commit comments