Skip to content

match syntax fails when used with raw Result instead of Failure/Success #56

@Artalus

Description

@Artalus

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I already read and followed all the documentation and didn't find an answer.

Description

Usage > Detail docs mention this:

If you are using Python 3.10 or above, you can take advantage of new syntax proposed in PEP 636 – Structural Pattern Matching to handle the result.

from meiga import Success, Failure, Error

class NoSuchKey(Error): ...
class TypeMismatch(Error): ...

match result:
    case Success(_):
        print(f"Success")
    case Failure(NoSuchKey()):
        print("Failure with NoSuchKey")
    case Failure(TypeMismatch()):
        print("Failure with TypeMismatch")
    case _:
        print("default")

While mostly convenient, this code does not work when the result variable was constructed not with Failure(NoSuchKey()) but with Result(failure=NoSuchKey()):

In [1]: from meiga import Result, Failure, Error, Success
In [2]: class NoSuchKey(Error): pass
In [3]: result = Result(failure=NoSuchKey())
In [4]: match result:
   ...:     case Success(_):
   ...:         print(f"Success")
   ...:     case Failure(NoSuchKey()):
   ...:         print("Failure with NoSuchKey")
   ...:     case Failure(TypeMismatch()):
   ...:         print("Failure with TypeMismatch")
   ...:     case _:
   ...:         print("default")

default

Getting rid of error types in case statements does not help:

In [5]: result = Result(failure=Error())
In [6]: match result:
   ...:     case Success(_):
   ...:         print("Success")
   ...:     case Failure(_):
   ...:         print("Failure")
   ...:     case _:
   ...:         print("default")

default

match and its magics are too new tech for me, so I cannot provide any insight on why isn't it working or what could be amended.

Operating System

Linux

Operating System Details

No response

meiga Version

1.8.3

Python Version

python --version

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdocumentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions