Skip to content

Commit

Permalink
Remove previously deprecated code (closes #184) (#188)
Browse files Browse the repository at this point in the history
* Remove previously deprecated code

Exception: `pre_call` was scheduled for removal in v0.30 but it needs to be
  replaced with some other mechanism, see discussion in #63.

* docs: update changelog

* chore: bump version
  • Loading branch information
neithere authored Sep 28, 2023
1 parent 03610b7 commit 505d49c
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 406 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Ignored stuff

# temp files
*.swo
*.swp
*$
*.pyc
*.pyo
Expand Down
16 changes: 16 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
Changelog
~~~~~~~~~

Version 0.30.0
--------------

Backwards incompatible changes:

- Removed previously deprecated features:

- argument help string in annotations — reserved for type hints;
- `argh.SUPPORTS_ALIASES`;
- `argh.safe_input()`;
- previously renamed arguments for `add_commands()`: `namespace`,
`namespace_kwargs`, `title`, `description`, `help`;

Note: the `pre_call` hack was scheduled to be removed but due to requests it
will remain until a replacement is implemented.

Version 0.29.4
--------------

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi"

[project]
name = "argh"
version = "0.29.4"
version = "0.30.0a1"
description = "An unobtrusive argparse wrapper with natural syntax"
readme = "README.rst"
requires-python = ">=3.8"
Expand Down
11 changes: 2 additions & 9 deletions src/argh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
# General Public License version 3 (LGPLv3) as published by the Free
# Software Foundation. See the file README.rst for copying conditions.
#
from .assembling import (
SUPPORTS_ALIASES,
add_commands,
add_subcommands,
set_default_command,
)
from .assembling import add_commands, add_subcommands, set_default_command
from .decorators import aliases, arg, expects_obj, named, wrap_errors
from .dispatching import (
PARSER_FORMATTER,
Expand All @@ -28,10 +23,9 @@
)
from .exceptions import AssemblingError, CommandError, DispatchingError
from .helpers import ArghParser
from .interaction import confirm, safe_input
from .interaction import confirm

__all__ = (
"SUPPORTS_ALIASES",
"add_commands",
"add_subcommands",
"set_default_command",
Expand All @@ -51,5 +45,4 @@
"DispatchingError",
"ArghParser",
"confirm",
"safe_input",
)
125 changes: 1 addition & 124 deletions src/argh/assembling.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
Functions and classes to properly assemble your commands in a parser.
"""
import warnings
from collections import OrderedDict

from argh.completion import COMPLETION_ENABLED
Expand All @@ -30,26 +29,12 @@
from argh.utils import get_arg_spec, get_subparsers

__all__ = [
"SUPPORTS_ALIASES",
"set_default_command",
"add_commands",
"add_subcommands",
]


# TODO: remove in v.0.30.
SUPPORTS_ALIASES = True
"""
.. deprecated:: 0.28.0
This constant will be removed in Argh v.0.30.
It's not relevant anymore because it's always `True` for all Python
versions currently supported by Argh.
"""


def _get_args_from_signature(function):
if getattr(function, ATTR_EXPECTS_NAMESPACE_OBJECT, False):
return
Expand All @@ -61,10 +46,6 @@ def _get_args_from_signature(function):

kwonly = getattr(spec, "kwonlyargs", [])

annotations = dict(
(k, v) for k, v in function.__annotations__.items() if isinstance(v, str)
)

# define the list of conflicting option strings
# (short forms, i.e. single-character ones)
named_args = set(list(defaults) + kwonly)
Expand All @@ -80,19 +61,6 @@ def _get_args_from_signature(function):
flags = [] # name_or_flags
akwargs = {} # keyword arguments for add_argument()

# TODO: remove this in v.0.30.
if name in annotations:
# help message: func(a : "b") -> add_argument("a", help="b")
value = annotations.get(name)
if isinstance(value, str):
warnings.warn(
"Defining argument help messages via annotations is "
+ "deprecated and will be removed in Argh 0.30. Please "
+ 'replace `f(a:"foo")` with `@arg("-a", help="foo")(a)`.',
DeprecationWarning,
)
akwargs.update(help=value)

if name in defaults or name in kwonly:
if name in defaults:
akwargs.update(default=defaults.get(name))
Expand Down Expand Up @@ -315,13 +283,7 @@ def add_commands(
group_name=None,
group_kwargs=None,
func_kwargs=None,
# deprecated args:
title=None,
description=None,
help=None,
namespace=None,
namespace_kwargs=None,
):
) -> None:
"""
Adds given functions as commands to given parser.
Expand Down Expand Up @@ -360,45 +322,6 @@ def add_commands(
a `dict` of keyword arguments to be passed to the nested ArgumentParser
instance under given `group_name`.
Deprecated params that should be renamed:
:param namespace:
.. deprecated:: 0.29.0
This argument will be removed in Argh v.0.30.
Please use `group_name` instead.
:param namespace_kwargs:
.. deprecated:: 0.29.0
This argument will be removed in Argh v.0.30.
Please use `group_kwargs` instead.
Deprecated params that should be moved into `group_kwargs`:
:param title:
.. deprecated:: 0.26.0
This argument will be removed in Argh v.0.30.
Please use `namespace_kwargs` instead.
:param description:
.. deprecated:: 0.26.0
This argument will be removed in Argh v.0.30.
Please use `namespace_kwargs` instead.
:param help:
.. deprecated:: 0.26.0
This argument will be removed in Argh v.0.30.
Please use `namespace_kwargs` instead.
.. note::
This function modifies the parser object. Generally side effects are
Expand All @@ -416,52 +339,6 @@ def add_commands(
"""
group_kwargs = group_kwargs or {}

# ------------------------------------------------------------------------
# TODO remove all of these in 0.30
#
if namespace:
warnings.warn(
"Argument `namespace` is deprecated in add_commands(), "
+ "it will be removed in Argh 0.30. "
+ "Please use `group_name` instead.",
DeprecationWarning,
)
group_name = namespace
if namespace_kwargs:
warnings.warn(
"Argument `namespace_kwargs` is deprecated in add_commands(), "
+ "it will be removed in Argh 0.30. "
+ "Please use `group_kwargs` instead.",
DeprecationWarning,
)
group_kwargs = namespace_kwargs
if title:
warnings.warn(
"Argument `title` is deprecated in add_commands(), "
+ "it will be removed in Argh 0.30. "
+ "Please use `parser_kwargs` instead.",
DeprecationWarning,
)
group_kwargs["description"] = title
if help:
warnings.warn(
"Argument `help` is deprecated in add_commands(), "
+ "it will be removed in Argh 0.30. "
+ "Please use `parser_kwargs` instead.",
DeprecationWarning,
)
group_kwargs["help"] = help
if description:
warnings.warn(
"Argument `description` is deprecated in add_commands(), "
+ "it will be removed in Argh 0.30. "
+ "Please use `parser_kwargs` instead.",
DeprecationWarning,
)
group_kwargs["description"] = description
#
# ------------------------------------------------------------------------

subparsers_action = get_subparsers(parser, create=True)

if group_name:
Expand Down
5 changes: 3 additions & 2 deletions src/argh/dispatching.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __setattr__(self, k, v):
# the function mapped to the innermost parser — the one we need.
self._functions_stack.append(v)
else:
super(ArghNamespace, self).__setattr__(k, v)
super().__setattr__(k, v)

def get_function(self):
return self._functions_stack[-1]
Expand All @@ -70,12 +70,13 @@ def dispatch(
argv=None,
add_help_command=True,
completion=True,
pre_call=None,
output_file=sys.stdout,
errors_file=sys.stderr,
raw_output=False,
namespace=None,
skip_unknown_args=False,
# deprecated args:
pre_call=None,
):
"""
Parses given list of arguments using given parser, calls the relevant
Expand Down
5 changes: 1 addition & 4 deletions src/argh/interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
Interaction
~~~~~~~~~~~
"""
# TODO: remove in v.0.30
from argh.io import safe_input

__all__ = ["confirm", "safe_input"]
__all__ = ["confirm"]


def confirm(action, default=None, skip=False):
Expand Down
28 changes: 0 additions & 28 deletions src/argh/io.py

This file was deleted.

56 changes: 18 additions & 38 deletions tests/test_assembling.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,24 @@ def three():
assert ns_default.get_function() == three


def test_set_add_commands_twice():
def one():
return 1

def two():
return 2

p = argh.ArghParser()
p.add_commands([one])
p.add_commands([two])

ns_explicit_one = p.parse_args(["one"])
ns_explicit_two = p.parse_args(["two"])

assert ns_explicit_one.get_function() == one
assert ns_explicit_two.get_function() == two


def test_add_command_with_group_kwargs_but_no_group_name():
def one():
return 1
Expand Down Expand Up @@ -275,23 +293,6 @@ def func(x, **kwargs):
]


# TODO: remove in v.0.30
def test_annotation():
"Extracting argument help from function annotations."

def cmd(foo: "quux" = 123): # noqa: F821
pass

parser = argh.ArghParser()

with pytest.warns(DeprecationWarning, match="will be removed in Argh 0.30"):
parser.set_default_command(cmd)

prog_help = parser.format_help()

assert "quux" in prog_help


def test_kwonlyargs():
"Correctly processing required and optional keyword-only arguments"

Expand Down Expand Up @@ -349,27 +350,6 @@ def func(foo):
assert not hasattr(p._actions[-1], "completer")


# TODO: remove in v.0.30
def test_set_default_command_deprecation_warnings():
parser = argh.ArghParser()

with pytest.warns(
DeprecationWarning, match="Argument `title` is deprecated in add_commands()"
):
argh.add_commands(parser, [], group_name="a", title="bar")

with pytest.warns(
DeprecationWarning,
match="Argument `description` is deprecated in add_commands()",
):
argh.add_commands(parser, [], group_name="b", description="bar")

with pytest.warns(
DeprecationWarning, match="Argument `help` is deprecated in add_commands()"
):
argh.add_commands(parser, [], group_name="c", help="bar")


@patch("argh.assembling.add_commands")
def test_add_subcommands(mock_add_commands):
mock_parser = MagicMock()
Expand Down
Loading

0 comments on commit 505d49c

Please sign in to comment.