Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from sqlglot.errors import ErrorLevel, ParseError
from sqlglot.helper import (
AutoName,
UNIT_TESTING,
camel_to_snake_case,
ensure_collection,
ensure_list,
Expand Down Expand Up @@ -57,6 +58,7 @@ def __new__(cls, clsname, bases, attrs):

# This is so that docstrings are not inherited in pdoc
klass.__doc__ = klass.__doc__ or ""
klass._mandatory_args = tuple(k for k, v in klass.arg_types.items() if v)

return klass

Expand Down Expand Up @@ -102,6 +104,7 @@ class Expression(metaclass=_Expression):

key = "expression"
arg_types = {"this": True}
_mandatory_args: t.Tuple[str, ...] = tuple() # automatically set, used for caching purposes
__slots__ = ("args", "parent", "arg_key", "index", "comments", "_type", "_meta", "_hash")

def __init__(self, **args: t.Any):
Expand Down Expand Up @@ -769,19 +772,20 @@ def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]:
"""
errors: t.List[str] = []

for k in self.args:
if k not in self.arg_types:
errors.append(f"Unexpected keyword: '{k}' for {self.__class__}")
for k, mandatory in self.arg_types.items():
if UNIT_TESTING:
for k in self.args:
if k not in self.arg_types:
errors.append(f"Unexpected keyword: '{k}' for {self.__class__}")
for k in self._mandatory_args:
v = self.args.get(k)
if mandatory and (v is None or (isinstance(v, list) and not v)):
if v is None or (type(v) is list and not v):
errors.append(f"Required keyword: '{k}' missing for {self.__class__}")

if (
args
and isinstance(self, Func)
and len(args) > len(self.arg_types)
and not self.is_var_len_args
and len(args) > len(self.arg_types)
):
errors.append(
f"The number of provided arguments ({len(args)}) is greater than "
Expand Down
1 change: 1 addition & 0 deletions sqlglot/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

CAMEL_CASE_PATTERN = re.compile("(?<!^)(?=[A-Z])")
PYTHON_VERSION = sys.version_info[:2]
UNIT_TESTING = "unittest" in sys.modules or "pytest" in sys.modules
logger = logging.getLogger("sqlglot")


Expand Down