Skip to content
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

Use overload to type get_global_option #4978

Merged
merged 5 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion pylint/extensions/code_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __init__(self, linter: PyLinter) -> None:
super().__init__(linter=linter)

def open(self) -> None:
py_version: Tuple[int, int] = get_global_option(self, "py-version")
py_version = get_global_option(self, "py-version")
self._py38_plus = py_version >= (3, 8)
self._max_length: int = (
self.config.max_line_length_suggestions
Expand Down
4 changes: 2 additions & 2 deletions pylint/extensions/typing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, List, NamedTuple, Set, Tuple, Union
from typing import Dict, List, NamedTuple, Set, Union

import astroid.bases
from astroid import nodes
Expand Down Expand Up @@ -139,7 +139,7 @@ def __init__(self, linter: PyLinter) -> None:
self._consider_using_alias_msgs: List[DeprecatedTypingAliasMsg] = []

def open(self) -> None:
py_version: Tuple[int, int] = get_global_option(self, "py-version")
py_version = get_global_option(self, "py-version")
self._py37_plus = py_version >= (3, 7)
self._py39_plus = py_version >= (3, 9)
self._py310_plus = py_version >= (3, 10)
Expand Down
75 changes: 74 additions & 1 deletion pylint/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,34 @@
import sys
import textwrap
import tokenize
from typing import TYPE_CHECKING, List, Optional, Pattern, Tuple, Union, overload

from astroid import Module, modutils

from pylint.constants import PY_EXTS

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

if TYPE_CHECKING:
from pylint.checkers.base_checker import BaseChecker

DEFAULT_LINE_LENGTH = 79
# These are types used to overload get_global_option() and refer to the options type
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
GLOBAL_OPTION_BOOL = Literal[
"ignore-mixin-members",
"suggestion-mode",
"analyse-fallback-blocks",
"allow-global-unused-variables",
]
GLOBAL_OPTION_INT = Literal["max-line-length", "docstring-min-length"]
GLOBAL_OPTION_LIST = Literal["ignored-modules"]
GLOBAL_OPTION_PATTERN = Literal[
"no-docstring-rgx", "dummy-variables-rgx", "ignored-argument-names"
]
GLOBAL_OPTION_TUPLE_INT = Literal["py-version"]


def normalize_text(text, line_len=DEFAULT_LINE_LENGTH, indent=""):
Expand Down Expand Up @@ -148,7 +170,58 @@ def register_plugins(linter, directory):
imported[base] = 1


def get_global_option(checker, option, default=None):
@overload
def get_global_option(
checker: "BaseChecker", option: GLOBAL_OPTION_BOOL, default: Optional[bool] = None
) -> bool:
...


@overload
def get_global_option(
checker: "BaseChecker", option: GLOBAL_OPTION_INT, default: Optional[int] = None
) -> int:
...


@overload
def get_global_option(
checker: "BaseChecker",
option: GLOBAL_OPTION_LIST,
default: Optional[List[str]] = None,
) -> List[str]:
...


@overload
def get_global_option(
checker: "BaseChecker",
option: GLOBAL_OPTION_PATTERN,
default: Optional[Pattern] = None,
) -> Pattern:
...


@overload
def get_global_option(
checker: "BaseChecker",
option: GLOBAL_OPTION_TUPLE_INT,
default: Tuple[int, ...] = None,
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
) -> Tuple[int, ...]:
...


def get_global_option(
checker: "BaseChecker",
option: Union[
GLOBAL_OPTION_BOOL,
GLOBAL_OPTION_INT,
GLOBAL_OPTION_LIST,
GLOBAL_OPTION_PATTERN,
GLOBAL_OPTION_TUPLE_INT,
],
cdce8p marked this conversation as resolved.
Show resolved Hide resolved
default: Union[None, bool, int, List[str], Pattern, Tuple[int, ...]] = None,
) -> Union[None, bool, int, List[str], Pattern, Tuple[int, ...]]:
cdce8p marked this conversation as resolved.
Show resolved Hide resolved
"""Retrieve an option defined by the given *checker* or
by all known option providers.

Expand Down