Skip to content

Commit

Permalink
Added some validation for Operation subclasses.
Browse files Browse the repository at this point in the history
Made empty name for infix Operation look nicer.
  • Loading branch information
wheerd committed Jan 20, 2017
1 parent f4f72a4 commit fa3764b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion matchpy/expressions/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ class _OperationMeta(ABCMeta):
and clashes with the `.FrozenOperation` class.
"""

def __init__(cls, name, bases, dct):
super(_OperationMeta, cls).__init__(name, bases, dct)

if cls.arity[1] and cls.one_identity:
raise TypeError('{}: An operation with fixed arity cannot have one_identity = True.'.format(name))

if cls.arity == Arity.unary and cls.infix:
raise TypeError('{}: Unary operations cannot use infix notation.'.format(name))

def _repr(cls, name): # pragma: no cover
flags = []
if cls.associative:
Expand Down Expand Up @@ -249,7 +258,8 @@ def _count_operands(operands):

def __str__(self):
if self.infix:
value = '({!s})'.format((' {!s} '.format(self.name)).join(str(o) for o in self.operands))
separator = ' {!s} '.format(self.name) if self.name else ''
value = '({!s})'.format(separator.join(str(o) for o in self.operands))
else:
value = '{!s}({!s})'.format(self.name, ', '.join(str(o) for o in self.operands))
if self.constraint:
Expand Down

0 comments on commit fa3764b

Please sign in to comment.