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

refactor(common): use an enum as a sentinel value instead of NoMatch class #6049

Closed
kszucs opened this issue Apr 20, 2023 · 0 comments · Fixed by #6061
Closed

refactor(common): use an enum as a sentinel value instead of NoMatch class #6049

kszucs opened this issue Apr 20, 2023 · 0 comments · Fixed by #6061

Comments

@kszucs
Copy link
Member

kszucs commented Apr 20, 2023

This pattern plays better with type checkers, and makes it clearer that NoMatch is a singleton value rather than a singleton type (i.e. consumers should check for is NoMatch rather than isinstance(x, NoMatch)).

class NoMatchType(enum.Enum):
    NoMatch = "NoMatch"

NoMatch = NoMatchType.NoMatch

This way if we ever wanted to type annotate the match methods below, their signature could be:

def match(self, value: Value) -> Value | NoMatchType:
    ...

See https://peps.python.org/pep-0484/#support-for-singleton-types-in-unions for reference.

Originally posted by @jcrist in #6041 (comment)

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

Successfully merging a pull request may close this issue.

1 participant