[torchgen] Refactor torchgen.utils.FileManager to accept pathlib.Path (#150726)

This PR allows `FileManager` to accept `pathlib.Path` as arguments while keeping the original `str` path support.

This allows us to simplify the code such as:

1. `os.path.join(..., ...)` with `Path.__floordiv__(..., ...)`.

95a5958db4/torchgen/utils.py (L155)

95a5958db4/torchgen/utils.py (L176)

2. `os.path.basename(...)` with `Path(...).name`.
 95a5958db4/torchgen/utils.py (L161)

3. Manual file extension split with `Path(...).with_stem(new_stem)`

95a5958db4/torchgen/utils.py (L241-L256)

------

Pull Request resolved: https://github.com/pytorch/pytorch/pull/150726
Approved by: https://github.com/aorenste
This commit is contained in:
Xuehai Pan
2025-05-15 01:40:19 +08:00
committed by PyTorch MergeBot
parent 881a598a1e
commit 014726d9d3
12 changed files with 110 additions and 74 deletions

View File

@ -9,6 +9,7 @@ from collections import defaultdict, namedtuple, OrderedDict
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Callable, Literal, TYPE_CHECKING, TypeVar
from typing_extensions import assert_never
import yaml
@ -84,7 +85,6 @@ from torchgen.native_function_generation import (
)
from torchgen.selective_build.selector import SelectiveBuilder
from torchgen.utils import (
assert_never,
concatMap,
context,
FileManager,