Skip to content

Commit

Permalink
Fix typing
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeldycke committed Nov 14, 2023
1 parent 7e0e881 commit fc0fd08
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
7 changes: 2 additions & 5 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from __future__ import annotations

import sys
from pathlib import Path

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib # type: ignore[import]
import tomllib # type: ignore[import-untyped]


project_path = Path(__file__).parent.parent.resolve()

Expand Down
42 changes: 25 additions & 17 deletions mail_deduplicate/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from __future__ import annotations

import re
from typing import Pattern, Callable

from click_extra import (
BadParameter,
Expand Down Expand Up @@ -69,36 +70,43 @@
)


def validate_regexp(ctx: Context, param: Parameter, value: str) -> str:
def validate_regexp(ctx: Context, param: Parameter, value: str) -> str | Pattern[str]:
"""Validate and compile regular expression provided as parameters to the CLI."""
if value:
try:
value = re.compile(value)
except ValueError:
msg = "invalid regular expression."
raise BadParameter(msg)
return value
if not value:
return ""

try:
return re.compile(value)
except ValueError:
msg = "invalid regular expression."
raise BadParameter(msg)


class MdedupCommand(ExtraCommand):
def format_help(self, ctx: Context, formatter: HelpExtraFormatter) -> None:
def format_help(
self,
ctx: Context,
formatter: HelpExtraFormatter, # type: ignore[override]
) -> None:
"""Extend the help screen with the description of all available strategies."""
# Populate the formatter with the default help screen content.
super().format_help(ctx, formatter)

# Produce the strategy reference table, with grouped aliases.
method_to_ids = {}
method_to_ids: dict[Callable, list[str]] = {}
for strat_id, method in sorted(STRATEGY_METHODS.items(), reverse=True):
method_to_ids.setdefault(method, []).append(strat_id)
strat_table = sorted(
[
(f"[{'|'.join(strat_ids)}]", " ".join(method.__doc__.split()))
for method, strat_ids in method_to_ids.items()
],
)

strat_table: list[tuple[str, str]] = []
for method, strat_ids in method_to_ids.items():
row_title = f"[{'|'.join(strat_ids)}]"
row_desc = ""
if method.__doc__:
row_desc = " ".join(method.__doc__.split())
strat_table.append((row_title, row_desc))

with formatter.section("Available strategies"):
formatter.write_dl(strat_table)
formatter.write_dl(sorted(strat_table))


@extra_command(
Expand Down

0 comments on commit fc0fd08

Please sign in to comment.