Merge pull request #1487 from PyCQA/debug-store-true

eliminate --bug-report double-parse quirk with store_true
This commit is contained in:
Anthony Sottile 2021-12-07 17:05:22 -05:00 committed by GitHub
commit 1587936e2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 88 deletions

View file

@ -17,6 +17,10 @@ repos:
hooks:
- id: black
args: [--line-length=79]
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
- repo: https://github.com/asottile/pyupgrade
rev: v2.29.1
hooks:

View file

@ -1,6 +1,7 @@
"""Module containing the application logic for Flake8."""
import argparse
import configparser
import json
import logging
import sys
import time
@ -19,6 +20,7 @@ from flake8 import defaults
from flake8 import exceptions
from flake8 import style_guide
from flake8 import utils
from flake8.main import debug
from flake8.main import options
from flake8.options import aggregator
from flake8.options import config
@ -184,6 +186,11 @@ class Application:
argv,
)
if self.options.bug_report:
info = debug.information(self.option_manager)
print(json.dumps(info, indent=2, sort_keys=True))
raise SystemExit(0)
if self.options.diff:
LOG.warning(
"the --diff option is deprecated and will be removed in a "

View file

@ -1,36 +1,5 @@
"""Module containing the logic for our debugging logic."""
import argparse
import json
import platform
from typing import Dict
from typing import List
class DebugAction(argparse.Action):
"""argparse action to print debug information."""
def __init__(self, *args, option_manager, **kwargs):
"""Initialize the action.
This takes an extra `option_manager` keyword argument which will be
used to delay response.
"""
self._option_manager = option_manager
super().__init__(*args, **kwargs)
def __call__(self, parser, namespace, values, option_string=None):
"""Perform the argparse action for printing debug information."""
# NOTE(sigmavirus24): Flake8 parses options twice. The first time, we
# will not have any registered plugins. We can skip this one and only
# take action on the second time we're called.
if not self._option_manager.registered_plugins:
return
print(
json.dumps(
information(self._option_manager), indent=2, sort_keys=True
)
)
raise SystemExit(0)
def information(option_manager):
@ -38,7 +7,6 @@ def information(option_manager):
return {
"version": option_manager.version,
"plugins": plugins_from(option_manager),
"dependencies": dependencies(),
"platform": {
"python_implementation": platform.python_implementation(),
"python_version": platform.python_version(),
@ -57,8 +25,3 @@ def plugins_from(option_manager):
}
for plugin in sorted(option_manager.registered_plugins)
]
def dependencies() -> List[Dict[str, str]]:
"""Generate the list of dependencies we care about."""
return []

View file

@ -1,9 +1,7 @@
"""Contains the logic for all of the default options for Flake8."""
import argparse
import functools
from flake8 import defaults
from flake8.main import debug
from flake8.options.manager import OptionManager
@ -379,9 +377,6 @@ def register_default_options(option_manager: OptionManager) -> None:
add_option(
"--bug-report",
action=functools.partial(
debug.DebugAction, option_manager=option_manager
),
nargs=0,
action="store_true",
help="Print information necessary when preparing a bug report",
)

View file

@ -7,11 +7,6 @@ from flake8.main import debug
from flake8.options import manager
def test_dependencies():
"""Verify that we format our dependencies appropriately."""
assert [] == debug.dependencies()
@pytest.mark.parametrize(
"plugins, expected",
[
@ -75,7 +70,6 @@ def test_information(system, pyversion, pyimpl):
{"plugin": "mccabe", "version": "0.5.9", "is_local": False},
{"plugin": "pycodestyle", "version": "2.0.0", "is_local": False},
],
"dependencies": [],
"platform": {
"python_implementation": "CPython",
"python_version": "3.5.3",
@ -93,42 +87,3 @@ def test_information(system, pyversion, pyimpl):
pyimpl.assert_called_once_with()
pyversion.assert_called_once_with()
system.assert_called_once_with()
@mock.patch("flake8.main.debug.print")
@mock.patch("flake8.main.debug.information", return_value={})
@mock.patch("json.dumps", return_value="{}")
def test_print_information_no_plugins(dumps, information, print_mock):
"""Verify we print and exit only when we have plugins."""
option_manager = mock.Mock(registered_plugins=set())
action = debug.DebugAction(
"--bug-report",
dest="bug_report",
option_manager=option_manager,
)
assert action(None, None, None, None) is None
assert dumps.called is False
assert information.called is False
assert print_mock.called is False
@mock.patch("flake8.main.debug.print")
@mock.patch("flake8.main.debug.information", return_value={})
@mock.patch("json.dumps", return_value="{}")
def test_print_information(dumps, information, print_mock):
"""Verify we print and exit only when we have plugins."""
plugins = [
manager.PluginVersion("pycodestyle", "2.0.0", False),
manager.PluginVersion("mccabe", "0.5.9", False),
]
option_manager = mock.Mock(registered_plugins=set(plugins))
action = debug.DebugAction(
"--bug-report",
dest="bug_report",
option_manager=option_manager,
)
with pytest.raises(SystemExit):
action(None, None, None, None)
print_mock.assert_called_once_with("{}")
dumps.assert_called_once_with({}, indent=2, sort_keys=True)
information.assert_called_once_with(option_manager)