41
41
from .bot import AutoShardedBot , Bot
42
42
43
43
_T = TypeVar ("_T" )
44
- BotT = Union [ Bot , AutoShardedBot ]
45
- DECBotT = Union [ commands .Bot , commands .AutoShardedBot ]
44
+ BotT = Bot | AutoShardedBot
45
+ DECBotT = commands .Bot | commands .AutoShardedBot
46
46
47
47
ellipsis = type (Ellipsis )
48
48
@@ -432,7 +432,7 @@ class CodeBlock:
432
432
_INLINE_PATTERN = re .compile (regex_patterns .INLINE_CODE_BLOCK )
433
433
434
434
def __init__ (
435
- self , code : str , language : Optional [ str ] = None , inline : Optional [ bool ] = None
435
+ self , code : str , language : str | None = None , inline : bool | None = None
436
436
) -> None :
437
437
self .code = code
438
438
self .language = language
@@ -560,6 +560,12 @@ async def convert(self, ctx: commands.Context[DECBotT], argument: str) -> str:
560
560
def __call__ (self , * args : Any , ** kwds : Any ) -> Any :
561
561
pass
562
562
563
+ def __or__ (self , other ): # Add support for UnionType
564
+ return self .__class__ .__class__ .__or__ (self .__class__ , other ) # type: ignore
565
+
566
+ def __ror__ (self , other ): # Add support for UnionType
567
+ return self .__class__ .__class__ .__ror__ (self .__class__ , other ) # type: ignore
568
+
563
569
@staticmethod
564
570
def escape (string : str ) -> str :
565
571
"""Convert a "raw" string to one where characters are escaped."""
@@ -629,7 +635,7 @@ def __init__(self, size: Any = None) -> None:
629
635
super ().__init__ ()
630
636
self .size : tuple = (..., ...) if size is None else size
631
637
632
- def __class_getitem__ (cls , size : Union [ StringParams , StringParamsTuple ] ) -> Self :
638
+ def __class_getitem__ (cls , size : StringParams | StringParamsTuple ) -> Self :
633
639
size_tuple = (..., ...)
634
640
635
641
if getattr (size , "__origin__" , None ) is Literal :
@@ -775,7 +781,7 @@ def __init__(self, regex: str, examples: tuple[str, ...]) -> None:
775
781
self .regex_pattern = re .compile (regex )
776
782
self .examples = examples
777
783
778
- def __class_getitem__ (cls , regex_and_examples : Union [ str , tuple [str , ...] ]) -> Self :
784
+ def __class_getitem__ (cls , regex_and_examples : str | tuple [str , ...]) -> Self :
779
785
regex = None
780
786
examples = ()
781
787
if isinstance (regex_and_examples , tuple ) and regex_and_examples :
@@ -891,7 +897,7 @@ def __class_getitem__(cls, params: Any) -> Self:
891
897
892
898
args = getattr (converter , "__args__" , ())
893
899
if sys .version_info >= (3 , 10 ) and converter .__class__ is types .UnionType : # type: ignore
894
- converter = Union [ args ] # type: ignore
900
+ converter = args # type: ignore
895
901
896
902
origin = getattr (converter , "__origin__" , None )
897
903
@@ -1138,10 +1144,10 @@ async def convert(
1138
1144
ctx .command
1139
1145
and getattr (fake_parameter .annotation , "__origin__" , None ) is Union
1140
1146
and type (None )
1141
- in fake_parameter .annotation .__args__ # check for Optional[ ...] # type: ignore
1147
+ in fake_parameter .annotation .__args__ # check for ... | None # type: ignore
1142
1148
and transformed is None
1143
1149
):
1144
- view .index = previous_index # view.undo() does not revert properly for Optional[ ...]
1150
+ view .index = previous_index # view.undo() does not revert properly for ... | None
1145
1151
view .previous = previous_previous
1146
1152
1147
1153
ctx .current_parameter = original_parameter
@@ -1150,6 +1156,12 @@ async def convert(
1150
1156
def __call__ (self , * args : Any , ** kwds : Any ) -> Any :
1151
1157
pass
1152
1158
1159
+ def __or__ (self , other ): # Add support for UnionType
1160
+ return self .__class__ .__class__ .__or__ (self .__class__ , other ) # type: ignore
1161
+
1162
+ def __ror__ (self , other ): # Add support for UnionType
1163
+ return self .__class__ .__class__ .__ror__ (self .__class__ , other ) # type: ignore
1164
+
1153
1165
def __repr__ (self ) -> str :
1154
1166
return f"{ self .__class__ .__name__ } [{ ', ' .join (self ._repr_converter (conv ) for conv in self .converters )} ]"
1155
1167
@@ -1296,7 +1308,7 @@ class String(str): # type: ignore
1296
1308
- `'"ab\\ "c"'` -> `'ab"c'`
1297
1309
"""
1298
1310
1299
- def __class_getitem__ (cls , size : Union [ StringParams , StringParamsTuple ] ):
1311
+ def __class_getitem__ (cls , size : StringParams | StringParamsTuple ):
1300
1312
...
1301
1313
1302
1314
class StringExpr (str ): # type: ignore
@@ -1312,15 +1324,15 @@ class StringExpr(str): # type: ignore
1312
1324
- `'"ab\\ "c"'` -> `'ab"c'`
1313
1325
"""
1314
1326
1315
- def __class_getitem__ (cls , regex_and_examples : Union [ str , tuple [str , ...] ]):
1327
+ def __class_getitem__ (cls , regex_and_examples : str | tuple [str , ...]):
1316
1328
...
1317
1329
1318
1330
class StringExprMatch (re .Match ): # type: ignore
1319
1331
"""A subclass of the `StringExpr` converter, that converts inputs into
1320
1332
`re.Match` objects instead of strings.
1321
1333
"""
1322
1334
1323
- def __class_getitem__ (cls , regex_and_examples : Union [ str , tuple [str , ...] ]):
1335
+ def __class_getitem__ (cls , regex_and_examples : str | tuple [str , ...]):
1324
1336
...
1325
1337
1326
1338
else :
@@ -1420,8 +1432,8 @@ async def convert_flag(
1420
1432
annotation = annotation .__args__ [0 ]
1421
1433
return await convert_flag (ctx , argument , flag , annotation )
1422
1434
elif origin is Union and type (None ) in annotation .__args__ :
1423
- # typing.Optional[x]
1424
- annotation = Union [ tuple (arg for arg in annotation .__args__ if arg is not type (None ))] # type: ignore
1435
+ # typing.x | None
1436
+ annotation = tuple (arg for arg in annotation .__args__ if arg is not type (None )) # type: ignore
1425
1437
return await commands .run_converters (ctx , annotation , argument , param )
1426
1438
elif origin is dict :
1427
1439
# typing.Dict[K, V] -> typing.Tuple[K, V]
0 commit comments