@@ -392,12 +392,19 @@ def binop(self, other):
392
392
__mod__ = _make_binop (ast .Mod ())
393
393
__lshift__ = _make_binop (ast .LShift ())
394
394
__rshift__ = _make_binop (ast .RShift ())
395
- __or__ = _make_binop (ast .BitOr ())
396
395
__xor__ = _make_binop (ast .BitXor ())
397
396
__and__ = _make_binop (ast .BitAnd ())
398
397
__floordiv__ = _make_binop (ast .FloorDiv ())
399
398
__pow__ = _make_binop (ast .Pow ())
400
399
400
+ def __or__ (self , other ):
401
+ if self .__stringifier_dict__ .create_unions :
402
+ return types .UnionType [self , other ]
403
+
404
+ return self .__make_new (
405
+ ast .BinOp (self .__get_ast (), ast .BitOr (), self .__convert_to_ast (other ))
406
+ )
407
+
401
408
del _make_binop
402
409
403
410
def _make_rbinop (op : ast .AST ):
@@ -416,12 +423,19 @@ def rbinop(self, other):
416
423
__rmod__ = _make_rbinop (ast .Mod ())
417
424
__rlshift__ = _make_rbinop (ast .LShift ())
418
425
__rrshift__ = _make_rbinop (ast .RShift ())
419
- __ror__ = _make_rbinop (ast .BitOr ())
420
426
__rxor__ = _make_rbinop (ast .BitXor ())
421
427
__rand__ = _make_rbinop (ast .BitAnd ())
422
428
__rfloordiv__ = _make_rbinop (ast .FloorDiv ())
423
429
__rpow__ = _make_rbinop (ast .Pow ())
424
430
431
+ def __ror__ (self , other ):
432
+ if self .__stringifier_dict__ .create_unions :
433
+ return types .UnionType [other , self ]
434
+
435
+ return self .__make_new (
436
+ ast .BinOp (self .__convert_to_ast (other ), ast .BitOr (), self .__get_ast ())
437
+ )
438
+
425
439
del _make_rbinop
426
440
427
441
def _make_compare (op ):
@@ -459,12 +473,13 @@ def unary_op(self):
459
473
460
474
461
475
class _StringifierDict (dict ):
462
- def __init__ (self , namespace , globals = None , owner = None , is_class = False ):
476
+ def __init__ (self , namespace , globals = None , owner = None , is_class = False , create_unions = False ):
463
477
super ().__init__ (namespace )
464
478
self .namespace = namespace
465
479
self .globals = globals
466
480
self .owner = owner
467
481
self .is_class = is_class
482
+ self .create_unions = create_unions
468
483
self .stringifiers = []
469
484
470
485
def __missing__ (self , key ):
@@ -569,7 +584,13 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
569
584
# that returns a bool and an defined set of attributes.
570
585
namespace = {** annotate .__builtins__ , ** annotate .__globals__ }
571
586
is_class = isinstance (owner , type )
572
- globals = _StringifierDict (namespace , annotate .__globals__ , owner , is_class )
587
+ globals = _StringifierDict (
588
+ namespace ,
589
+ annotate .__globals__ ,
590
+ owner ,
591
+ is_class ,
592
+ create_unions = True
593
+ )
573
594
if annotate .__closure__ :
574
595
freevars = annotate .__code__ .co_freevars
575
596
new_closure = []
0 commit comments