dbrattli / Expression

Pragmatic functional programming for Python inspired by F#
https://expression.readthedocs.io
MIT License
424 stars 31 forks source link

Possible wrong order of parent classes in Error? #46

Closed vputz closed 2 years ago

vputz commented 2 years ago

Just noticed this playing with Try (Python 3.9.7, expression 1.1.0):

Python 3.9.7 (default, Aug 31 2021, 13:28:12) 
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> python.el: native completion setup loaded
>>> from expression import Failure, Success
>>> x = Success(10)
>>> y = Failure(ValueError())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vputz/.cache/pypoetry/virtualenvs/plugin-playground-TQ0vVnN3-py3.9/lib/python3.9/site-packages/expression/core/result.py", line 199, in __init__
    super().__init__(str(error))
TypeError: object.__init__() takes exactly one argument (the instance to initialize)

Looks like in result.py:199:


class Error(Result[TSource, TError], ResultException):
    """The Error result case class."""

    def __init__(self, error: TError) -> None:
        super().__init__(str(error))
        self._error = error

... it looks like it's trying to call the Result version of __init__ first (which makes sense; it's first in the parent list) instead of Error.__init__, which indeed takes a string as an argument.

Changing the order of parents to:

class Error(ResultException, Result[TSource, TError]):

Avoids the error:

>>> from expression import Failure, Success
>>> x = Success(10)
>>> y = Failure(ValueError())

...although there may be another way to resolve it which may be preferable.