Skip to content

Commit

Permalink
Merge pull request #767 from UnknownPlatypus/fix-binor
Browse files Browse the repository at this point in the history
Add support for BinOr typing union syntax
  • Loading branch information
asottile authored Dec 6, 2022
2 parents a19e733 + 8548b04 commit 9969644
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyupgrade/_plugins/typing_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def _unparse(node: ast.expr) -> str:
return '[{}]'.format(', '.join(_unparse(elt) for elt in node.elts))
elif isinstance(node, ast.NameConstant):
return repr(node.value)
elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitOr):
return f'{_unparse(node.left)} | {_unparse(node.right)}'
else:
raise NotImplementedError(ast.dump(node))

Expand Down
20 changes: 20 additions & 0 deletions tests/features/typing_classes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ def test_typing_named_tuple_noop(s):
id='preserves comments without alignment',
),
pytest.param(
'from typing import NamedTuple\n'
'Foo = NamedTuple("Foo", [("union", str | None)])',
'from typing import NamedTuple\n'
'class Foo(NamedTuple):\n'
' union: str | None',
id='BitOr unparse error',
),
),
)
def test_fix_typing_named_tuple(s, expected):
Expand Down Expand Up @@ -393,6 +403,16 @@ def test_typing_typed_dict_noop(s):
id='right after a dedent',
),
pytest.param(
'from typing import TypedDict\n'
'Foo = TypedDict("Foo", {"union": str | int | None})',
'from typing import TypedDict\n'
'class Foo(TypedDict):\n'
' union: str | int | None',
id='BitOr unparse error',
),
),
)
def test_typing_typed_dict(s, expected):
Expand Down

0 comments on commit 9969644

Please sign in to comment.