mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-21 05:34:18 +08:00
Changes: - => this PR: #95200 1. Recognize `.py.in` and `.pyi.in` files as Python in VS Code for a better development experience. 2. Fix deep setting merge in `tools/vscode_settings.py`. - #95267 3. Use `Namedtuple` rather than `namedtuple + __annotations__` for `torch.nn.utils.rnn.PackedSequence_`: `namedtuple + __annotations__`: ```python PackedSequence_ = namedtuple('PackedSequence_', ['data', 'batch_sizes', 'sorted_indices', 'unsorted_indices']) # type annotation for PackedSequence_ to make it compatible with TorchScript PackedSequence_.__annotations__ = {'data': torch.Tensor, 'batch_sizes': torch.Tensor, 'sorted_indices': Optional[torch.Tensor], 'unsorted_indices': Optional[torch.Tensor]} ``` `Namedtuple`: Python 3.6+ ```python class PackedSequence_(NamedTuple): data: torch.Tensor batch_sizes: torch.Tensor sorted_indices: Optional[torch.Tensor] unsorted_indices: Optional[torch.Tensor] ``` - #95268 4. Sort import statements and remove unnecessary imports in `.pyi`, `.pyi.in` files. 5. Format `.pyi`, `.pyi.in` files and remove unnecessary ellipsis `...` in type stubs. Pull Request resolved: https://github.com/pytorch/pytorch/pull/95200 Approved by: https://github.com/janeyx99
66 lines
1.8 KiB
Python
Executable File
66 lines
1.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
from pathlib import Path
|
|
|
|
try:
|
|
# VS Code settings allow comments and trailing commas, which are not valid JSON.
|
|
import json5 as json # type: ignore[import]
|
|
|
|
HAS_JSON5 = True
|
|
except ImportError:
|
|
import json # type: ignore[no-redef]
|
|
|
|
HAS_JSON5 = False
|
|
|
|
|
|
ROOT_FOLDER = Path(__file__).absolute().parent.parent
|
|
VSCODE_FOLDER = ROOT_FOLDER / ".vscode"
|
|
RECOMMENDED_SETTINGS = VSCODE_FOLDER / "settings_recommended.json"
|
|
SETTINGS = VSCODE_FOLDER / "settings.json"
|
|
|
|
|
|
# settings can be nested, so we need to recursively update the settings.
|
|
def deep_update(d: dict, u: dict) -> dict: # type: ignore[type-arg]
|
|
for k, v in u.items():
|
|
if isinstance(v, dict):
|
|
d[k] = deep_update(d.get(k, {}), v)
|
|
elif isinstance(v, list):
|
|
d[k] = d.get(k, []) + v
|
|
else:
|
|
d[k] = v
|
|
return d
|
|
|
|
|
|
def main() -> None:
|
|
recommended_settings = json.loads(RECOMMENDED_SETTINGS.read_text())
|
|
try:
|
|
current_settings_text = SETTINGS.read_text()
|
|
except FileNotFoundError:
|
|
current_settings_text = "{}"
|
|
|
|
try:
|
|
current_settings = json.loads(current_settings_text)
|
|
except ValueError as ex: # json.JSONDecodeError is a subclass of ValueError
|
|
if HAS_JSON5:
|
|
raise SystemExit("Failed to parse .vscode/settings.json.") from ex
|
|
raise SystemExit(
|
|
"Failed to parse .vscode/settings.json. "
|
|
"Maybe it contains comments or trailing commas. "
|
|
"Try `pip install json5` to install an extended JSON parser."
|
|
) from ex
|
|
|
|
settings = deep_update(current_settings, recommended_settings)
|
|
|
|
SETTINGS.write_text(
|
|
json.dumps(
|
|
settings,
|
|
indent=4,
|
|
)
|
|
+ "\n", # add a trailing newline
|
|
encoding="utf-8",
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|