Skip to content

Commit b6bdb67

Browse files
XuehaiPanpytorchmergebot
authored andcommitted
[BE][Easy] use pathlib.Path instead of dirname / ".." / pardir (pytorch#129374)
Changes by apply order: 1. Replace all `".."` and `os.pardir` usage with `os.path.dirname(...)`. 2. Replace nested `os.path.dirname(os.path.dirname(...))` call with `str(Path(...).parent.parent)`. 3. Reorder `.absolute()` ~/ `.resolve()`~ and `.parent`: always resolve the path first. `.parent{...}.absolute()` -> `.absolute().parent{...}` 4. Replace chained `.parent x N` with `.parents[${N - 1}]`: the code is easier to read (see 5.) `.parent.parent.parent.parent` -> `.parents[3]` 5. ~Replace `.parents[${N - 1}]` with `.parents[${N} - 1]`: the code is easier to read and does not introduce any runtime overhead.~ ~`.parents[3]` -> `.parents[4 - 1]`~ 6. ~Replace `.parents[2 - 1]` with `.parent.parent`: because the code is shorter and easier to read.~ Pull Request resolved: pytorch#129374 Approved by: https://github.com/justinchuby, https://github.com/malfet
1 parent 7101b8c commit b6bdb67

File tree

62 files changed

+120
-128
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+120
-128
lines changed

.circleci/codegen_validation/normalize_yaml_fragment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
# Need to import modules that lie on an upward-relative path
10-
sys.path.append(os.path.join(sys.path[0], ".."))
10+
sys.path.append(os.path.dirname(sys.path[0]))
1111

1212
import cimodel.lib.miniyaml as miniyaml
1313

.github/scripts/delete_old_branches.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
if not TOKEN:
2323
raise Exception("GITHUB_TOKEN is not set") # noqa: TRY002
2424

25-
REPO_ROOT = Path(__file__).parent.parent.parent
25+
REPO_ROOT = Path(__file__).parents[2]
2626

2727
# Query for all PRs instead of just closed/merged because it's faster
2828
GRAPHQL_ALL_PRS_BY_UPDATED_AT = """

.github/scripts/ensure_actions_will_cancel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import yaml
77

88

9-
REPO_ROOT = Path(__file__).resolve().parent.parent.parent
9+
REPO_ROOT = Path(__file__).resolve().parents[2]
1010
WORKFLOWS = REPO_ROOT / ".github" / "workflows"
1111
EXPECTED_GROUP_PREFIX = (
1212
"${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}"

.github/scripts/generate_binary_build_matrix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def get_nccl_submodule_version() -> str:
9494
from pathlib import Path
9595

9696
nccl_version_mk = (
97-
Path(__file__).absolute().parent.parent.parent
97+
Path(__file__).absolute().parents[2]
9898
/ "third_party"
9999
/ "nccl"
100100
/ "nccl"

.github/scripts/gitutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def get_git_remote_name() -> str:
3232
def get_git_repo_dir() -> str:
3333
from pathlib import Path
3434

35-
return os.getenv("GIT_REPO_DIR", str(Path(__file__).resolve().parent.parent.parent))
35+
return os.getenv("GIT_REPO_DIR", str(Path(__file__).resolve().parents[2]))
3636

3737

3838
def fuzzy_list_to_dict(items: List[Tuple[str, str]]) -> Dict[str, List[str]]:

.github/scripts/lint_native_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def fn(base: str) -> str:
2626
return str(base / Path("aten/src/ATen/native/native_functions.yaml"))
2727

2828

29-
with open(Path(__file__).parent.parent.parent / fn(".")) as f:
29+
with open(Path(__file__).parents[2] / fn(".")) as f:
3030
contents = f.read()
3131

3232
yaml = ruamel.yaml.YAML() # type: ignore[attr-defined]

.github/scripts/test_gitutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def foo(x: int, y: int) -> int:
6868

6969
class TestGitRepo(TestCase):
7070
def setUp(self) -> None:
71-
repo_dir = BASE_DIR.parent.parent.absolute()
71+
repo_dir = BASE_DIR.absolute().parent.parent
7272
if not (repo_dir / ".git").is_dir():
7373
raise SkipTest(
7474
"Can't find git directory, make sure to run this test on real repo checkout"

benchmarks/dynamo/ci_expected_accuracy/update_expected.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"c1cdfadc-6bb2-4a91-bbf9-3d19e1981cd4/run?format=JSON"
7272
)
7373
CSV_LINTER = str(
74-
Path(__file__).absolute().parent.parent.parent.parent
74+
Path(__file__).absolute().parents[3]
7575
/ "tools/linter/adapters/no_merge_conflict_csv_linter.py"
7676
)
7777

docs/source/scripts/build_opsets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
from torchgen.gen import parse_native_yaml
88

99

10-
ROOT = Path(__file__).absolute().parent.parent.parent.parent
11-
NATIVE_FUNCTION_YAML_PATH = ROOT / Path("aten/src/ATen/native/native_functions.yaml")
12-
TAGS_YAML_PATH = ROOT / Path("aten/src/ATen/native/tags.yaml")
10+
ROOT = Path(__file__).absolute().parents[3]
11+
NATIVE_FUNCTION_YAML_PATH = ROOT / "aten/src/ATen/native/native_functions.yaml"
12+
TAGS_YAML_PATH = ROOT / "aten/src/ATen/native/tags.yaml"
1313

1414
BUILD_DIR = "build/ir"
1515
ATEN_OPS_CSV_FILE = "aten_ops.csv"

docs/source/scripts/build_quantization_configs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
# Create a directory for the images, if it doesn't exist
1717
QUANTIZATION_BACKEND_CONFIG_IMAGE_PATH = os.path.join(
18-
os.path.realpath(os.path.join(__file__, "..")), "quantization_backend_configs"
18+
os.path.realpath(os.path.dirname(__file__)), "quantization_backend_configs"
1919
)
2020

2121
if not os.path.exists(QUANTIZATION_BACKEND_CONFIG_IMAGE_PATH):

0 commit comments

Comments
 (0)