-
Notifications
You must be signed in to change notification settings - Fork 25.4k
[1/3] Recognize .py.in
and .pyi.in
files as Python in VS Code
#95200
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
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/95200
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 391b2f5: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
1e997b5
to
f10b578
Compare
f10b578
to
f2117c1
Compare
.pyi
Python stub files and recognize .py.in
and .pyi.in
files as Python in VS Code
torch/_C/_distributed_rpc.pyi
Outdated
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.
We should not use mutable variables as function defaults.
Ref: dangerous-default-value / W0102
Correct code:
def func(
...,
device_maps: Optional[Dict[str, Dict[torch.device, torch.device]]] = None,
devices: Optional[List[torch.device]] = None
):
if device_maps is None:
device_maps = {}
if devices is None:
devices = []
...
fa34621
to
5013d55
Compare
Thanks for the PR! Also I'm not sure what the vscode_settings.py file does, how to test these changes? |
.pyi
Python stub files and recognize .py.in
and .pyi.in
files as Python in VS Code.pyi
Python stub files and recognize .py.in
and .pyi.in
files as Python in VS Code
5013d55
to
7173060
Compare
I split this into 3 PRs. The
You could run: ./tools/vscode_settings.py or python3 ./tools/vscode_settings.py It will update the workspace VS Code settings For example: current file (.vscode/settings.json): {
"[c]": {
"editor.insertSpaces": true,
"editor.tabSize": 2
},
"[cpp]": {
"editor.insertSpaces": true,
"editor.tabSize": 2,
"editor.defaultFormatter": "ms-vscode.cpptools"
},
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"files.associations": {
"aten/src/ATen/ATenConfig.cmake.in": "cmake"
},
"java.autobuild.enabled": false,
"java.compile.nullAnalysis.mode": "disabled",
} after running {
"[c]": {
"editor.insertSpaces": true,
"editor.tabSize": 2,
},
"[cpp]": {
"editor.insertSpaces": true,
"editor.tabSize": 2,
"editor.defaultFormatter": "ms-vscode.cpptools",
},
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
+ "editor.tabSize": 4,
},
"files.associations": {
"aten/src/ATen/ATenConfig.cmake.in": "cmake",
+ "*.py.in": "python",
+ "*.pyi.in": "python",
},
"java.autobuild.enabled": false,
"java.compile.nullAnalysis.mode": "disabled",
+ "files.eol": "\n",
+ "files.insertFinalNewline": true,
+ "files.trimFinalNewlines": true,
+ "files.trimTrailingWhitespace": true,
+ "python.formatting.provider": "none",
+ "python.linting.enabled": true,
+ "python.linting.flake8Enabled": true,
} |
7173060
to
e7ded55
Compare
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.
Thanks for the change! Looks fine overall, just had some minor points :D
tools/vscode_settings.py
Outdated
current_settings = json.loads(current_settings_text) | ||
except ValueError: # json.JSONDecodeError is a subclass of ValueError | ||
if HAS_JSON5: | ||
raise SystemExit("Failed to parse .vscode/settings.json.") |
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.
Why SystemExit and not RuntimeError?
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.
Since tools/vscode_settings.py
is a script to run in the shell. It is not meant to be imported, so its functions will not be put in try-execpt
blocks in other scripts.
Raising SystemExit
is equivalent to printing an error message and exiting with code 1:
$ pip3 uninstall json5
$ ./tools/vscode_settings.py
Failed to parse .vscode/settings.json. Maybe it contains comments or trailing commas. Try `pip install json5` to install an extended JSON parser.
$ echo $?
1
Using RuntimeError
:
$ pip3 uninstall json5
$ ./tools/vscode_settings.py
Traceback (most recent call last):
File "/home/PanXuehai/Projects/pytorch/./tools/vscode_settings.py", line 42, in main
current_settings = json.loads(current_settings_text)
File "/home/PanXuehai/Miniconda3/envs/pytorch-dev/lib/python3.10/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/home/PanXuehai/Miniconda3/envs/pytorch-dev/lib/python3.10/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/home/PanXuehai/Miniconda3/envs/pytorch-dev/lib/python3.10/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 5 column 5 (char 85)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/PanXuehai/Projects/pytorch/./tools/vscode_settings.py", line 65, in <module>
main()
File "/home/PanXuehai/Projects/pytorch/./tools/vscode_settings.py", line 46, in main
raise RuntimeError(
RuntimeError: Failed to parse .vscode/settings.json. Maybe it contains comments or trailing commas. Try `pip install json5` to install an extended JSON parser.
$ echo $?
1
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.
Ah, thank you for explaining!
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
.pyi
Python stub files and recognize .py.in
and .pyi.in
files as Python in VS Code.py.in
and .pyi.in
files as Python in VS Code
…e annotated `NamedTuple` (#95267) Changes: - #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`. - => this PR: #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: #95267 Approved by: https://github.com/janeyx99
) Changes: - #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] ``` - => this PR: #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: #95268 Approved by: https://github.com/huydhn
…5200) 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: pytorch/pytorch#95200 Approved by: https://github.com/janeyx99
…5200) 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: pytorch/pytorch#95200 Approved by: https://github.com/janeyx99
…5200) 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: pytorch/pytorch#95200 Approved by: https://github.com/janeyx99
…Code (pytorch#95200)" This reverts commit a46e550.
Changes:
.py.in
and.pyi.in
files as Python in VS Code #95200.py.in
and.pyi.in
files as Python in VS Code for a better development experience.tools/vscode_settings.py
..pyi
Python stub files: Prettifyrnn.py
by using type annotatedNamedTuple
#95267Use
Namedtuple
rather thannamedtuple + __annotations__
fortorch.nn.utils.rnn.PackedSequence_
:namedtuple + __annotations__
:Namedtuple
: Python 3.6+.pyi
Python stub files and enable'UFMT'
linter #95268.pyi
,.pyi.in
files..pyi
,.pyi.in
files and remove unnecessary ellipsis...
in type stubs.