@@ -611,6 +611,21 @@ e: Literal[dummy()] # E: Invalid type: Literal[...] cannot contain a
611611[builtins fixtures/tuple.pyi]
612612[out]
613613
614+ [case testLiteralDisallowUnionExpressions]
615+ from typing import Literal, Optional, Union
616+ from typing import Literal as L, Optional as O, Union as U
617+
618+ a: Literal[1 | None] # E: Parameter 1 of Literal[...] cannot be a union expression
619+ b: Literal[1 | 2] # E: Parameter 1 of Literal[...] cannot be a union expression
620+ c: Literal[Literal[1] | None] # E: Parameter 1 of Literal[...] cannot be a union expression
621+ d: Literal[Union[Literal[1], None]] # E: Parameter 1 of Literal[...] cannot be a union expression
622+ e: Literal[Optional[Literal[1]]] # E: Parameter 1 of Literal[...] cannot be a union expression
623+ f: L[1 | None] # E: Parameter 1 of Literal[...] cannot be a union expression
624+ g: Literal[U[Literal[1], None]] # E: Parameter 1 of Literal[...] cannot be a union expression
625+ h: Literal[O[Literal[1]]] # E: Parameter 1 of Literal[...] cannot be a union expression
626+ [builtins fixtures/tuple.pyi]
627+ [out]
628+
614629[case testLiteralDisallowCollections]
615630from typing import Literal
616631a: Literal[{"a": 1, "b": 2}] # E: Parameter 1 of Literal[...] is invalid
@@ -688,6 +703,32 @@ reveal_type(e) # N: Revealed type is "None | None | None"
688703[builtins fixtures/bool.pyi]
689704[out]
690705
706+ [case testLiteralValidNoneUnionAlias]
707+ from typing import Literal, Union
708+
709+ a: Literal[1, None]
710+ b: Literal[Literal[1, None]]
711+
712+ Alias1 = Literal[1, None]
713+ c: Literal[Alias1]
714+
715+ Alias2 = Literal[1] | None
716+ d: Literal[Alias2]
717+
718+ Alias3 = Union[Literal[1], None]
719+ e: Literal[Alias3]
720+
721+ f: Literal["1 | None"]
722+
723+ reveal_type(a) # N: Revealed type is "Literal[1] | None"
724+ reveal_type(b) # N: Revealed type is "Literal[1] | None"
725+ reveal_type(c) # N: Revealed type is "Literal[1] | None"
726+ reveal_type(d) # N: Revealed type is "Literal[1] | None"
727+ reveal_type(e) # N: Revealed type is "Literal[1] | None"
728+ reveal_type(f) # N: Revealed type is "Literal['1 | None']"
729+ [builtins fixtures/tuple.pyi]
730+ [out]
731+
691732[case testLiteralMultipleValuesExplicitTuple]
692733from typing import Literal
693734# Unfortunately, it seems like typed_ast is unable to distinguish this from
0 commit comments