Skip to content

Commit 22951aa

Browse files
flying-sheeppre-commit-ci[bot]hamdanal
authored
Fix types and mark as typed (#43)
Package type information in order to make pyright/mypy identify this as typed. Apart from this, I fixed the types: `group_name_formatter`, `styles`, `highlights`, and `usage_markup` are `ClassVar`s, not instance variables. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ali Hamdan <[email protected]>
1 parent b06817f commit 22951aa

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ repos:
2222
rev: 5.10.1
2323
hooks:
2424
- id: isort
25+
types_or: [python]
2526
args: [--add-import, "from __future__ import annotations"]
27+
- id: isort
28+
types_or: [pyi]
2629
- repo: https://github.com/psf/black
2730
rev: 22.10.0
2831
hooks:

pyproject.toml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[build-system]
2-
requires = ["flit_core >=3.2,<4"]
3-
build-backend = "flit_core.buildapi"
2+
requires = ["hatchling>=1.11.0"]
3+
build-backend = "hatchling.build"
44

55
[project]
66
name = "rich_argparse"
@@ -35,6 +35,13 @@ Documentation = "https://github.com/hamdanal/rich-argparse#rich-argparse"
3535
Issue-Tracker = "https://github.com/hamdanal/rich-argparse/issues"
3636
Changelog = "https://github.com/hamdanal/rich-argparse/blob/main/CHANGELOG.md"
3737

38+
[tool.hatch.build]
39+
sources = ["stubs"]
40+
include = [
41+
"rich_argparse-stubs",
42+
"rich_argparse.py",
43+
]
44+
3845
[tool.tox]
3946
legacy_tox_ini = """
4047
[tox]
@@ -64,6 +71,7 @@ line_length = 100
6471
[tool.mypy]
6572
strict = true
6673
show_error_codes = true
74+
explicit_package_bases = true
6775

6876
[[tool.mypy.overrides]]
6977
module = ["tests.*"]

rich_argparse.py

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

55
import argparse
66
import sys
7-
from typing import TYPE_CHECKING, Callable, Iterable, Iterator
7+
from typing import TYPE_CHECKING, Callable, ClassVar, Iterable, Iterator
88

99
# rich is only used to display help. It is imported inside the functions in order
1010
# not to add delays to command line tools that use this formatter.
@@ -28,20 +28,20 @@
2828
class RichHelpFormatter(argparse.HelpFormatter):
2929
"""An argparse HelpFormatter class that renders using rich."""
3030

31-
group_name_formatter: Callable[[str], str] = str.upper
32-
styles: dict[str, StyleType] = {
31+
group_name_formatter: ClassVar[Callable[[str], str]] = str.upper
32+
styles: ClassVar[dict[str, StyleType]] = {
3333
"argparse.args": "cyan",
3434
"argparse.groups": "dark_orange",
3535
"argparse.help": "default",
3636
"argparse.metavar": "dark_cyan",
3737
"argparse.syntax": "bold",
3838
"argparse.text": "default",
3939
}
40-
highlights: list[str] = [
40+
highlights: ClassVar[list[str]] = [
4141
r"(?:^|\s)(?P<args>-{1,2}[\w]+[\w-]*)", # highlight --words-with-dashes as args
4242
r"`(?P<syntax>[^`]*)`", # highlight text in backquotes as syntax
4343
]
44-
usage_markup: bool = False
44+
usage_markup: ClassVar[bool] = False
4545

4646
def __init__(
4747
self,
@@ -60,7 +60,7 @@ class _Section(argparse.HelpFormatter._Section): # type: ignore[valid-type,misc
6060
def __init__(
6161
self,
6262
formatter: RichHelpFormatter,
63-
parent: RichHelpFormatter._Section,
63+
parent: RichHelpFormatter._Section | None,
6464
heading: str | None = None,
6565
) -> None:
6666
if heading is not None:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import argparse
2+
from collections.abc import Callable
3+
from typing import ClassVar
4+
5+
from rich.console import Console
6+
from rich.style import StyleType
7+
8+
# flake8: noqa
9+
10+
class RichHelpFormatter(argparse.HelpFormatter):
11+
group_name_formatter: ClassVar[Callable[[str], str]]
12+
styles: ClassVar[dict[str, StyleType]]
13+
highlights: ClassVar[list[str]]
14+
usage_markup: ClassVar[bool]
15+
console: Console
16+
17+
class RawDescriptionRichHelpFormatter(RichHelpFormatter): ...
18+
class RawTextRichHelpFormatter(RawDescriptionRichHelpFormatter): ...
19+
class ArgumentDefaultsRichHelpFormatter(
20+
argparse.ArgumentDefaultsHelpFormatter, RichHelpFormatter
21+
): ...
22+
class MetavarTypeRichHelpFormatter(argparse.MetavarTypeHelpFormatter, RichHelpFormatter): ...

0 commit comments

Comments
 (0)