Skip to content

Commit 8548b04

Browse files
Add support for BinOr typing union syntax
1 parent a19e733 commit 8548b04

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

pyupgrade/_plugins/typing_classes.py

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def _unparse(node: ast.expr) -> str:
4646
return '[{}]'.format(', '.join(_unparse(elt) for elt in node.elts))
4747
elif isinstance(node, ast.NameConstant):
4848
return repr(node.value)
49+
elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitOr):
50+
return f'{_unparse(node.left)} | {_unparse(node.right)}'
4951
else:
5052
raise NotImplementedError(ast.dump(node))
5153

tests/features/typing_classes_test.py

+20
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,16 @@ def test_typing_named_tuple_noop(s):
210210
211211
id='preserves comments without alignment',
212212
),
213+
pytest.param(
214+
'from typing import NamedTuple\n'
215+
'Foo = NamedTuple("Foo", [("union", str | None)])',
216+
217+
'from typing import NamedTuple\n'
218+
'class Foo(NamedTuple):\n'
219+
' union: str | None',
220+
221+
id='BitOr unparse error',
222+
),
213223
),
214224
)
215225
def test_fix_typing_named_tuple(s, expected):
@@ -393,6 +403,16 @@ def test_typing_typed_dict_noop(s):
393403
394404
id='right after a dedent',
395405
),
406+
pytest.param(
407+
'from typing import TypedDict\n'
408+
'Foo = TypedDict("Foo", {"union": str | int | None})',
409+
410+
'from typing import TypedDict\n'
411+
'class Foo(TypedDict):\n'
412+
' union: str | int | None',
413+
414+
id='BitOr unparse error',
415+
),
396416
),
397417
)
398418
def test_typing_typed_dict(s, expected):

0 commit comments

Comments
 (0)