10000 [CI] Run `lintrunner` on generated `.pyi` stub files in CI by XuehaiPan · Pull Request #129887 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[CI] Run lintrunner on generated .pyi stub files in CI #129887

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

Closed
wants to merge 61 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
7bb5200
Update
XuehaiPan Jul 1, 2024
fdd52d4
Update
XuehaiPan Jul 1, 2024
0e8513d
Update
XuehaiPan Jul 1, 2024
18a6dac
Update
XuehaiPan Jul 1, 2024
dc69a86
Update
XuehaiPan Jul 1, 2024
c1d7eaa
Update
XuehaiPan Jul 1, 2024
2d3c43a
Update
XuehaiPan Jul 1, 2024
0149917
Update
XuehaiPan Jul 1, 2024
51e79a0
Update
XuehaiPan Jul 1, 2024
4278fe6
Update
XuehaiPan Jul 1, 2024
a5ccde1
Update
XuehaiPan Jul 1, 2024
27817ee
Update
XuehaiPan Jul 2, 2024
0ec869c
Update
XuehaiPan Jul 2, 2024
5da3cbf
Update
XuehaiPan Jul 2, 2024
2d351b0
Update
XuehaiPan Jul 2, 2024
8000 dcb8867
Update
XuehaiPan Jul 2, 2024
decc1c1
Update
XuehaiPan Jul 2, 2024
3cfe146
Update
XuehaiPan Jul 2, 2024
2794c02
Update
XuehaiPan Jul 3, 2024
9a9ba40
Update
XuehaiPan Jul 3, 2024
09cacc5
Update
XuehaiPan Jul 15, 2024
48b3487
Update
XuehaiPan Jul 17, 2024
4e71a71
Update
XuehaiPan Jul 20, 2024
0dbba8b
Update
XuehaiPan Jul 20, 2024
0d457d7
Update
XuehaiPan Jul 20, 2024
29751d0
Update
XuehaiPan Jul 20, 2024
8d6a9c2
Update
XuehaiPan Jul 20, 2024
6804049
Update
XuehaiPan Aug 1, 2024
07245e0
Update
XuehaiPan Aug 2, 2024
6c9905a
Update
XuehaiPan Aug 3, 2024
51e791e
Update
XuehaiPan Aug 12, 2024
e718002
Update
XuehaiPan Oct 11, 2024
9fb5f00
Update
XuehaiPan Oct 11, 2024
59cfddc
Update
XuehaiPan Oct 24, 2024
ab8119b
Update
XuehaiPan Oct 24, 2024
5501f5f
Update
XuehaiPan Nov 11, 2024
6217953
Update
XuehaiPan Dec 8, 2024
88b3e2e
Update
XuehaiPan Dec 21, 2024
3d52d20
Update
XuehaiPan Jan 13, 2025
664835b
Update
XuehaiPan Jan 13, 2025
53c5f31
Update
XuehaiPan Jan 13, 2025
10147fc
Update
XuehaiPan Feb 28, 2025
18f6502
Update
XuehaiPan Feb 28, 2025
99846a6
Update
XuehaiPan Feb 28, 2025
82c4175
Update
XuehaiPan Feb 28, 2025
ce11302
Update
XuehaiPan Mar 6, 2025
b66a6a6
Update
XuehaiPan Mar 13, 2025
ac54a14
Update
XuehaiPan Mar 13, 2025
cd35157
Update
XuehaiPan Mar 13, 2025
400fb6b
Update
XuehaiPan Mar 14, 2025
2e4e7a7
Update
XuehaiPan Mar 16, 2025
bd4ce92
Update
XuehaiPan Mar 16, 2025
8c3212d
Update
XuehaiPan Mar 16, 2025
6b66134
Update
XuehaiPan Mar 16, 2025
2cbc628
Update
XuehaiPan Mar 20, 2025
503cbc0
Update
XuehaiPan Mar 20, 2025
8a53453
Update
XuehaiPan Apr 3, 2025
75b88f7
Update
XuehaiPan Apr 4, 2025
3503bbd
Update
XuehaiPan Apr 4, 2025
10000
b058918
Update
XuehaiPan Apr 4, 2025
818450e
Update
XuehaiPan Apr 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/scripts/lintrunner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ python3 -m tools.pyi.gen_pyi \
--deprecated-functions-path "tools/autograd/deprecated.yaml"
python3 torch/utils/data/datapipes/gen_pyi.py

# Also check generated pyi files
find torch -name '*.pyi' -exec git add --force -- "{}" +

