You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've found many situations where it would have been preferable to be able to use a Literal at runtime, or to be able to dynamically check if a value is in it. I'm proposing that a frozenset (and perhaps other immutable containers) should be interpreted as a Literal if defined as a literal, used as a type hint, and perhaps required to be defined with ClassVar or Final. This would allow sharing definitions of valid choices between runtime and static type checking.
Further I'm proposing to add type-guards for ... in ... statements using these Literal-like collections.
An example of how I'm thinking this should work:
AllowedMethod: Final=frozenset({"POST", "GET", "PUT", "PATCH", "HEAD"})
# This should work as if defined as method: Literal["POST", "GET", "PUT", "PATCH", "HEAD"]defrequest(method: AllowedMethod) ->Response:
...
defdo_thing(method: str) ->Response:
# Testing for membership should narrow `method` to `AllowedMethod`ifmethodinAllowedMethod:
request(method)
The text was updated successfully, but these errors were encountered:
antonagestam
changed the title
Proposal: Allow using frozenset in place of Literal
Proposal: Allow using literal immutable containers as Literal
Apr 17, 2020
antonagestam
changed the title
Proposal: Allow using literal immutable containers as Literal
Proposal: Allow using final literal immutable containers as Literal
Apr 17, 2020
I've found many situations where it would have been preferable to be able to use a
Literal
at runtime, or to be able to dynamically check if a value is in it. I'm proposing that a frozenset (and perhaps other immutable containers) should be interpreted as a Literal if defined as a literal, used as a type hint, and perhaps required to be defined with ClassVar or Final. This would allow sharing definitions of valid choices between runtime and static type checking.Further I'm proposing to add type-guards for
... in ...
statements using these Literal-like collections.An example of how I'm thinking this should work:
The text was updated successfully, but these errors were encountered: