Skip to content

Commit

Permalink
Add typing-extensions as an explicit dependency
Browse files Browse the repository at this point in the history
Add pyupgrade as a pre-commit hook with --py37-plus arg.
Run and upgrade all files accordingly.

Configure mypy to check typing expecting Python 3.7.
  • Loading branch information
CasperWA committed Nov 3, 2023
1 parent d161a2f commit b6578a8
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 82 deletions.
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ repos:
- id: isort
args: ["--profile", "black", "--filter-files", "--skip-gitignore"]

# pyupgrade is a tool to automatically upgrade Python syntax for newer versions
# It works on files in-place
- repo: https://github.com/asottile/pyupgrade
# Latest version for Python 3.7: 3.3.2
# Latest version for Python 3.8: 3.8.0
rev: v3.3.2
hooks:
- id: pyupgrade
args: ["--py37-plus"]

# Black is a code style and formatter
# It works on files in-place
- repo: https://github.com/ambv/black
Expand Down
16 changes: 9 additions & 7 deletions ci_cd/tasks/api_reference_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from ci_cd.utils import Emoji

if TYPE_CHECKING: # pragma: no cover
from typing import List

from invoke import Context, Result


Expand Down Expand Up @@ -121,18 +123,18 @@ def create_api_reference_docs( # pylint: disable=too-many-locals,too-many-branc
debug: bool = debug # type: ignore[no-redef]

if not unwanted_folder:
unwanted_folder: list[str] = ["__pycache__"] # type: ignore[no-redef]
unwanted_folder: "List[str]" = ["__pycache__"] # type: ignore[no-redef]
if not unwanted_file:
unwanted_file: list[str] = ["__init__.py"] # type: ignore[no-redef]
unwanted_file: "List[str]" = ["__init__.py"] # type: ignore[no-redef]
if not full_docs_folder:
full_docs_folder: list[str] = [] # type: ignore[no-redef]
full_docs_folder: "List[str]" = [] # type: ignore[no-redef]
if not full_docs_file:
full_docs_file: list[str] = [] # type: ignore[no-redef]
full_docs_file: "List[str]" = [] # type: ignore[no-redef]
if not special_option:
special_option: list[str] = [] # type: ignore[no-redef]
special_option: "List[str]" = [] # type: ignore[no-redef]

# Initialize user-given paths as pure POSIX paths
package_dir: list[PurePosixPath] = [PurePosixPath(_) for _ in package_dir]
package_dir: "List[PurePosixPath]" = [PurePosixPath(_) for _ in package_dir]
root_repo_path = str(PurePosixPath(root_repo_path))
docs_folder: PurePosixPath = PurePosixPath(docs_folder) # type: ignore[no-redef]
full_docs_folder = [Path(PurePosixPath(_)) for _ in full_docs_folder]
Expand Down Expand Up @@ -161,7 +163,7 @@ def write_file(full_path: Path, content: str) -> None:
root_repo_path = result.stdout.strip("\n") # type: ignore[no-redef]

root_repo_path: Path = Path(root_repo_path).resolve() # type: ignore[no-redef]
package_dirs: list[Path] = [Path(root_repo_path / _) for _ in package_dir]
package_dirs: "List[Path]" = [Path(root_repo_path / _) for _ in package_dir]
docs_api_ref_dir = Path(root_repo_path / docs_folder / "api_reference")

LOGGER.debug(
Expand Down
4 changes: 3 additions & 1 deletion ci_cd/tasks/docs_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from ci_cd.utils import Emoji

if TYPE_CHECKING: # pragma: no cover
from typing import List

from invoke import Context, Result


Expand Down Expand Up @@ -56,7 +58,7 @@ def create_docs_index( # pylint: disable=too-many-locals
docs_folder: Path = Path(docs_folder)

if not replacement:
replacement: list[str] = [] # type: ignore[no-redef]
replacement: "List[str]" = [] # type: ignore[no-redef]

Check warning on line 61 in ci_cd/tasks/docs_index.py

View check run for this annotation

Codecov / codecov/patch

ci_cd/tasks/docs_index.py#L61

Added line #L61 was not covered by tests
replacement.append(f"{docs_folder.name}/{replacement_separator}")

if pre_commit and root_repo_path == ".":
Expand Down
7 changes: 5 additions & 2 deletions ci_cd/tasks/setver.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

from ci_cd.utils import Emoji, SemanticVersion, update_file

if TYPE_CHECKING: # pragma: no cover
from typing import List

# Get logger
LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -63,7 +66,7 @@ def setver( # pylint: disable=too-many-locals
package_dir: str = package_dir # type: ignore[no-redef]
version: str = version # type: ignore[no-redef]
root_repo_path: str = root_repo_path # type: ignore[no-redef]
code_base_update: list[str] = code_base_update # type: ignore[no-redef]
code_base_update: List[str] = code_base_update # type: ignore[no-redef]
code_base_update_separator: str = code_base_update_separator # type: ignore[no-redef] # pylint: disable=line-too-long
test: bool = test # type: ignore[no-redef]
fail_fast: bool = fail_fast # type: ignore[no-redef]
Expand Down Expand Up @@ -100,7 +103,7 @@ def setver( # pylint: disable=too-many-locals
),
)
else:
errors: list[str] = []
errors: "List[str]" = []

Check warning on line 106 in ci_cd/tasks/setver.py

View check run for this annotation

Codecov / codecov/patch

ci_cd/tasks/setver.py#L106

Added line #L106 was not covered by tests
for code_update in code_base_update:
try:
filepath, pattern, replacement = code_update.split(
Expand Down
10 changes: 5 additions & 5 deletions ci_cd/tasks/update_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def update_deps( # pylint: disable=too-many-branches,too-many-locals,too-many-s
):
"""Update dependencies in specified Python package's `pyproject.toml`."""
if TYPE_CHECKING: # pragma: no cover
context: "Context" = context # type: ignore[no-redef]
context: Context = context # type: ignore[no-redef]
root_repo_path: str = root_repo_path # type: ignore[no-redef]
fail_fast: bool = fail_fast # type: ignore[no-redef]
pre_commit: bool = pre_commit # type: ignore[no-redef]
Expand All @@ -130,7 +130,7 @@ def update_deps( # pylint: disable=too-many-branches,too-many-locals,too-many-s

if pre_commit and root_repo_path == ".":
# Use git to determine repo root
result: "Result" = context.run("git rev-parse --show-toplevel", hide=True)
result: Result = context.run("git rev-parse --show-toplevel", hide=True)
root_repo_path = result.stdout.strip("\n")

pyproject_path = Path(root_repo_path).resolve() / "pyproject.toml"
Expand Down Expand Up @@ -261,7 +261,7 @@ def update_deps( # pylint: disable=too-many-branches,too-many-locals,too-many-s
LOGGER.debug("Min/max Python version from marker: %s", marker_py_version)

# Check version from PyPI's online package index
out: "Result" = context.run(
out: Result = context.run(
"pip index versions "
f"--python-version {marker_py_version or py_version} "
f"{parsed_requirement.name}",
Expand Down Expand Up @@ -342,8 +342,8 @@ def update_deps( # pylint: disable=too-many-branches,too-many-locals,too-many-s

# Apply ignore rules
if parsed_requirement.name in ignore_rules or "*" in ignore_rules:
versions: "IgnoreVersions" = []
update_types: "IgnoreUpdateTypes" = {}
versions: IgnoreVersions = []
update_types: IgnoreUpdateTypes = {}

if "*" in ignore_rules:
versions, update_types = parse_ignore_rules(ignore_rules["*"])
Expand Down
Loading

0 comments on commit b6578a8

Please sign in to comment.