summaryrefslogtreecommitdiff
path: root/tests/functional/u/unnecessary/unnecessary_ellipsis.py
blob: c46fd323a06281e12e1a4c40ed71d6180e3d993b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""Emit a warning when the ellipsis constant is used and can be avoided"""

# pylint: disable=missing-docstring, too-few-public-methods, invalid-name, unused-argument, comparison-of-constants

from typing import List, overload, Union

# Ellipsis and preceding statement
try:
    A = 2
except ValueError:
    A = 24
    ...  # [unnecessary-ellipsis]

def ellipsis_and_subsequent_statement():
    ...  # [unnecessary-ellipsis]
    return 0

# The parent of ellipsis is an assignment
B = ...
C = [..., 1, 2, 3]

# The parent of ellipsis is a call
if "X" is type(...):
    ...

def docstring_only():
    '''In Python, stubbed functions often have a body that contains just a
    single `...` constant, indicating that the function doesn't do
    anything. However, a stubbed function can also have just a
    docstring, and function with a docstring and no body also does
    nothing.
    '''


# This function has no docstring, so it needs a `...` constant.
def ellipsis_only():
    ...


def docstring_and_ellipsis():
    '''This function doesn't do anything, but it has a docstring, so its
    `...` constant is useless clutter.

    NEW CHECK: unnecessary-ellipsis

    This would check for stubs with both docstrings and `...`
    constants, suggesting the removal of the useless `...`
    constants
    '''
    ... # [unnecessary-ellipsis]


class DocstringOnly:
    '''The same goes for class stubs: docstring, or `...`, but not both.
    '''


# No problem
class EllipsisOnly:
    ...


class DocstringAndEllipsis:
    '''Whoops! Mark this one as bad too.
    '''
    ... # [unnecessary-ellipsis]


# Function overloading
@overload
def summarize(data: int) -> float: ...


@overload
def summarize(data: str) -> str: ...


def summarize(data):
    if isinstance(data, str):
        ...
    return float(data)



# Method overloading
class MyIntegerList(List[int]):
    @overload
    def __getitem__(self, index: int) -> int: ...

    @overload
    def __getitem__(self, index: slice) -> List[int]: ...

    def __getitem__(self, index: Union[int, slice]) -> Union[int, List[int]]:
        if isinstance(index, int):
            ...
        elif isinstance(index, slice):
            ...
        else:
            raise TypeError(...)

# Ellipsis is allowed as a default argument
def func_with_ellipsis_default_arg(a = ...) -> None:
    "Some docstring."

# Ignore if the ellipsis is inside a container:
my_list = [...]
my_tuple = (...,)
my_set = {...}

# Ellipsis inside a container which is a value in a dictionary
mydict1 = {'x': [...]}
mydict2 = {'x': {...}}
mydict3 = {'x': (...,)}

# Ignore if the ellipsis is used with a lambda expression
print("x", lambda: ...)


def func1(val1, _):
    if val1 is not ...:
        pass


def func2(val1, val2):
    """Ignore if ellipsis is used on comparisons.
    See https://github.com/PyCQA/pylint/issues/6071."""
    if val1 is not ... and val2:
        pass


assert "x" != ...