Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match statement not working properly for Option #234

Open
vinicius-tersi opened this issue Jan 5, 2025 · 0 comments
Open

Match statement not working properly for Option #234

vinicius-tersi opened this issue Jan 5, 2025 · 0 comments

Comments

@vinicius-tersi
Copy link

Describe the bug
I tried to reproduce the same tests in the documentation, but I was unable to reproduce a formula using Some with a match statement. I imagine that the formulas presented in the documentation work, but this one does not.

To Reproduce
Steps to reproduce the behavior:

  1. Create a test with the exists function:
    def test_option_syntax_Ok(self):
        def keep_positive(a: int) -> Option[int]:
            if a > 0:
                return Some(a)

            return Nothing

        def exists(x: Option[int]) -> bool:
            match x:
                case Some(_):
                    return True
            return False

        test1 = 100

        result1 = exists(keep_positive(test1))
        assert result1 is True

The exists() function above corresponds to what the documentation claims to work using Some.

Expected behavior
The behavior expected would be that the test would be OK, because test1 is an integer with the value 100, that with the keep_positive function is transformed into Some(100), that in the exists function should return True.

Code or Screenshots
This is the actual return of the test above I am obtaining:

ERROR: test_option_syntax_Ok (plutfolio.domain.tests.expression_lib_test.expression_test.ExpressionLibrary.test_option_syntax_Ok)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/.../domain/tests/expression_lib_test/expression_test.py", line 99, in test_option_syntax_Ok
    result1 = exists(keep_positive(test1))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../domain/tests/expression_lib_test/expression_test.py", line 93, in exists
    case Some(_):
         ^^^^^^^
TypeError: called match pattern must be a class

----------------------------------------------------------------------
Ran 7 tests in 0.003s

FAILED (errors=1)

Additional context
I am able to refactor the exists() function in two ways to make the test pass:

def exists(x: Option[int]) -> bool:
    return x.is_some()

and

def exists(x: Option[int]) -> bool:
    match x:
        case Option(tag="some"):
            return True
        case _:
            return False

However, I believe that those alternatives defeat the purpose of using match statements to unpack Option values when the match statement is more complex (such as matching with specific values). It seems that the syntax in the documentation is more pythonic, and should work.

  • OS: Windows with WSL
  • Expression version 5.3.0
  • Python version 3.12.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant