Skip to content

Possible regression in enum member literals as sentinel values #8820

Answered by erictraut
Sharp-Eyes asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, this error is intentional. This behavior is mandated by the Python typing spec, which explicitly disallows variables from being used in type expressions, including Literal type definitions.

Your code sample is conflating value expressions with type expressions. The statement SENTINEL = _Sentinel.SENTINEL defines a variable whose type is initialized to the value _Sentinel.SENTINEL. By contrast, the statements type SENTINEL = Literal[_Sentinel.SENTINEL] and SENTINEL: TypeAlias = Literal[_Sentinel.SENTINEL] define a type alias whose type is a literal.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Sharp-Eyes
Comment options

Answer selected by Sharp-Eyes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants