Skip to content

Create new Argument class for options #5152

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions pylint/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import platformdirs

from pylint.config.argument_types import Argument, StoreArgument
from pylint.config.configuration_mixin import ConfigurationMixIn
from pylint.config.find_default_config_files import find_default_config_files
from pylint.config.man_help_formatter import _ManHelpFormatter
Expand All @@ -60,6 +61,8 @@
"OptionParser",
"OptionsProviderMixIn",
"UnsupportedAction",
"StoreArgument",
"Argument",
]

USER_HOME = os.path.expanduser("~")
Expand Down
66 changes: 66 additions & 0 deletions pylint/config/argument_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
import sys
from typing import NamedTuple, Optional, Tuple, Union

if sys.version_info >= (3, 8):
from typing import Literal
else:
from typing_extensions import Literal


class Argument(NamedTuple):
"""Data about a argument to be parsed by argparse (or optparse until deprecation)
Follows structure of the parameters for argparse.ArgumentParser.add_argument
See https://docs.python.org/3/library/argparse.html#the-add-argument-method"""

name: Tuple[str, ...]
"""Tuple of names or flags (i.e., -f or --foo)"""

action: str
"""Action to do after"""

nargs: Optional[int]
"""The number of command line arguments to capture"""

const: Optional[str]
"""The const to store with certain types of actions"""

default: Union[str, int, bool, None]
"""Default value"""

argument_type: str
"""Name of validator"""

help_string: str
"""Help message"""

metavar: str
"""Metavar for help message"""


class StoreArgument(Argument):
"""Used to store a passed argument or default value"""

def __new__(
cls,
name: Tuple[str, ...],
action: Literal["store"],
nargs: Literal[1],
const: None,
default: Union[str, int, bool, None],
argument_type: Literal["yn"],
help_string: str,
metavar: Literal["<y_or_n>"],
) -> "StoreArgument":
return Argument.__new__(
cls,
name,
action,
nargs,
const,
default,
argument_type,
help_string,
metavar,
)
19 changes: 19 additions & 0 deletions pylint/extensions/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
is_node_in_type_annotation_context,
safe_infer,
)
from pylint.config import StoreArgument
from pylint.interfaces import IAstroidChecker
from pylint.lint import PyLinter
from pylint.utils.utils import get_global_option
Expand Down Expand Up @@ -121,6 +122,24 @@ class TypingChecker(BaseChecker):
},
),
)
arguments = (
StoreArgument(
("runtime-typing",),
"store",
1,
None,
True,
"yn",
"Set to ``no`` if the app / library does **NOT** need to "
"support runtime introspection of type annotations. "
"If you use type annotations **exclusively** for type checking "
"of an application, you're probably fine. For libraries, "
"evaluate if some users what to access the type hints "
"at runtime first, e.g., through ``typing.get_type_hints``. "
"Applies to Python versions 3.7 - 3.9",
"<y_or_n>",
),
)

_should_check_typing_alias: bool
"""The use of type aliases (PEP 585) requires Python 3.9
Expand Down