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

[PY3.11 | Pytype 2024.02.13] Pattern matching fail when not specifying all enum values #1591

Open
albertocalderari opened this issue Feb 23, 2024 · 2 comments
Labels
bug cat: control flow control flow analysis

Comments

@albertocalderari
Copy link

Pattern matching fail when not specifying all enum values:

The match is missing the following cases: Environment.UAT, tenant_api.models.Environment.TEST, tenant_api.models.Environment.PROD, tenant_api.models.Environment.DEV [incomplete-match]

See my simplified code below:

class Environment(Enum):
    DEV = "DEV"
    UAT = "UAT"
    TEST = "TEST"
    PROD = "PROD"

def list_items(
        usecase_id: Optional[str] = Query(default=None),
        environment: Optional[Environment] = Query(default=None),
        asset_id: Optional[str] = Query(default=None)
) -> ListOut:
        match (usecase_id, environment, asset_id):
                    case (None, None, None):
                        out = ...
                    case (uc, None, None):
                        out = ...
                    case (None, e, None) if e:
                        out = ...
                    case (None, None, a) if a:
                        out = ...
                    case (None, e, a) if e and a:
                        out =  ...
                    case (u, e, None) if u and 
                        out = ...
                    case (u, None, a) if u and a:
                        out = ...
                    case (uc, e, a):
                        out = ...
                return out

It clearly can't figure out that all cases of the enum are handled by the variable e.
even adding a "catch all" case doesn't seem to cut it.

            case _:
                raise IncompleteMatchError("One of more case statements are not being handled!")
@rchen152 rchen152 added bug cat: control flow control flow analysis labels Feb 27, 2024
@rchen152
Copy link
Contributor

Thanks for the report. Self-contained repro:

from enum import Enum
from typing import Optional

def Query(default):
  return __any_object__

class Environment(Enum):
    DEV = "DEV"
    UAT = "UAT"
    TEST = "TEST"
    PROD = "PROD"

class ListOut:
  pass

def list_items(
        usecase_id: Optional[str] = Query(default=None),
        environment: Optional[Environment] = Query(default=None),
        asset_id: Optional[str] = Query(default=None)
) -> ListOut:
        match (usecase_id, environment, asset_id):
                    case (None, None, None):
                        out = ListOut()
                    case (uc, None, None):
                        out = ListOut()
                    case (None, e, None) if e:
                        out = ListOut()
                    case (None, None, a) if a:
                        out = ListOut()
                    case (None, e, a) if e and a:
                        out = ListOut()
                    case (u, e, None) if u and e:
                        out = ListOut()
                    case (u, None, a) if u and a:
                        out = ListOut()
                    case (uc, e, a):
                        out = ListOut()
        return out

The issue with the catch-all case is a bug that was fixed in the latest release (version 2024.02.27).

@albertocalderari
Copy link
Author

Oh wow, fantastic, lemme give it a go :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cat: control flow control flow analysis
Projects
None yet
Development

No branches or pull requests

2 participants