Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d810953

Browse filesBrowse files
committed
Make stringifiers create unions if create_unions is True
1 parent fba5efa commit d810953
Copy full SHA for d810953

File tree

Expand file treeCollapse file tree

1 file changed

+25
-4
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+25
-4
lines changed

‎Lib/annotationlib.py

Copy file name to clipboardExpand all lines: Lib/annotationlib.py
+25-4Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,19 @@ def binop(self, other):
392392
__mod__ = _make_binop(ast.Mod())
393393
__lshift__ = _make_binop(ast.LShift())
394394
__rshift__ = _make_binop(ast.RShift())
395-
__or__ = _make_binop(ast.BitOr())
396395
__xor__ = _make_binop(ast.BitXor())
397396
__and__ = _make_binop(ast.BitAnd())
398397
__floordiv__ = _make_binop(ast.FloorDiv())
399398
__pow__ = _make_binop(ast.Pow())
400399

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+
401408
del _make_binop
402409

403410
def _make_rbinop(op: ast.AST):
@@ -416,12 +423,19 @@ def rbinop(self, other):
416423
__rmod__ = _make_rbinop(ast.Mod())
417424
__rlshift__ = _make_rbinop(ast.LShift())
418425
__rrshift__ = _make_rbinop(ast.RShift())
419-
__ror__ = _make_rbinop(ast.BitOr())
420426
__rxor__ = _make_rbinop(ast.BitXor())
421427
__rand__ = _make_rbinop(ast.BitAnd())
422428
__rfloordiv__ = _make_rbinop(ast.FloorDiv())
423429
__rpow__ = _make_rbinop(ast.Pow())
424430

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+
425439
del _make_rbinop
426440

427441
def _make_compare(op):
@@ -459,12 +473,13 @@ def unary_op(self):
459473

460474

461475
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):
463477
super().__init__(namespace)
464478
self.namespace = namespace
465479
self.globals = globals
466480
self.owner = owner
467481
self.is_class = is_class
482+
self.create_unions = create_unions
468483
self.stringifiers = []
469484

470485
def __missing__(self, key):
@@ -569,7 +584,13 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
569584
# that returns a bool and an defined set of attributes.
570585
namespace = {**annotate.__builtins__, **annotate.__globals__}
571586
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+
)
573594
if annotate.__closure__:
574595
freevars = annotate.__code__.co_freevars
575596
new_closure = []

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.