Skip to content

Commit

Permalink
[lint] run mypy on the utils/ directory (#12090)
Browse files Browse the repository at this point in the history
Co-authored-by: daniel.eades <[email protected]>
  • Loading branch information
danieleades and daniel.eades committed Mar 15, 2024
1 parent 7bd9c59 commit 768cf5e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ exclude = [
]

[tool.mypy]
files = ["sphinx"]
files = ["sphinx", "utils"]
check_untyped_defs = true
disallow_incomplete_defs = true
python_version = "3.9"
Expand Down
26 changes: 13 additions & 13 deletions utils/babel_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,16 @@ def _get_logger():

os.chdir(ROOT)
if action == "extract":
raise SystemExit(run_extract())
if action == "update":
raise SystemExit(run_update())
if action == "compile":
raise SystemExit(run_compile())
if action == "all":
exit_code = run_extract()
if exit_code:
raise SystemExit(exit_code)
exit_code = run_update()
if exit_code:
raise SystemExit(exit_code)
raise SystemExit(run_compile())
run_extract()
elif action == "update":
run_update()
elif action == "compile":
run_compile()
elif action == "all":
run_extract()
run_update()
run_compile()
else:
msg = f"invalid action: '{action}'"
raise ValueError(msg)
raise SystemExit
48 changes: 32 additions & 16 deletions utils/bump_version.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
#!/usr/bin/env python3

from __future__ import annotations

import argparse
import os
import re
import sys
import time
from contextlib import contextmanager
from pathlib import Path
from typing import TYPE_CHECKING, Sequence

from typing_extensions import TypeAlias

script_dir = os.path.dirname(__file__)
package_dir = os.path.abspath(os.path.join(script_dir, '..'))
if TYPE_CHECKING:
from collections.abc import Iterator

script_dir = Path(__file__).parent
package_dir = script_dir.parent

RELEASE_TYPE = {'a': 'alpha', 'b': 'beta'}

VersionInfo: TypeAlias = tuple[int, int, int, str, int]


def stringify_version(version_info, in_develop=True):
def stringify_version(
version_info: VersionInfo, in_develop: bool = True,
) -> str:
version = '.'.join(str(v) for v in version_info[:3])
if not in_develop and version_info[3] != 'final':
version += version_info[3][0] + str(version_info[4])

return version


def bump_version(path, version_info, in_develop=True) -> None:
def bump_version(
path: Path, version_info: VersionInfo, in_develop: bool = True,
) -> None:
version = stringify_version(version_info, in_develop)

with open(path, encoding='utf-8') as f:
Expand All @@ -42,7 +56,7 @@ def bump_version(path, version_info, in_develop=True) -> None:
f.write('\n'.join(lines) + '\n')


def parse_version(version):
def parse_version(version: str) -> VersionInfo:
matched = re.search(r'^(\d+)\.(\d+)$', version)
if matched:
major, minor = matched.groups()
Expand Down Expand Up @@ -73,7 +87,7 @@ class Skip(Exception):


@contextmanager
def processing(message):
def processing(message: str) -> Iterator[None]:
try:
print(message + ' ... ', end='')
yield
Expand All @@ -87,7 +101,7 @@ def processing(message):


class Changes:
def __init__(self, path):
def __init__(self, path: Path) -> None:
self.path = path
self.fetch_version()

Expand Down Expand Up @@ -120,16 +134,18 @@ def finalize_release_date(self) -> None:
f.write('=' * len(heading) + '\n')
f.write(self.filter_empty_sections(body))

def add_release(self, version_info) -> None:
def add_release(self, version_info: VersionInfo) -> None:
if version_info[-2:] in (('beta', 0), ('final', 0)):
version = stringify_version(version_info)
else:
reltype = version_info[3]
version = (f'{stringify_version(version_info)} '
f'{RELEASE_TYPE.get(reltype, reltype)}{version_info[4] or ""}')
version = (
f'{stringify_version(version_info)} '
f'{RELEASE_TYPE.get(reltype, reltype)}{version_info[4] or ""}'
)
heading = 'Release %s (in development)' % version

with open(os.path.join(script_dir, 'CHANGES_template.rst'), encoding='utf-8') as f:
with open(script_dir / 'CHANGES_template.rst', encoding='utf-8') as f:
f.readline() # skip first two lines
f.readline()
tmpl = f.read()
Expand All @@ -145,11 +161,11 @@ def add_release(self, version_info) -> None:
f.write('\n')
f.write(body)

def filter_empty_sections(self, body):
def filter_empty_sections(self, body: str) -> str:
return re.sub('^\n.+\n-{3,}\n+(?=\n.+\n[-=]{3,}\n)', '', body, flags=re.MULTILINE)


def parse_options(argv):
def parse_options(argv: Sequence[str]) -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument('version', help='A version number (cf. 1.6b0)')
parser.add_argument('--in-develop', action='store_true')
Expand All @@ -162,11 +178,11 @@ def main() -> None:
options = parse_options(sys.argv[1:])

with processing("Rewriting sphinx/__init__.py"):
bump_version(os.path.join(package_dir, 'sphinx/__init__.py'),
bump_version(package_dir / 'sphinx' / '__init__.py',
options.version, options.in_develop)

with processing('Rewriting CHANGES'):
changes = Changes(os.path.join(package_dir, 'CHANGES.rst'))
changes = Changes(package_dir / 'CHANGES.rst')
if changes.version_info == options.version:
if changes.in_development:
changes.finalize_release_date()
Expand Down

0 comments on commit 768cf5e

Please sign in to comment.