-
Notifications
You must be signed in to change notification settings - Fork 220
Add codemod to convert typing.Union
to |
#1270
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
# | ||
# pyre-strict | ||
|
||
import libcst as cst | ||
from libcst.codemod import VisitorBasedCodemodCommand | ||
from libcst.codemod.visitors import RemoveImportsVisitor | ||
from libcst.metadata import QualifiedName, QualifiedNameProvider, QualifiedNameSource | ||
|
||
|
||
class ConvertUnionToOrCommand(VisitorBasedCodemodCommand): | ||
DESCRIPTION: str = "Convert `Union[A, B]` to `A | B` in Python 3.10+" | ||
|
||
METADATA_DEPENDENCIES = (QualifiedNameProvider,) | ||
|
||
def leave_Subscript( | ||
self, original_node: cst.Subscript, updated_node: cst.Subscript | ||
) -> cst.BaseExpression: | ||
""" | ||
Given a subscript, check if it's a Union - if so, either flatten the members | ||
into a nested BitOr (if multiple members) or unwrap the type (if only one member). | ||
""" | ||
if QualifiedNameProvider.has_name( | ||
self, | ||
original_node, | ||
QualifiedName(name="typing.Union", source=QualifiedNameSource.IMPORT), | ||
): | ||
types = [ | ||
cst.ensure_type( | ||
cst.ensure_type(s, cst.SubscriptElement).slice, cst.Index | ||
).value | ||
for s in updated_node.slice | ||
] | ||
if len(types) == 1: | ||
replacement = types[0] | ||
else: | ||
replacement = cst.BinaryOperation( | ||
left=types[0], right=types[1], operator=cst.BitOr() | ||
) | ||
for type_ in types[2:]: | ||
replacement = cst.BinaryOperation( | ||
left=replacement, right=type_, operator=cst.BitOr() | ||
) | ||
return replacement | ||
return updated_node | ||
|
||
def leave_Module( | ||
self, original_node: cst.Module, updated_node: cst.Module | ||
) -> cst.Module: | ||
RemoveImportsVisitor.remove_unused_import( | ||
self.context, module="typing", obj="Union" | ||
) | ||
return updated_node |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
# | ||
# pyre-strict | ||
|
||
from libcst.codemod import CodemodTest | ||
from libcst.codemod.commands.convert_union_to_or import ConvertUnionToOrCommand | ||
|
||
|
||
class TestConvertUnionToOrCommand(CodemodTest): | ||
TRANSFORM = ConvertUnionToOrCommand | ||
|
||
def test_simple_union(self) -> None: | ||
before = """ | ||
from typing import Union | ||
x: Union[int, str] | ||
""" | ||
after = """ | ||
x: int | str | ||
""" | ||
self.assertCodemod(before, after) | ||
|
||
def test_nested_union(self) -> None: | ||
before = """ | ||
from typing import Union | ||
x: Union[int, Union[str, float]] | ||
""" | ||
after = """ | ||
x: int | str | float | ||
""" | ||
self.assertCodemod(before, after) | ||
|
||
def test_single_type_union(self) -> None: | ||
before = """ | ||
from typing import Union | ||
x: Union[int] | ||
""" | ||
after = """ | ||
x: int | ||
""" | ||
self.assertCodemod(before, after) | ||
|
||
def test_union_with_alias(self) -> None: | ||
before = """ | ||
import typing as t | ||
x: t.Union[int, str] | ||
""" | ||
after = """ | ||
import typing as t | ||
x: int | str | ||
""" | ||
self.assertCodemod(before, after) | ||
|
||
def test_union_with_unused_import(self) -> None: | ||
before = """ | ||
from typing import Union, List | ||
x: Union[int, str] | ||
""" | ||
after = """ | ||
from typing import List | ||
x: int | str | ||
""" | ||
self.assertCodemod(before, after) | ||
|
||
def test_union_no_import(self) -> None: | ||
before = """ | ||
x: Union[int, str] | ||
""" | ||
after = """ | ||
x: Union[int, str] | ||
""" | ||
self.assertCodemod(before, after) | ||
|
||
def test_union_in_function(self) -> None: | ||
before = """ | ||
from typing import Union | ||
def foo(x: Union[int, str]) -> Union[float, None]: | ||
... | ||
""" | ||
after = """ | ||
def foo(x: int | str) -> float | None: | ||
... | ||
""" | ||
self.assertCodemod(before, after) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I generally prefer early return in these cases