Skip to content

TemplateCommand subclasses handle method typing is incorrect #1820

@PedroPerpetua

Description

@PedroPerpetua

Bug report

What's wrong

The TemplateCommand class stub (django-stubs/core/management/templates.pyi) defines the handle command as follows:

def handle(self, app_or_project: str, name: str, target: str | None = ..., **options: Any) -> None: ...

We can see that in subclasses (startapp and startproject), Django actually does not follow the super's definition, instead manually parsing kwargs to pass into the super's method:

# django.core.management.commands.startapp
class Command(TemplateCommand):
    help = (
        "Creates a Django app directory structure for the given app name in "
        "the current directory or optionally in the given directory."
    )
    missing_args_message = "You must provide an application name."

    def handle(self, **options):
        app_name = options.pop("name")
        target = options.pop("directory")
        super().handle("app", app_name, target, **options)

How is that should be

This is an issue where Django itself is kinda violating the principles of inheritance by havind a different definition in the child class; however, I believe the stubs should still reflect this behaviour, and should define the class methods for the respective commands in the way they are actually used:

def handle(self, **options: Any) -> Optional[str]: ...

System information

  • OS:
  • python version: 3.11
  • django version: 4.2.7
  • mypy version: 1.6.1
  • django-stubs version: 4.2.6
  • django-stubs-ext version: 4.2.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomersstubsIssues in stubs files (.pyi)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions