Skip to content

Commit

Permalink
Switch to mypy - see also RobertCraigie/pyright-python#300
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibo-Joshi committed Sep 29, 2024
1 parent 9feb1d4 commit 3851718
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 42 deletions.
35 changes: 19 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,32 @@ repos:
args:
- --fix
- --unsafe-fixes
# Does not have support for PEP 695 generics yet
# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: v1.10.1
# hooks:
# - id: mypy
# name: mypy
# files: ^(?!(tests)).*\.py$
# args:
# - --install-types
- repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.382.post0
# Does not have support for PEP 695 generics yet
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.2
hooks:
- id: pyright
name: pyright
- id: mypy
name: mypy
files: ^(?!(tests)).*\.py$
args:
- --install-types
- --enable-incomplete-feature=NewGenericSyntax
additional_dependencies:
# https://github.com/RobertCraigie/pyright-python/issues/164
- pyright[nodejs]
- pydantic-settings~=2.3
- shortuuid~=1.0
- sphinx~=7.3
- tomlkit~=0.13
- typer~=0.12
# - repo: https://github.com/RobertCraigie/pyright-python
# rev: v1.1.382.post0
# hooks:
# - id: pyright
# name: pyright
# files: ^(?!(tests)).*\.py$
# additional_dependencies:
# - pydantic-settings~=2.3
# - shortuuid~=1.0
# - tomlkit~=0.13
# - typer~=0.12
- repo: https://github.com/asottile/pyupgrade
rev: v3.17.0
hooks:
Expand Down
4 changes: 2 additions & 2 deletions src/chango/_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
app = typer.Typer(help="CLI for chango - CHANgelog GOvernor for Your Project")


def version_callback(value: bool):
def version_callback(value: bool) -> None:
if value:
typer.echo(__version__)
raise typer.Exit
Expand All @@ -29,7 +29,7 @@ def main(
bool,
typer.Option("--version", callback=version_callback, help="Show the version and exit."),
] = False,
):
) -> None:
pass


Expand Down
10 changes: 5 additions & 5 deletions src/chango/_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


@app.callback()
def callback(context: typer.Context, path: _PATH_ANNOTATION = None):
def callback(context: typer.Context, path: _PATH_ANNOTATION = None) -> None:
if not path:
effective_path = Path.cwd() / "pyproject.toml"
elif path.is_absolute():
Expand All @@ -48,15 +48,15 @@ def callback(context: typer.Context, path: _PATH_ANNOTATION = None):
raise typer.BadParameter(f"Failed to parse the configuration file: {exc}") from exc

try:
context.obj["data"] = toml_data["tool"]["chango"] # type: ignore[reportIndexIssue]
context.obj["data"] = toml_data["tool"]["chango"] # type: ignore[index]
except KeyError as exc:
raise typer.BadParameter(
"No configuration found for chango in the configuration file."
) from exc


@app.command()
def show(context: typer.Context):
def show(context: typer.Context) -> None:
"""Show the configuration."""
string = f"""
Showing the configuration of the chango CLIas configured in {context.obj['path']}.
Expand All @@ -68,13 +68,13 @@ def show(context: typer.Context):


@app.command()
def schema():
def schema() -> None:
"""Show the JSON schema of the configuration."""
rprint(Markdown(f"```json\n{json.dumps(CLIConfig.model_json_schema(), indent=2)}\n```"))


@app.command()
def validate(context: typer.Context):
def validate(context: typer.Context) -> None:
"""Validate the configuration."""
try:
config = CLIConfig.model_validate(dict(context.obj["data"]))
Expand Down
4 changes: 2 additions & 2 deletions src/chango/_cli/config_module/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@


class _UserConfigManager:
def __init__(self):
def __init__(self) -> None:
self._chango_instance: ChanGo | None = None

def get_chango_instance(self) -> ChanGo:
if self._chango_instance is None:
self._chango_instance = import_chango_instance_from_config(
CLIConfig() # type: ignore[reportCallIssue]
CLIConfig() # type: ignore[call-arg]
)
return self._chango_instance

Expand Down
2 changes: 1 addition & 1 deletion src/chango/_cli/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ def edit(
help="The unique identifier of the change note to edit.", show_default=False
),
],
):
) -> None:
"""Edit an existing change note in the default editor."""
typer.launch(get_chango_instance().scanner.lookup_change_note(uid).path.as_posix())
2 changes: 1 addition & 1 deletion src/chango/_cli/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def new(
edit: Annotated[
bool, typer.Option(help="Whether to open the change note in the default editor.")
] = True,
):
) -> None:
"""Create a new change note."""
change_note = get_chango_instance().build_template_change_note(slug=slug)
path = get_chango_instance().write_change_note(change_note, version=None)
Expand Down
2 changes: 1 addition & 1 deletion src/chango/_cli/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def release(
date: Annotated[
dtm.date, typer.Argument(help="The date of the version release.", parser=date_callback)
],
):
) -> None:
"""Release the unreleased changes to a new version."""
if get_chango_instance().release(Version(uid, date)):
typer.echo(f"Released version {uid} on {date}")
Expand Down
4 changes: 2 additions & 2 deletions src/chango/_cli/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def version(
],
markup: MARKUP = MarkupLanguage.MARKDOWN,
output: OUTPUT_FILE = None,
):
) -> None:
"""Print a report of the change notes for a specific version."""
version_note = get_chango_instance().load_version_note(uid)
text = version_note.render(markup=markup)
Expand All @@ -43,7 +43,7 @@ def version(


@app.command()
def history(markup: MARKUP = MarkupLanguage.MARKDOWN, output: OUTPUT_FILE = None):
def history(markup: MARKUP = MarkupLanguage.MARKDOWN, output: OUTPUT_FILE = None) -> None:
"""Print a report of the version history."""
version_history = get_chango_instance().load_version_history()
text = version_history.render(markup=markup)
Expand Down
1 change: 0 additions & 1 deletion src/chango/_utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@

VersionUID = str | None
VUIDInput = Union["Version", str] | None
VersionIO = Union["Version", None]
CNUIDInput = Union["ChangeNote", str]
PathLike = str | Path
6 changes: 3 additions & 3 deletions src/chango/abc/_versionhistory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class VersionHistory[VNT: VersionNote](MutableMapping[VersionUID, VNT], abc.ABC)
themselves.
"""

