Interpreting PEP 586
This question is based on my fundamental misunderstanding of Literal that was pointed-out in the "Arbitrary Literal Strings" thread. As a mental exercise, I'm imagining `open` having a mode that is `Literal["r", "w", "a", ...]` per PEP 586. If one then wants to pass a non-literal string value of "rb" (supposing it was dynamically generated), to the newly Literalized `mode` param, would the only way to accomplish this be to compare and supplying literals in if-elif statements, maps of values to their literal selves, (or structural pattern matching when available)? Is there a better way of coercing a string value to be a Literal? A casual reading of the PEP results in an ambiguous understanding of this point as well. It states:
Literal types indicate that a variable has a specific and concrete value. For example, if we define some variable foo to have type Literal[3], we are declaring that foo must be exactly equal to 3 and no other value.
It says "exactly equal to", and makes no mention of how the value was evaluated. Arguably, `foo: Literal[3] = 1 + 2` assigns a specific and concrete value of 3 to foo. foo will be exactly equal to 3 and no other value.
participants (1)
-
Paul Bryan