@@ -2252,8 +2252,13 @@ def format_literal_value(typ: LiteralType) -> str:
2252
2252
s = f"TypedDict({{{ ', ' .join (items )} }})"
2253
2253
return s
2254
2254
elif isinstance (typ , LiteralType ):
2255
+ if mypy .options ._based :
2256
+ return format_literal_value (typ )
2255
2257
return f"Literal[{ format_literal_value (typ )} ]"
2256
2258
elif isinstance (typ , UnionType ):
2259
+ if mypy .options ._based :
2260
+ return " | " .join (format (item ) for item in typ .items )
2261
+
2257
2262
literal_items , union_items = separate_union_literals (typ )
2258
2263
2259
2264
# Coalesce multiple Literal[] members. This also changes output order.
@@ -2264,14 +2269,9 @@ def format_literal_value(typ: LiteralType) -> str:
2264
2269
)
2265
2270
2266
2271
if len (union_items ) == 1 and isinstance (get_proper_type (union_items [0 ]), NoneType ):
2267
- if mypy .options ._based :
2268
- return f"{ literal_str } | None"
2269
- else :
2270
- return f"Optional[{ literal_str } ]"
2272
+ return f"Optional[{ literal_str } ]"
2271
2273
elif union_items :
2272
- if not mypy .options ._based :
2273
- return f"Union[{ format_list (union_items )} , { literal_str } ]"
2274
- return f"{ format_union (union_items )} | { literal_str } "
2274
+ return f"Union[{ format_list (union_items )} , { literal_str } ]"
2275
2275
else :
2276
2276
return literal_str
2277
2277
else :
@@ -2282,15 +2282,9 @@ def format_literal_value(typ: LiteralType) -> str:
2282
2282
)
2283
2283
if print_as_optional :
2284
2284
rest = [t for t in typ .items if not isinstance (get_proper_type (t ), NoneType )]
2285
- if mypy .options ._based :
2286
- return f"{ format (rest [0 ])} | None"
2287
- else :
2288
- return f"Optional[{ format (rest [0 ])} ]"
2285
+ return f"Optional[{ format (rest [0 ])} ]"
2289
2286
else :
2290
- if mypy .options ._based :
2291
- s = format_union (typ .items )
2292
- else :
2293
- s = f"Union[{ format_list (typ .items )} ]"
2287
+ s = f"Union[{ format_list (typ .items )} ]"
2294
2288
2295
2289
return s
2296
2290
elif isinstance (typ , NoneType ):
0 commit comments