Skip to content

Commit

Permalink
Changed Arity to not be an enum anymore. This caused all sorts of pro…
Browse files Browse the repository at this point in the history
…blems in combination with typing.

This fixes #28.
  • Loading branch information
wheerd committed Oct 6, 2017
1 parent 69da140 commit 76d27a3
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions matchpy/expressions/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,37 +212,27 @@ def __hash__(self):
raise NotImplementedError()


# This class is needed so that Tuple and Enum play nicely with each other
class _ArityMeta(TupleMeta, EnumMeta):
@classmethod
def __prepare__(mcs, cls, bases, **kwargs): # pylint: disable=unused-argument
return super().__prepare__(cls, bases)


_ArityBase = NamedTuple('_ArityBase', [('min_count', int), ('fixed_size', bool)])


class Arity(_ArityBase, Enum, metaclass=_ArityMeta):
class Arity(_ArityBase):
"""Arity of an operator as (`int`, `bool`) tuple.
The first component is the minimum number of operands.
If the second component is ``True``, the operator has fixed width arity. In that case, the first component
describes the fixed number of operands required.
If it is ``False``, the operator has variable width arity.
"""
pass

def __new__(cls, *data):
return tuple.__new__(cls, data)

nullary = (0, True)
unary = (1, True)
binary = (2, True)
ternary = (3, True)
polyadic = (2, False)
variadic = (0, False)

def __repr__(self):
return "{!s}.{!s}".format(type(self).__name__, self._name_)
Arity.nullary = Arity(0, True)
Arity.unary = Arity(1, True)
Arity.binary = Arity(2, True)
Arity.ternary = Arity(3, True)
Arity.polyadic = Arity(2, False)
Arity.variadic = Arity(0, False)


class _OperationMeta(ABCMeta):
Expand Down Expand Up @@ -448,7 +438,7 @@ def new(
>>> Times = Operation.new('*', Arity.polyadic, 'Times', associative=True, commutative=True, one_identity=True)
>>> Times
Times['*', Arity.polyadic, associative, commutative, one_identity]
Times['*', Arity(min_count=2, fixed_size=False), associative, commutative, one_identity]
>>> str(Times(Symbol('a'), Symbol('b')))
'*(a, b)'
Expand Down

0 comments on commit 76d27a3

Please sign in to comment.