-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
tests/fixtures/nonpublic/checker/default_collate_convert.py
This file contains 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,11 @@ | ||
from torch.utils.data import _utils | ||
batch = _utils.collate.default_collate(batch) | ||
|
||
from torch.utils.data._utils.collate import default_collate | ||
inputs, labels, video_idx = default_collate(inputs), default_collate(labels), default_collate(video_idx) | ||
|
||
from torch.utils.data._utils.collate import default_convert | ||
values = default_convert(values) | ||
|
||
import torch | ||
values = torch.utils.data._utils.collate.default_convert(values) |
This file contains 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,8 @@ | ||
2:9 TOR104 Use of non-public function `torch.utils.data._utils.collate.default_collate`, please use `torch.utils.data.dataloader.default_collate` instead | ||
4:1 TOR105 Import of non-public function `torch.utils.data._utils.collate.default_collate`, please use `torch.utils.data.dataloader.default_collate` instead | ||
5:29 TOR104 Use of non-public function `torch.utils.data._utils.collate.default_collate`, please use `torch.utils.data.dataloader.default_collate` instead | ||
5:54 TOR104 Use of non-public function `torch.utils.data._utils.collate.default_collate`, please use `torch.utils.data.dataloader.default_collate` instead | ||
5:79 TOR104 Use of non-public function `torch.utils.data._utils.collate.default_collate`, please use `torch.utils.data.dataloader.default_collate` instead | ||
7:1 TOR105 Import of non-public function `torch.utils.data._utils.collate.default_convert`, please use `torch.utils.data.dataloader.default_convert` instead | ||
8:10 TOR104 Use of non-public function `torch.utils.data._utils.collate.default_convert`, please use `torch.utils.data.dataloader.default_convert` instead | ||
11:10 TOR104 Use of non-public function `torch.utils.data._utils.collate.default_convert`, please use `torch.utils.data.dataloader.default_convert` instead |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,51 @@ | ||
from typing import Sequence | ||
|
||
import libcst as cst | ||
from ...common import TorchVisitor | ||
|
||
|
||
class TorchNonPublicAliasVisitor(TorchVisitor): | ||
""" | ||
Suggest to use public APIs instead of non-public aliases. | ||
Currently implemented for | ||
torch.utils.data._utils.collate.default_collate and | ||
torch.utils.data._utils.collate.default_convert, | ||
see https://github.com/pytorch/pytorch/pull/69862/files | ||
""" | ||
|
||
ERROR_CODE = ["TOR104", "TOR105"] | ||
|
||
# fmt: off | ||
ALIASES = { | ||
"torch.utils.data._utils.collate.default_collate": "torch.utils.data.dataloader.default_collate", # noqa: E501 | ||
"torch.utils.data._utils.collate.default_convert": "torch.utils.data.dataloader.default_convert", # noqa: E501 | ||
} | ||
# fmt: on | ||
|
||
def visit_Call(self, node): | ||
qualified_name = self.get_qualified_name_for_call(node) | ||
if qualified_name is None: | ||
return | ||
|
||
if qualified_name in self.ALIASES: | ||
public_name = self.ALIASES[qualified_name] | ||
error_code = self.ERROR_CODE[0] | ||
message = f"Use of non-public function `{qualified_name}`, please use `{public_name}` instead" # noqa: E501 | ||
self.add_violation(node, error_code=error_code, message=message) | ||
|
||
def visit_ImportFrom(self, node: cst.ImportFrom) -> None: | ||
if node.module is None: | ||
return | ||
|
||
module = cst.helpers.get_full_name_for_node(node.module) | ||
if not isinstance(node.names, Sequence): | ||
return | ||
|
||
for name in node.names: | ||
qualified_name = f"{module}.{name.name.value}" | ||
if qualified_name in self.ALIASES: | ||
public_name = self.ALIASES[qualified_name] | ||
error_code = self.ERROR_CODE[1] | ||
message = f"Import of non-public function `{qualified_name}`, please use `{public_name}` instead" # noqa: E501 | ||
self.add_violation(node, error_code=error_code, message=message) |