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

Set-based Literal syntax #76

Open
jorenham opened this issue Oct 1, 2024 · 0 comments
Open

Set-based Literal syntax #76

jorenham opened this issue Oct 1, 2024 · 0 comments

Comments

@jorenham
Copy link
Owner

jorenham commented Oct 1, 2024

allow writing Literal[<args>] as {<args>}, i.e. a set literal, and only a set literal

the same rules apply to <args> as for typing.Literal

this example illustrates that | works in the same way for sets as it does for Literal, making this a rather intuitive syntax, that additionally highlights the unique and unordered nature of Literal types, and automatically disallows unhashable (i.e. immutable) types

type Zero = {False, 0}
type Blank = {"", b""}
type Falsy = Zero | Blank

with the default --target=3.10, this translates to

from typing import Literal, TypeAlias

Zero: TypeAlias = Literal[False, 0]
Blank: TypeAlias = Literal["", b""]
Falsy: TypeAlias = Literal[Zero, Blank]

note that the order isn't lost, as this is but a syntactical conversion

also note that Falsy is a nested Literal, which, unlike union-ed Literals, is automatically unpacked and de-duplicated at runtime, so should for the sake of consistency also be the preferred approach within stubs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant