Open jerbaroo opened 5 years ago
Thanks for reaching out, and for the feedback!
I too would really like to do something like this. Unfortunately, we are ultimately constrained by the syntax of the language and what is observable through mypy
, so I'm not sure how to make the example as written (Case[foo: str, bar: str]
) valid.
One workaround could be to define a separate type which names the fields you care about—e.g.:
class StringPair(NamedTuple):
foo: str
bar: str
@adt
class ExampleADT:
STRING_PAIR: Case[StringPair]
… but I realize that's not ideal syntactically.
A possibility here could be to permit a "special" initial value for the field, which names the arguments—something like:
@adt
class ExampleADT:
STRING_PAIR = NamedCase(foo: str, bar: str)
If our mypy
plugin would be able to extract str, str
from this, then we should still have enough type information to check that the Case
s are being used correctly. I haven't thought about it a whole lot, though. 😄
If you or anyone would like to take a stab at something like this, I'd be happy to help and/or review the code—just let me know!
First, I love what you are doing with this library. There is just one thing holding me back from using it. I would like to name the fields, rather than access them with positional indexing. Something like what I am showing below.
The accessor method could return a
NamedTuple
instead of just aTuple
.