RC=0
# Run lintrunner on all files
if ! lintrunner --force-color --tee-json=lint.json ${ADDITIONAL_LINTRUNNER_ARGS} 2> /dev/null; then
Expand All @@ -41,6 +44,9 @@ if ! lintrunner --force-color --tee-json=lint.json ${ADDITIONAL_LINTRUNNER_ARGS}
RC=1
fi

# Unstage temporally added pyi files
find torch -name '*.pyi' -exec git restore --staged -- "{}" +

# Use jq to massage the JSON lint output into GitHub Actions workflow commands.
jq --raw-output \
'"::\(if .severity == "advice" or .severity == "disabled" then "warning" else .severity end) file=\(.path),line=\(.line),col=\(.char),title=\(.code) \(.name)::" + (.description | gsub("\\n"; "%0A"))' \
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ select = [
"__init__.py" = [
"F401",
]
"*.pyi" = [
"PYI011", # typed-argument-default-in-stub
"PYI021", # docstring-in-stub
"PYI053", # string-or-bytes-too-long
]
"functorch/notebooks/**" = [
"F401",
]
Expand Down
23 changes: 17 additions & 6 deletions tools/pyi/gen_pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import argparse
import collections
import importlib
import inspect
import sys
import textwrap
from typing import TYPE_CHECKING
from unittest.mock import Mock, patch
from warnings import warn
Expand Down Expand Up @@ -132,7 +134,7 @@ def should_bind_method(python_func: PythonSignatureNativeFunctionPair) -> bool:
"_bool | _int | slice | EllipsisType | Tensor | None" # not SupportsIndex!
)
_index_types = f"SupportsIndex | {_leaf_types} | _NestedSequence[{_leaf_types}]"
_index_type_def = f"_Index: TypeAlias = {_index_types}"
_index_type_def = f"_Index: TypeAlias = {_index_types} # fmt: skip"
INDICES = "indices: _Index | tuple[_Index, ...]"

blocklist = [
Expand Down Expand Up @@ -252,6 +254,11 @@ def sig_for_ops(opname: str) -> list[str]:
f"def {opname}(self, other: Tensor | Number | _complex) -> Tensor: ... # type: ignore[has-type]"
]
elif name in arithmetic_ops:
if name.startswith("i"):
# In-place binary-operation dunder methods, like `__iadd__`, should return `Self`
return [
f"def {opname}(self, other: Tensor | Number | _complex) -> Tensor: ... # noqa: PYI034"
]
return [f"def {opname}(self, other: Tensor | Number | _complex) -> Tensor: ..."]
elif name in logic_ops:
return [f"def {opname}(self, other: Tensor | _bool) -> Tensor: ..."]
Expand Down Expand Up @@ -669,12 +676,16 @@ def mock_add_docstr(func: Mock, docstr: str) -> None:


def add_docstr_to_hint(docstr: str, hint: str) -> str:
docstr = inspect.cleandoc(docstr).strip()
if "..." in hint: # function or method
assert hint.endswith("..."), f"Hint `{hint}` does not end with '...'"
hint = hint[:-3] # remove "..."
return "\n ".join([hint, 'r"""'] + docstr.split("\n") + ['"""', "..."])
else: # attribute or property
return f'{hint}\nr"""{docstr}"""\n'
hint = hint[:-3].rstrip() # remove "..."
content = hint + "\n" + textwrap.indent(f'r"""\n{docstr}\n"""', prefix=" ")
# Remove trailing whitespace on each line
return "\n".join(map(str.rstrip, content.splitlines())).rstrip()

# attribute or property
return f'{hint}\nr"""{docstr}"""'


def gen_pyi(
Expand Down Expand Up @@ -1557,7 +1568,7 @@ def replace_special_case(hint: str) -> str:
# Generate type signatures for legacy classes
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

legacy_storage_base_hints = ["class StorageBase(object): ..."]
legacy_storage_base_hints = ["class StorageBase: ..."]

legacy_class_hints = []
for c in (
Expand Down
15 changes: 3 additions & 12 deletions torch/_C/_VariableFunctions.pyi.in
A4B3
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
# ${generated_comment}
# mypy: disable-error-code="type-arg"
# mypy: allow-untyped-defs
# ruff: noqa: F401,PYI054

import builtins
from collections.abc import Sequence
from types import EllipsisType
from typing import (
Any,
Callable,
ContextManager,
Iterator,
Literal,
NamedTuple,
overload,
Sequence,
TypeVar,
)
from typing import Any, Callable, Literal, overload, TypeVar

import torch
from torch import (
Expand Down
Loading
Loading
0