Skip to content

Commit

Permalink
fix: deprecation warning when passing parser to Command.__init__ (#2010)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming authored Jun 13, 2023
1 parent 185ed17 commit ce2f939
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions news/2007.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pop up a warning when the deprecated `parser` argument is passed to `BaseCommand.__init__()` method.
14 changes: 10 additions & 4 deletions src/pdm/cli/commands/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import annotations

import argparse
import inspect
from argparse import _SubParsersAction
from typing import Any, TypeVar

from pdm.cli.options import Option, global_option, project_option, verbose_option
from pdm.project import Project
from pdm.utils import deprecation_warning

C = TypeVar("C", bound="BaseCommand")

Expand All @@ -21,12 +23,16 @@ class BaseCommand:
# Rewrite this if you don't want the default ones
arguments: list[Option] = [verbose_option, global_option, project_option]

def __init__(self, parser: argparse.ArgumentParser | None = None) -> None:
"""For compatibility, the parser is optional and won't be used."""

@classmethod
def init_parser(cls: type[C], parser: argparse.ArgumentParser) -> C:
cmd = cls()
args = inspect.signature(cls).parameters
if "parser" in args:
deprecation_warning(
f"The `parser` argument of `{cls.__name__}.__init__()` is deprecated. It won't be used."
)
cmd = cls(parser) # type: ignore[call-arg]
else:
cmd = cls()
for arg in cmd.arguments:
arg.add_to_parser(parser)
cmd.add_arguments(parser)
Expand Down

0 comments on commit ce2f939

Please sign in to comment.