Skip to content

Commit 7460931

Browse files
committed
fix: Linting errors resulting from upgrading mypy/pyright.
1 parent 72ed0e6 commit 7460931

22 files changed

Lines changed: 89 additions & 50 deletions

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: install test lint lint-typos lint-examples format
1+
.PHONY: install test lint lint-base lint-312 lint-typos lint-examples format
22

33
install:
44
uv sync --all-extras

examples/test_kitchen_sink/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Example:
4141
subcommand: Annotated[Union[MeowCommand, BarkCommand], cappa.Subcommand]
4242

4343
flags: Annotated[list[str], cappa.Arg(short=True, long=True)] = field(
44-
default_factory=list
44+
default_factory=lambda: []
4545
)
4646
flag: bool = False # --flag
4747

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ ignore_missing_imports = true
8080
warn_unused_ignores = true
8181
incremental = true
8282
check_untyped_defs = true
83+
exclude = ["tests/py312"]
8384

8485
[tool.pyright]
8586
typeCheckingMode = "strict"
8687
typeCheckingModeOverrides = { "type_lens/**" = "basic" }
8788

88-
8989
[tool.typos.default]
9090
extend-ignore-re = [".*#.*typos: ignore[^\\n]*\\n"]
9191

src/cappa/arg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ def infer_parse(
622622

623623
literal_type = Literal[tuple(arg.choices)] # type: ignore
624624
literal_parse: Parser[Any] = parse_literal(literal_type) # type: ignore
625-
parse = [*parse, literal_parse]
625+
parse = cast(Sequence[Parser[Any]], [*parse, literal_parse])
626626

627627
return evaluate_parse(parse, type_view, state=state)
628628

src/cappa/argparse.py

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

33
import argparse
44
import sys
5-
from typing import TYPE_CHECKING, Any, Callable, Hashable, TypeVar, cast
5+
from typing import TYPE_CHECKING, Any, Callable, Hashable, List, TypeVar, cast
66

77
from cappa.arg import Arg, ArgAction
88
from cappa.command import Command, Subcommand
@@ -227,11 +227,11 @@ def add_argument(
227227

228228
names: list[str] = []
229229
if arg.short:
230-
short: list[str] = assert_type(arg.short, list)
230+
short = cast(List[str], arg.short)
231231
names.extend(short)
232232

233233
if arg.long:
234-
long: list[str] = assert_type(arg.long, list)
234+
long = cast(List[str], arg.long)
235235
names.extend(long)
236236

237237
is_positional = not names
@@ -288,7 +288,7 @@ def add_subcommands(
288288
description=subcommand.description,
289289
formatter_class=parser.formatter_class,
290290
add_help=False,
291-
command=subcommand, # type: ignore
291+
command=subcommand,
292292
output=output,
293293
prog=f"{parser.prog} {subcommand.real_name()}",
294294
**deprecated_kwarg,

src/cappa/base.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
import dataclasses
44
import inspect
5-
from typing import TYPE_CHECKING, Any, Protocol, TextIO, Type, TypeVar, Union, overload
5+
from typing import (
6+
TYPE_CHECKING,
7+
Any,
8+
Protocol,
9+
TextIO,
10+
Type,
11+
TypeVar,
12+
Union,
13+
cast,
14+
overload,
15+
)
616

717
from rich.theme import Theme
818
from typing_extensions import dataclass_transform
@@ -412,7 +422,7 @@ def wrapper(_decorated_cls: U) -> U:
412422
# Functions (and in particular class methods, must return a function object in order
413423
# to be attached as methods) cannot be nested, so we can just directly return it.
414424
if inspect.isfunction(_decorated_cls):
415-
return _decorated_cls # type: ignore
425+
return cast(U, _decorated_cls)
416426

417427
# Whereas classes will **generally** be the **exact** object as `_decorated_cls` was,
418428
# except in the case of dynamically generated subclasses used for detecting methods.

src/cappa/class_inspect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Field:
3434
annotation: type
3535
default: typing.Any | EmptyType = Empty
3636
default_factory: typing.Any | EmptyType = Empty
37-
metadata: dict[str, Any] = dataclasses.field(default_factory=dict)
37+
metadata: dict[str, Any] = dataclasses.field(default_factory=lambda: {})
3838

3939

4040
@dataclasses.dataclass
@@ -47,7 +47,7 @@ def collect(cls, typ: type) -> list[Self]:
4747
continue
4848
field = cls(
4949
name=f.name,
50-
annotation=f.type, # pyright: ignore
50+
annotation=f.type, # type: ignore
5151
default=f.default if f.default is not dataclasses.MISSING else Empty,
5252
default_factory=f.default_factory
5353
if f.default_factory is not dataclasses.MISSING

src/cappa/command.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ class Command(Generic[T]):
8181
"""
8282

8383
cmd_cls: type[T]
84-
arguments: list[Arg[Any] | Subcommand] = dataclasses.field(default_factory=list)
85-
propagated_arguments: list[Arg[Any]] = dataclasses.field(default_factory=list)
84+
arguments: list[Arg[Any] | Subcommand] = dataclasses.field(
85+
default_factory=lambda: []
86+
)
87+
propagated_arguments: list[Arg[Any]] = dataclasses.field(default_factory=lambda: [])
8688

8789
name: str | None = None
8890
help: str | None = None

src/cappa/docstring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def get_attribute_docstrings(command: type) -> dict[str, str]:
9090
if last_assignment:
9191
name = typing.cast(ast.Name, last_assignment.target).id
9292
value = typing.cast(ast.Constant, node.value).value
93-
result[name] = value
93+
result[name] = str(value)
9494
continue
9595

9696
last_assignment = node if isinstance(node, ast.AnnAssign) else None

src/cappa/help.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from collections.abc import Iterable
55
from dataclasses import dataclass, replace
66
from itertools import groupby
7-
from typing import Any, Sequence, cast
7+
from typing import Any, Sequence, Tuple, cast
88

99
from rich.console import Console, NewLine
1010
from rich.markdown import Markdown
@@ -31,19 +31,14 @@
3131
TextComponent = typing.Union[Text, Markdown, str]
3232
ArgFormat: TypeAlias = typing.Union[
3333
TextComponent,
34-
typing.Sequence[
35-
typing.Union[
36-
TextComponent,
37-
typing.Callable[[Arg[Any]], typing.Union[TextComponent, None]],
38-
]
39-
],
4034
typing.Callable[[Arg[Any]], typing.Union[TextComponent, None]],
4135
]
36+
ArgFormats: TypeAlias = typing.Sequence[ArgFormat]
4237

4338

4439
class HelpFormattable(typing.Protocol):
4540
left_padding: Dimension
46-
arg_format: ArgFormat
41+
arg_format: ArgFormat | ArgFormats
4742
default_format: str
4843

4944
def __call__(
@@ -117,7 +112,7 @@ def create_completion_arg(completion: bool | Arg[bool] = True) -> Arg[bool] | No
117112
@dataclass(frozen=True)
118113
class HelpFormatter(HelpFormattable):
119114
left_padding: Dimension = (0, 0, 0, 2)
120-
arg_format: ArgFormat = (
115+
arg_format: ArgFormat | ArgFormats = (
121116
Markdown("{help}"),
122117
Markdown("{choices}"),
123118
Markdown("{default}", style="dim italic"),
@@ -143,8 +138,12 @@ def __call__(self, command: Command[Any], prog: str) -> list[Displayable]:
143138
lines.extend(add_long_args(console, self, arg_groups))
144139
return lines
145140

146-
def with_arg_format(self, _format: ArgFormat, *formats: ArgFormat) -> Self:
147-
format = _format if isinstance(_format, tuple) else (_format,)
141+
def with_arg_format(
142+
self, _format: ArgFormat | tuple[ArgFormat, ...], *formats: ArgFormat
143+
) -> Self:
144+
format = cast(
145+
Tuple[ArgFormat, ...], _format if isinstance(_format, tuple) else (_format,)
146+
)
148147
arg_format = (*format, *formats)
149148
return replace(self, arg_format=arg_format)
150149

@@ -185,9 +184,13 @@ def add_long_args(
185184
def format_arg(
186185
console: Console, help_formatter: HelpFormattable, arg: Arg[Any]
187186
) -> Displayable:
188-
arg_format = help_formatter.arg_format
189-
if not isinstance(arg_format, Iterable) or isinstance(arg_format, str):
190-
arg_format = (arg_format,)
187+
unknown_arg_format = help_formatter.arg_format
188+
if isinstance(unknown_arg_format, Iterable) and not isinstance(
189+
unknown_arg_format, str
190+
):
191+
arg_format = cast(Sequence[ArgFormat], unknown_arg_format)
192+
else:
193+
arg_format = (unknown_arg_format,)
191194

192195
segments: list[TextComponent] = []
193196
for format_segment in arg_format:

0 commit comments

Comments
 (0)