Skip to content

Increase coverage for argparse #6331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions pylint/config/arguments_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from __future__ import annotations

import argparse
import collections
import configparser
import copy
import optparse # pylint: disable=deprecated-module
Expand All @@ -16,6 +15,7 @@
import sys
import textwrap
import warnings
from collections import OrderedDict
from collections.abc import Sequence
from pathlib import Path
from typing import TYPE_CHECKING, Any, TextIO, Union
Expand Down Expand Up @@ -46,10 +46,6 @@
else:
import tomli as tomllib

if sys.version_info <= (3, 7, 1):
from typing_extensions import OrderedDict
else:
from typing import OrderedDict

if TYPE_CHECKING:
from pylint.config.arguments_provider import _ArgumentsProvider
Expand Down Expand Up @@ -87,7 +83,7 @@ def __init__(self, prog: str, usage: str | None = None) -> None:
# list of registered options providers
self.options_providers: list[ConfigProvider] = []
# dictionary associating option name to checker
self._all_options: OrderedDict[str, ConfigProvider] = collections.OrderedDict()
self._all_options: OrderedDict[str, ConfigProvider] = OrderedDict()
self._short_options: dict[str, str] = {}
self._nocallback_options: dict[ConfigProvider, str] = {}
self._mygroups: dict[str, optparse.OptionGroup] = {}
Expand Down
13 changes: 13 additions & 0 deletions tests/config/test_argparse_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import pytest

from pylint.config.arguments_manager import _ArgumentsManager
from pylint.config.exceptions import UnrecognizedArgumentAction
from pylint.lint import Run

HERE = abspath(dirname(__file__))
Expand Down Expand Up @@ -64,3 +66,14 @@ def test_old_names() -> None:
assert run.linter.config.ignore == run.linter.config.black_list
assert run.linter.config.ignore_patterns == [re.compile("^\\.#")]
assert run.linter.config.ignore_patterns == run.linter.config.black_list_re


class TestArguments:
@staticmethod
def test_unrecognized_argument() -> None:
"""Check that we correctly emit a warning for unrecognized argument types."""
manager = _ArgumentsManager(prog="test")
group = manager._arg_parser.add_argument_group(title="test")
with pytest.raises(UnrecognizedArgumentAction):
# We test with None as that is 'unrecognized'
manager._add_parser_option(group, None) # type: ignore[arg-type]
25 changes: 25 additions & 0 deletions tests/config/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ class SampleChecker(BaseChecker):
options = (("test-opt", {"action": "store_true", "help": "help message"}),)


class SampleCheckerTwo(BaseChecker):
options = (
("test-opt-two", {"action": "store", "type": "string", "help": "help message"}),
)


class SampleCheckerThree(BaseChecker):
options = (
(
"test-opt-three",
{"action": "store_true", "level": 1, "help": "help message"},
),
)


class TestDeprecationArgumentsManager:
"""Tests for deprecation warnings in the ArgumentsManager class."""

Expand Down Expand Up @@ -87,3 +102,13 @@ def test_level_attribute(self) -> None:

with pytest.warns(DeprecationWarning):
assert self.linter.level is not None

def test_no_default_in_optdict(self) -> None:
"""Test that not having a default value in a optiondict emits a DeprecationWarning."""
with pytest.warns(DeprecationWarning):
SampleCheckerTwo(self.linter)

def test_no_level_in_optdict(self) -> None:
"""Test that not having a level value in a optiondict emits a DeprecationWarning."""
with pytest.warns(DeprecationWarning):
SampleCheckerThree(self.linter)