Skip to content

Commit

Permalink
fix isort/black conflicts, fix linting and type checking errors in sh…
Browse files Browse the repository at this point in the history
…ellcompletion command
  • Loading branch information
bckohan committed Jan 27, 2024
1 parent b3d0b33 commit 8ae86b9
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 138 deletions.
76 changes: 37 additions & 39 deletions django_typer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

import contextlib
import inspect
import sys
import os
import sys
import typing as t
from copy import deepcopy
from importlib import import_module
Expand All @@ -24,10 +24,11 @@
from django.core.management.base import BaseCommand
from django.utils.translation import gettext as _

if '--no-color' in sys.argv and '--force-color' not in sys.argv:
# this has to go here before rich Consoles are instantiated by Typer
if "--no-color" in sys.argv and "--force-color" not in sys.argv:
os.environ["NO_COLOR"] = "1"

from typer import Typer
from typer import Typer # pylint disable=wrong-import-position
from typer.core import TyperCommand as CoreTyperCommand
from typer.core import TyperGroup as CoreTyperGroup
from typer.main import MarkupMode
Expand All @@ -37,16 +38,9 @@
from typer.models import Context as TyperContext
from typer.models import Default, DefaultPlaceholder

from .types import (
ForceColor,
NoColor,
PythonPath,
Settings,
SkipChecks,
Traceback,
Verbosity,
Version,
)
from .types import ForceColor, NoColor, PythonPath, Settings, SkipChecks
from .types import Style as ColorStyle
from .types import Traceback, Verbosity, Version

VERSION = (0, 4, "0b")

Expand All @@ -60,12 +54,6 @@
__all__ = ["TyperCommand", "Context", "initialize", "command", "group", "get_command"]

"""
TODO
- useful django types (app label, etc)
- documentation
- linting
- type hints
design decision: no monkey-patching for call_command. call_command converts arguments to
strings. This is unavoidable and will just be a noted caveat that is also consistent with
how native django commands work. For calls with previously resolved types - the direct
Expand All @@ -75,6 +63,7 @@
behavior should align with native django commands
"""


def traceback_config() -> t.Union[bool, t.Dict[str, t.Any]]:
"""
Fetch the rich traceback installation parameters from our settings. By default
Expand Down Expand Up @@ -119,13 +108,15 @@ def _common_options(
no_color: NoColor = False,
force_color: ForceColor = False,
skip_checks: SkipChecks = False,
):
pass
) -> None:
"""
Common django options.
"""


# cache common params to avoid this extra work on every command
# we cant resolve these at module scope because translations break it
_common_params = []
_common_params: t.List[click.Parameter] = []


def _get_common_params():
Expand Down Expand Up @@ -298,6 +289,7 @@ def call_with_self(*args, **kwargs):


class TyperCommandWrapper(DjangoAdapterMixin, CoreTyperCommand):

def common_params(self):
if (
hasattr(self, "django_command")
Expand Down Expand Up @@ -358,10 +350,12 @@ def bind(self, django_command_cls: t.Type["TyperCommand"]):
self.django_command_cls.typer_app.add_typer(deepcopy(self))

def callback(self, *args, **kwargs):
raise NotImplementedError(_(
"callback is not supported - the function decorated by group() is the "
"callback."
))
raise NotImplementedError(
_(
"callback is not supported - the function decorated by group() is the "
"callback."
)
)

def command(
self,
Expand Down Expand Up @@ -661,20 +655,9 @@ def __new__(
pretty_exceptions_short=pretty_exceptions_short,
)

def handle(self, *args, **options):
return self.typer_app(
args=args,
standalone_mode=False,
supplied_params=options,
django_command=self,
complete_var=None,
prog_name=f"{sys.argv[0]} {self.typer_app.info.name}",
)

attrs = {
"_handle": attrs.pop("handle", None),
**attrs,
"handle": handle,
"typer_app": typer_app,
}

Expand All @@ -684,7 +667,7 @@ def __init__(cls, name, bases, attrs, **kwargs):
"""
This method is called after a new class is created.
"""
if cls.typer_app is not None:
if getattr(cls, "typer_app", None):
cls.typer_app.info.name = cls.__module__.rsplit(".", maxsplit=1)[-1]
cls.suppressed_base_arguments = {
arg.lstrip("--").replace("-", "_")
Expand Down Expand Up @@ -844,6 +827,11 @@ class Command(TyperCommand, attach='app_label.command_name.subcommand1.subcomman
...
"""

style: ColorStyle
stdout: t.IO[str]
stderr: t.IO[str]
requires_system_checks: t.Union[t.Sequence[str], str]

# we do not use verbosity because the base command does not do anything with it
# if users want to use a verbosity flag like the base django command adds
# they can use the type from django_typer.types.Verbosity
Expand Down Expand Up @@ -880,7 +868,7 @@ def get_command(self, *command_path: str):
except KeyError:
raise ValueError(f'No such command "{command_path[0]}"')

typer_app: t.Optional[Typer] = None
typer_app: Typer
_num_commands: int = 0
_has_callback: bool = False
_root_groups: int = 0
Expand Down Expand Up @@ -972,3 +960,13 @@ def __call__(self, *args, **kwargs):
"functions directly."
).format(cls=self.__class__)
)

def handle(self, *args, **options):
return self.typer_app(
args=args,
standalone_mode=False,
supplied_params=options,
django_command=self,
complete_var=None,
prog_name=f"{sys.argv[0]} {self.typer_app.info.name}",
)
Loading

0 comments on commit 8ae86b9

Please sign in to comment.