Skip to content

Commit 9c959a7

Browse files
AvasamAlexWaygoodpre-commit-ci[bot]
authored
Enable Ruff D (pydocstyle) with pep257 convention (#13326)
Co-authored-by: Alex Waygood <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a151457 commit 9c959a7

File tree

12 files changed

+34
-22
lines changed

12 files changed

+34
-22
lines changed

lib/ts_utils/metadata.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def is_obsolete(self) -> bool:
184184

185185

186186
class NoSuchStubError(ValueError):
187-
"""Raise NoSuchStubError to indicate that a stubs/{distribution} directory doesn't exist"""
187+
"""Raise NoSuchStubError to indicate that a stubs/{distribution} directory doesn't exist."""
188188

189189

190190
@cache
@@ -302,9 +302,10 @@ def read_metadata(distribution: str) -> StubMetadata:
302302

303303

304304
def update_metadata(distribution: str, **new_values: object) -> tomlkit.TOMLDocument:
305-
"""Updates a distribution's METADATA.toml.
305+
"""Update a distribution's METADATA.toml.
306306
307-
Return the updated TOML dictionary for use without having to open the file separately."""
307+
Return the updated TOML dictionary for use without having to open the file separately.
308+
"""
308309
path = metadata_path(distribution)
309310
try:
310311
with path.open("rb") as file:

lib/ts_utils/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def venv_python(venv_dir: Path) -> Path:
9292
@cache
9393
def parse_requirements() -> Mapping[str, Requirement]:
9494
"""Return a dictionary of requirements from the requirements file."""
95-
9695
with REQUIREMENTS_PATH.open(encoding="UTF-8") as requirements_file:
9796
stripped_lines = map(strip_comments, requirements_file)
9897
stripped_more = [li for li in stripped_lines if not li.startswith("-")]

pyproject.toml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ external = ["F821", "NQA", "Y"]
3939
select = [
4040
"ARG", # flake8-unused-arguments
4141
"B", # flake8-bugbear
42+
"D", # pydocstyle
4243
"EXE", # flake8-executable
4344
"FA", # flake8-future-annotations
4445
"I", # isort
@@ -108,11 +109,15 @@ ignore = [
108109
###
109110
# Rules we don't want or don't agree with
110111
###
111-
# Slower and more verbose https://github.com/astral-sh/ruff/issues/7871
112-
"UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)`
112+
# We're not a library, no need to document everything
113+
"D1", # Missing docstring in ...
114+
# Doesn't support split "summary line"
115+
"D205", # 1 blank line required between summary line and description
113116
# Used for direct, non-subclass type comparison, for example: `type(val) is str`
114117
# see https://github.com/astral-sh/ruff/issues/6465
115118
"E721", # Do not compare types, use `isinstance()`
119+
# Slower and more verbose https://github.com/astral-sh/ruff/issues/7871
120+
"UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)`
116121
###
117122
# False-positives, but already checked by type-checkers
118123
###
@@ -139,11 +144,16 @@ ignore = [
139144
"RUF022",
140145
"RUF023",
141146
]
142-
# See comment on black's force-exclude config above
143147
"*_pb2.pyi" = [
148+
# Leave the docstrings as-is, matching source
149+
"D", # pydocstyle
150+
# See comment on black's force-exclude config above
144151
"E501", # Line too long
145152
]
146153

154+
[tool.ruff.lint.pydocstyle]
155+
convention = "pep257" # https://docs.astral.sh/ruff/settings/#lint_pydocstyle_convention
156+
147157
[tool.ruff.lint.isort]
148158
split-on-trailing-comma = false
149159
combine-as-imports = true

scripts/sync_protobuf/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def extract_archive(archive_path: StrPath, destination: StrPath) -> None:
3333
def run_protoc(
3434
proto_paths: Iterable[StrPath], mypy_out: StrPath, proto_globs: Iterable[str], cwd: StrOrBytesPath | None = None
3535
) -> str:
36-
"""TODO: Describe parameters and return"""
36+
"""TODO: Describe parameters and return."""
3737
protoc_version = (
3838
subprocess.run([sys.executable, "-m", "grpc_tools.protoc", "--version"], capture_output=True).stdout.decode().strip()
3939
)

scripts/sync_protobuf/google_protobuf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333

3434
def extract_python_version(file_path: Path) -> str:
35-
"""Extract the Python version from https://github.com/protocolbuffers/protobuf/blob/main/version.json"""
35+
"""Extract the Python version from https://github.com/protocolbuffers/protobuf/blob/main/version.json ."""
3636
with open(file_path) as file:
3737
data: dict[str, Any] = json.load(file)
3838
# The root key will be the protobuf source code version
@@ -45,7 +45,7 @@ def extract_proto_file_paths(temp_dir: Path) -> list[str]:
4545
"""
4646
Roughly reproduce the subset of .proto files on the public interface
4747
as described in py_proto_library calls in
48-
https://github.com/protocolbuffers/protobuf/blob/main/python/dist/BUILD.bazel
48+
https://github.com/protocolbuffers/protobuf/blob/main/python/dist/BUILD.bazel .
4949
"""
5050
with open(temp_dir / EXTRACTED_PACKAGE_DIR / "python" / "dist" / "BUILD.bazel") as file:
5151
matched_lines = filter(None, (re.search(PROTO_FILE_PATTERN, line) for line in file))

scripts/sync_protobuf/s2clientprotocol.py

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

3131

3232
def extract_python_version(file_path: Path) -> str:
33-
"""Extract Python version from s2clientprotocol's build file"""
33+
"""Extract Python version from s2clientprotocol's build file."""
3434
match = re.search(VERSION_PATTERN, file_path.read_text())
3535
assert match
3636
return match.group(1)

scripts/sync_protobuf/tensorflow.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@
5757
def move_tree(source: Path, destination: Path) -> None:
5858
"""Move directory and merge if destination already exists.
5959
60-
Can't use shutil.move because it can't merge existing directories."""
60+
Can't use shutil.move because it can't merge existing directories.
61+
"""
6162
print(f"Moving '{source}' to '{destination}'")
6263
shutil.copytree(source, destination, dirs_exist_ok=True)
6364
shutil.rmtree(source)
6465

6566

6667
def post_creation() -> None:
67-
"""Move third-party and fix imports"""
68+
"""Move third-party and fix imports."""
6869
print()
6970
move_tree(STUBS_FOLDER / "tsl", STUBS_FOLDER / "tensorflow" / "tsl")
7071
move_tree(STUBS_FOLDER / "xla", STUBS_FOLDER / "tensorflow" / "compiler" / "xla")

tests/mypy_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class CommandLineArgs:
6262

6363

6464
def valid_path(cmd_arg: str) -> Path:
65-
"""Helper function for argument-parsing"""
65+
"""Parse a CLI argument that is intended to point to a valid typeshed path."""
6666
path = Path(cmd_arg)
6767
if not path.exists():
6868
raise argparse.ArgumentTypeError(f'"{path}" does not exist in typeshed!')
@@ -72,7 +72,10 @@ def valid_path(cmd_arg: str) -> Path:
7272

7373

7474
def remove_dev_suffix(version: str) -> str:
75-
"""Helper function for argument-parsing"""
75+
"""Remove the `-dev` suffix from a version string.
76+
77+
This is a helper function for argument-parsing.
78+
"""
7679
if version.endswith("-dev"):
7780
return version[: -len("-dev")]
7881
return version
@@ -303,7 +306,6 @@ def test_third_party_distribution(
303306
Return a tuple, where the first element indicates mypy's return code
304307
and the second element is the number of checked files.
305308
"""
306-
307309
files: list[Path] = []
308310
configurations: list[MypyDistConf] = []
309311
seen_dists: set[str] = set()

tests/pytype_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def create_parser() -> argparse.ArgumentParser:
7474

7575

7676
def run_pytype(*, filename: str, python_version: str, missing_modules: Iterable[str]) -> str | None:
77-
"""Runs pytype, returning the stderr if any."""
77+
"""Run pytype, returning the stderr if any."""
7878
if python_version not in _LOADERS:
7979
options = pytype_config.Options.create("", parse_pyi=True, python_version=python_version)
8080
# For simplicity, pretends missing modules are part of the stdlib.
@@ -107,7 +107,7 @@ def _get_relative(filename: str) -> str:
107107

108108

109109
def _get_module_name(filename: str) -> str:
110-
"""Converts a filename {subdir}/m.n/module/foo to module.foo."""
110+
"""Convert a filename {subdir}/m.n/module/foo to module.foo."""
111111
parts = _get_relative(filename).split(os.path.sep)
112112
if parts[0] == "stdlib":
113113
module_parts = parts[1:]

tests/regr_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444

4545

4646
def distribution_with_test_cases(distribution_name: str) -> DistributionTests:
47-
"""Helper function for argument-parsing."""
48-
47+
"""Parse a CLI argument that is intended to be to a valid typeshed distribution."""
4948
try:
5049
return distribution_info(distribution_name)
5150
except RuntimeError as exc:

0 commit comments

Comments
 (0)