Skip to content

Commit 0841c19

Browse files
committed
burn down pyright errors
1 parent b308bfe commit 0841c19

File tree

5 files changed

+99
-53
lines changed

5 files changed

+99
-53
lines changed

django_typer/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
from typer.core import TyperGroup as CoreTyperGroup
9090
from typer.main import get_command as get_typer_command
9191
from typer.main import get_params_convertors_ctx_param_name_from_function
92-
from typer.models import CommandFunctionType
9392
from typer.models import Context as TyperContext
9493
from typer.models import Default, DefaultPlaceholder
9594

@@ -713,7 +712,7 @@ def subcommand(self):
713712
this can be used to group commands into panels in the help output.
714713
"""
715714

716-
def create_app(func: CommandFunctionType):
715+
def create_app(func: t.Callable[..., t.Any]):
717716
grp = GroupFunction( # type: ignore
718717
name=name,
719718
cls=cls,
@@ -855,7 +854,7 @@ def divide(
855854
this can be used to group commands into panels in the help output.
856855
"""
857856

858-
def decorator(func: CommandFunctionType):
857+
def decorator(func: t.Callable[..., t.Any]):
859858
setattr(
860859
func,
861860
"_typer_callback_",
@@ -958,7 +957,7 @@ def other_command(self):
958957
this can be used to group commands into panels in the help output.
959958
"""
960959

961-
def decorator(func: CommandFunctionType):
960+
def decorator(func: t.Callable[..., t.Any]):
962961
setattr(
963962
func,
964963
"_typer_command_",
@@ -1071,7 +1070,7 @@ def subcommand(self):
10711070
this can be used to group commands into panels in the help output.
10721071
"""
10731072

1074-
def create_app(func: CommandFunctionType):
1073+
def create_app(func: t.Callable[..., t.Any]):
10751074
grp = GroupFunction( # type: ignore
10761075
name=name,
10771076
cls=cls,
@@ -1199,8 +1198,9 @@ def __new__(
11991198
This method is called when a new class is created.
12001199
"""
12011200
try:
1202-
TyperCommand # pylint: disable=pointless-statement
1203-
is_base_init = False
1201+
# avoid unnecessary work creating the TyperCommand class
1202+
# is there a less weird way to do this?
1203+
is_base_init = not TyperCommand
12041204
except NameError:
12051205
is_base_init = True
12061206
typer_app = None
@@ -1267,7 +1267,7 @@ def __init__(cls, name, bases, attrs, **kwargs):
12671267
for arg in cls.suppressed_base_arguments or []
12681268
} # per django docs - allow these to be specified by either the option or param name
12691269

1270-
def get_ctor(attr: str) -> t.Optional[t.Callable[..., t.Any]]:
1270+
def get_ctor(attr: t.Any) -> t.Optional[t.Callable[..., t.Any]]:
12711271
return getattr(
12721272
attr, "_typer_command_", getattr(attr, "_typer_callback_", None)
12731273
)

django_typer/apps.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424

2525
try:
2626
import rich
27+
from rich import traceback
2728

2829
tb_config = traceback_config()
2930
if rich and isinstance(tb_config, dict) and not tb_config.get("no_install", False):
3031
# install rich tracebacks if we've been configured to do so (default)
3132
no_color = "NO_COLOR" in os.environ
3233
force_color = "FORCE_COLOR" in os.environ
33-
rich.traceback.install(
34+
traceback.install(
3435
console=tb_config.pop(
3536
"console",
3637
(
@@ -48,8 +49,7 @@
4849
**{
4950
param: value
5051
for param, value in tb_config.items()
51-
if param
52-
in set(inspect.signature(rich.traceback.install).parameters.keys())
52+
if param in set(inspect.signature(traceback.install).parameters.keys())
5353
},
5454
)
5555
except ImportError:

django_typer/management/commands/shellcompletion.py

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@
3636
else:
3737
from typing import Annotated
3838

39+
from functools import cached_property
40+
3941
from click.parser import split_arg_string
4042
from click.shell_completion import (
4143
CompletionItem,
4244
add_completion_class,
4345
get_completion_class,
4446
)
4547
from django.core.management import CommandError, ManagementUtility
46-
from django.utils.functional import cached_property
4748
from django.utils.module_loading import import_string
4849
from django.utils.translation import gettext
4950
from django.utils.translation import gettext_lazy as _
@@ -87,7 +88,7 @@ class Command(TyperCommand):
8788
cases users should refer to the online documentation for their specific shell to troubleshoot.
8889
"""
8990

90-
help = _("Install autocompletion for the current shell.")
91+
help = t.cast(str, _("Install autocompletion for the current shell."))
9192

9293
# disable the system checks - no reason to run these for this one-off command
9394
requires_system_checks = []
@@ -134,7 +135,7 @@ def manage_script_name(self) -> str:
134135
"""
135136
Get the name of the manage script as a command available from the shell's path.
136137
"""
137-
return getattr(self.manage_script, "name", self.manage_script)
138+
return str(getattr(self.manage_script, "name", self.manage_script))
138139

139140
@property
140141
def shell(self) -> Shells:
@@ -265,30 +266,40 @@ def replace(s: str, old: str, new: str, occurrences: t.List[int]) -> str:
265266
typer_scripts._completion_scripts[Shells.powershell.value] = script
266267
typer_scripts._completion_scripts[Shells.pwsh.value] = script
267268

268-
@command(help=_("Install autocompletion for the current or given shell."))
269+
@command(
270+
help=t.cast(str, _("Install autocompletion for the current or given shell."))
271+
)
269272
def install(
270273
self,
271274
shell: Annotated[
272275
t.Optional[Shells],
273276
Argument(
274-
help=_("Specify the shell to install or remove autocompletion for.")
277+
help=t.cast(
278+
str, _("Specify the shell to install or remove autocompletion for.")
279+
)
275280
),
276281
] = DETECTED_SHELL,
277282
manage_script: Annotated[
278283
t.Optional[str],
279284
Option(
280-
help=_(
281-
"The name of the django manage script to install autocompletion for if "
282-
"different than the script used to invoke this command."
285+
help=t.cast(
286+
str,
287+
_(
288+
"The name of the django manage script to install autocompletion for if "
289+
"different than the script used to invoke this command."
290+
),
283291
)
284292
),
285293
] = None,
286294
fallback: Annotated[
287295
t.Optional[str],
288296
Option(
289-
help=_(
290-
"The python import path to a fallback complete function to use when "
291-
"the completion command is not a TyperCommand."
297+
help=t.cast(
298+
str,
299+
_(
300+
"The python import path to a fallback complete function to use when "
301+
"the completion command is not a TyperCommand."
302+
),
292303
)
293304
),
294305
] = None,
@@ -311,7 +322,7 @@ def install(
311322
self.patch_script(fallback=fallback)
312323
install_path = install(
313324
shell=self.shell.value,
314-
prog_name=manage_script or self.manage_script_name,
325+
prog_name=str(manage_script or self.manage_script_name),
315326
complete_var=self.COMPLETE_VAR,
316327
)[1]
317328
self.stdout.write(
@@ -322,21 +333,29 @@ def install(
322333
)
323334
)
324335

325-
@command(help=_("Remove autocompletion for the current or given shell."))
336+
@command(
337+
help=t.cast(str, _("Remove autocompletion for the current or given shell."))
338+
)
326339
def remove(
327340
self,
328341
shell: Annotated[
329342
t.Optional[Shells],
330343
Argument(
331-
help=_("Specify the shell to install or remove shell completion for.")
344+
help=t.cast(
345+
str,
346+
_("Specify the shell to install or remove shell completion for."),
347+
)
332348
),
333349
] = DETECTED_SHELL,
334350
manage_script: Annotated[
335351
t.Optional[str],
336352
Option(
337-
help=_(
338-
"The name of the django manage script to remove shell completion for if "
339-
"different than the script used to invoke this command."
353+
help=t.cast(
354+
str,
355+
_(
356+
"The name of the django manage script to remove shell completion for if "
357+
"different than the script used to invoke this command."
358+
),
340359
)
341360
),
342361
] = None,
@@ -362,7 +381,7 @@ def remove(
362381
self.shell = shell # type: ignore
363382
stdout = self.stdout
364383
self.stdout = io.StringIO()
365-
prog_name = manage_script or self.manage_script_name
384+
prog_name = str(manage_script or self.manage_script_name)
366385
installed_path = install(shell=self.shell.value, prog_name=prog_name)[1]
367386
if self.shell in [Shells.pwsh, Shells.powershell]:
368387
# annoyingly, powershell has one profile script for all completion commands
@@ -421,31 +440,41 @@ def remove(
421440
)
422441
)
423442

424-
@command(help=_("Generate autocompletion for command string."), hidden=False)
443+
@command(
444+
help=t.cast(str, _("Generate autocompletion for command string.")), hidden=False
445+
)
425446
def complete(
426447
self,
427448
cmd_str: Annotated[
428449
t.Optional[str],
429450
Argument(
430451
metavar="command",
431-
help=_("The command string to generate completion suggestions for."),
452+
help=t.cast(
453+
str, _("The command string to generate completion suggestions for.")
454+
),
432455
),
433456
] = None,
434457
shell: Annotated[
435458
t.Optional[Shells],
436459
Option(
437-
help=_(
438-
"Specify the shell to fetch completion for, default will autodetect."
460+
help=t.cast(
461+
str,
462+
_(
463+
"Specify the shell to fetch completion for, default will autodetect."
464+
),
439465
)
440466
),
441467
] = None,
442468
fallback: Annotated[
443469
t.Optional[str],
444470
Option(
445-
help=_(
446-
"The python import path to a fallback complete function to use when "
447-
"the completion command is not a TyperCommand. By default, the builtin "
448-
"django autocomplete function is used."
471+
help=t.cast(
472+
str,
473+
_(
474+
"The python import path to a fallback complete function to use when "
475+
"the completion command is not a TyperCommand. By default, the builtin "
476+
"django autocomplete function is used."
477+
),
449478
)
450479
),
451480
] = None,

django_typer/types.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import sys
88
from pathlib import Path
9-
from typing import Any, Callable, Optional
9+
from typing import Any, Callable, Optional, cast
1010

1111
if sys.version_info < (3, 9):
1212
from typing_extensions import Annotated
@@ -57,7 +57,7 @@ def set_force_color(context, param, value):
5757
bool,
5858
Option(
5959
"--version",
60-
help=_("Show program's version number and exit."),
60+
help=cast(str, _("Show program's version number and exit.")),
6161
callback=print_version,
6262
is_eager=True,
6363
rich_help_panel=COMMON_PANEL,
@@ -74,9 +74,12 @@ def set_force_color(context, param, value):
7474
Verbosity = Annotated[
7575
int,
7676
Option(
77-
help=_(
78-
"Verbosity level; 0=minimal output, 1=normal output, "
79-
"2=verbose output, 3=very verbose output"
77+
help=cast(
78+
str,
79+
_(
80+
"Verbosity level; 0=minimal output, 1=normal output, "
81+
"2=verbose output, 3=very verbose output"
82+
),
8083
),
8184
show_choices=True,
8285
min=0,
@@ -101,10 +104,13 @@ def handle(self, verbosity: Verbosity = 1):
101104
Settings = Annotated[
102105
str,
103106
Option(
104-
help=_(
105-
"The Python path to a settings module, e.g. "
106-
'"myproject.settings.main". If this isn\'t provided, the '
107-
"DJANGO_SETTINGS_MODULE environment variable will be used."
107+
help=cast(
108+
str,
109+
_(
110+
"The Python path to a settings module, e.g. "
111+
'"myproject.settings.main". If this isn\'t provided, the '
112+
"DJANGO_SETTINGS_MODULE environment variable will be used."
113+
),
108114
),
109115
rich_help_panel=COMMON_PANEL,
110116
),
@@ -121,9 +127,12 @@ def handle(self, verbosity: Verbosity = 1):
121127
PythonPath = Annotated[
122128
Optional[Path],
123129
Option(
124-
help=_(
125-
"A directory to add to the Python path, e.g. "
126-
'"/home/djangoprojects/myproject".'
130+
help=cast(
131+
str,
132+
_(
133+
"A directory to add to the Python path, e.g. "
134+
'"/home/djangoprojects/myproject".'
135+
),
127136
),
128137
rich_help_panel=COMMON_PANEL,
129138
),
@@ -141,7 +150,7 @@ def handle(self, verbosity: Verbosity = 1):
141150
bool,
142151
Option(
143152
"--traceback",
144-
help=_("Raise on CommandError exceptions"),
153+
help=cast(str, _("Raise on CommandError exceptions")),
145154
rich_help_panel=COMMON_PANEL,
146155
),
147156
]
@@ -158,7 +167,7 @@ def handle(self, verbosity: Verbosity = 1):
158167
bool,
159168
Option(
160169
"--no-color",
161-
help=_("Don't colorize the command output."),
170+
help=cast(str, _("Don't colorize the command output.")),
162171
is_eager=True,
163172
callback=set_no_color,
164173
rich_help_panel=COMMON_PANEL,
@@ -177,7 +186,7 @@ def handle(self, verbosity: Verbosity = 1):
177186
bool,
178187
Option(
179188
"--force-color",
180-
help=_("Force colorization of the command output."),
189+
help=cast(str, _("Force colorization of the command output.")),
181190
is_eager=True,
182191
callback=set_force_color,
183192
rich_help_panel=COMMON_PANEL,
@@ -195,7 +204,9 @@ def handle(self, verbosity: Verbosity = 1):
195204
SkipChecks = Annotated[
196205
bool,
197206
Option(
198-
"--skip-checks", help=_("Skip system checks."), rich_help_panel=COMMON_PANEL
207+
"--skip-checks",
208+
help=cast(str, _("Skip system checks.")),
209+
rich_help_panel=COMMON_PANEL,
199210
),
200211
]
201212
"""

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,9 @@ disable = [
188188
'C0411', # wrong import order. I put them where they need to be!
189189
'W0603', # Using the global statement
190190
]
191+
192+
[tool.pyright]
193+
exclude = [
194+
"django_typer/tests/**/*",
195+
"django_typer/examples/**/*"
196+
]

0 commit comments

Comments
 (0)