def __init__(self):
def __init__(self) -> None:
self._version_notes: dict[VersionUID, VNT] = {}

def __delitem__(self, __key: VUIDInput) -> None:
Expand Down Expand Up @@ -50,15 +50,15 @@ def add_version_note(self, version_note: VNT) -> None:
Args:
version_note: The version note to add.
"""
self[version_note.uid] = version_note # type: ignore[reportArgumentType]
self[version_note.uid] = version_note # type: ignore[index]

def remove_version_note(self, version_note: VNT) -> None:
"""Remove a version note from the version note.
Args:
version_note: The version note to remove.
"""
del self[version_note.uid] # type: ignore[reportArgumentType]
del self[version_note.uid] # type: ignore[arg-type]

@abc.abstractmethod
def render(self, markup: str) -> str:
Expand Down
3 changes: 1 addition & 2 deletions src/chango/abc/_versionnote.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
from typing import TYPE_CHECKING, overload

from .._utils.filename import FileName
from .._utils.types import VersionIO
from ..abc._changenote import ChangeNote

if TYPE_CHECKING:
from chango import Version


class VersionNote[CNT: ChangeNote, V: VersionIO](MutableMapping[str, CNT], abc.ABC):
class VersionNote[CNT: ChangeNote, V: (Version, None)](MutableMapping[str, CNT], abc.ABC):
"""Abstract base class for a version note describing the set of changes in a software project
for a single version.
Expand Down
4 changes: 2 additions & 2 deletions src/chango/concrete/_commentversionnote.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: MIT
from typing import override

from .._utils.types import VersionIO
from .. import Version
from ..abc import VersionNote
from ..concrete import CommentChangeNote
from ..constants import MarkupLanguage
Expand All @@ -16,7 +16,7 @@ def _indent_multiline(text: str, indent: int = 2) -> str:
)


class CommentVersionNote[V: VersionIO](VersionNote[CommentChangeNote, V]):
class CommentVersionNote[V: (Version, None)](VersionNote[CommentChangeNote, V]):
"""A simple version note implementation that works with
:class:`~chango.concrete.CommentChangeNote`.
"""
Expand Down
2 changes: 1 addition & 1 deletion src/chango/concrete/_directoryversionscanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
if (path := Path(unreleased_directory)).is_dir():
self.unreleased_directory: Path = path
else:
self.unreleased_directory: Path = self.base_directory / unreleased_directory
self.unreleased_directory = self.base_directory / unreleased_directory

self.__available_versions: dict[str, _VersionInfo] | None = None

Expand Down
8 changes: 5 additions & 3 deletions src/chango/concrete/_headerversionhistory.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def render(self, markup: str) -> str:
if has_dates:
changes = sorted(
released_notes,
key=lambda note: note.date, # type: ignore[reportArgumentType]
key=lambda note: note.date, # type: ignore[arg-type,return-value]
)
match markup:
case MarkupLanguage.MARKDOWN:
Expand Down Expand Up @@ -66,8 +66,10 @@ def render(self, markup: str) -> str:
template = string.Template(tpl_str)
return "\n\n".join(
template.substitute(
uid=note.uid or "Unreleased",
date=note.date.isoformat() if note.date else "unknown",
uid="Unreleased" if note.uid is None else "Unreleased",
date=(
"unknown" if (note.date is None) else note.date.isoformat() # type: ignore[attr-defined]
),
comment=note.render(markup),
)
for note in changes
Expand Down

0 comments on commit 3851718

Please sign in to comment.