Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for BinOr typing union syntax #767

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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