hundreds-breakfast-49010
10/18/2019, 10:01 PMEnum
?enough-analyst-54434
10/18/2019, 11:08 PMhundreds-father-404
10/18/2019, 11:24 PMenum()
to avoid losing the functionality of .resolve_for_enum_variant
. Originally we used a simple dictionary lookup, but several reviewers wanted the exhaustiveness check to be preserved
Converting match() from a method to a function could make sense!enough-analyst-54434
10/18/2019, 11:26 PM$ python
Python 3.7.4 (default, Oct 4 2019, 06:57:26)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from enum import Enum, auto
>>> from typing import Dict, TypeVar
>>>
>>>
>>> class Choices(Enum):
... A = auto()
... B = auto()
...
>>>
>>> E = TypeVar('E', bound=Enum)
>>> R = TypeVar('R')
>>>
>>>
>>> class EnumMatchError(ValueError):
... """Issue when using match() on an enum."""
...
>>>
>>> class UnrecognizedMatchError(EnumMatchError):
... """A value is used that is not a part of the enum."""
...
>>>
>>> class InexhaustiveMatchError(EnumMatchError):
... """Not all values of the enum specified in the pattern match."""
...
>>>
>>> def match(enum_value: Enum, arms: Dict[E, R]):
... enum_type = type(enum_value)
... all_values = [e for e in enum_type]
... unrecognized_values = [value for value in arms if value not in all_values]
... if unrecognized_values:
... raise UnrecognizedMatchError(
... f"Match includes values not defined in the enum. Unrecognized: {unrecognized_values}"
... )
... missing_values = [value for value in all_values if value not in arms]
... if missing_values:
... raise InexhaustiveMatchError(
... f"All enum values must be covered by the match. Missing: {missing_values}"
... )
... return arms[enum_value]
...
>>>
>>> match(Choices.A, {Choices.A: 42})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 12, in match
__main__.InexhaustiveMatchError: All enum values must be covered by the match. Missing: [<Choices.B: 2>]
>>> match(Choices.A, {Choices.A: 42.0, Choices.B: 1/137})
42.0
>>>
aloof-angle-91616
10/19/2019, 4:47 AMaloof-angle-91616
10/19/2019, 4:48 AMaloof-angle-91616
10/19/2019, 4:49 AMaloof-angle-91616
10/19/2019, 4:49 AMhundreds-father-404
10/19/2019, 4:51 AMhundreds-father-404
10/19/2019, 4:53 AM@frozen_after_init
. Maybe belongs in objects.py
over meta.py
?aloof-angle-91616
10/19/2019, 4:53 AMmeta.py
? i feel more comfortable putting stuff there if it's specifically to operate on stdlib typesaloof-angle-91616
10/19/2019, 4:53 AMaloof-angle-91616
10/19/2019, 4:53 AMaloof-angle-91616
10/19/2019, 4:55 AMmeta.py
maybe could be like "literal extensions to stdlib stuff like `classproperty`" and objects.py
more like "the pants codebase uses these methods/decorators"aloof-angle-91616
10/19/2019, 4:55 AMengine/objects.py
being that, but for the engine specifically somehowhundreds-father-404
10/19/2019, 4:55 AMmeta.py
for both? I’m not convinced either way and totally think it’s worth revisiting. I know it results in a bit more temporary churn, but hopefully these APIs will stabilize after one or two more weeks of tweaks like John&/ proposalaloof-angle-91616
10/19/2019, 4:56 AMobjects.py
after we kill datatype
aloof-angle-91616
10/19/2019, 4:57 AMaloof-angle-91616
10/19/2019, 4:58 AMmatch
into meta.py
(2) kill datatype
(3) remove TypeConstraint
usages
(4) kill objects.py
not too opinionated about ithundreds-father-404
10/19/2019, 4:58 AMwithAbout that file, going to take another look at Collection.of() this weekend after having to revert Collection[X]. I realized in the revert that there’s very little difference between Collection[X] and Tuple[X, ...]. We might be able to simplify by using the latter. Still testing if it will workbeing that, but for the engine specifically somehowengine/objects.py
aloof-angle-91616
10/19/2019, 5:00 AMTuple[X, ...]
mostly because i think i was conflating like "the specific Collection.of()
factory" with "the ability to wrap types in other types" in my head the last time we discussed thishundreds-father-404
10/19/2019, 5:01 AMpartially_type_checked
)hundreds-father-404
10/19/2019, 5:02 AMCollection.of()
! I was really confused by the differencealoof-angle-91616
10/19/2019, 6:39 AMmeta.py
)