Passed
Push — main ( 402b70...4bfd4d )
by
unknown
01:30
created

pincer.utils.extraction.GetItem.__getitem__()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
# -*- coding: utf-8 -*-
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# MIT License
3
#
4
# Copyright (c) 2021 Pincer
5
#
6
# Permission is hereby granted, free of charge, to any person obtaining
7
# a copy of this software and associated documentation files
8
# (the "Software"), to deal in the Software without restriction,
9
# including without limitation the rights to use, copy, modify, merge,
10
# publish, distribute, sublicense, and/or sell copies of the Software,
11
# and to permit persons to whom the Software is furnished to do so,
12
# subject to the following conditions:
13
#
14
# The above copyright notice and this permission notice shall be
15
# included in all copies or substantial portions of the Software.
16
#
17
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
from typing import Any, Optional, Protocol, TypeVar
0 ignored issues
show
Bug introduced by
The name Protocol does not seem to exist in module typing.
Loading history...
25
26
T = TypeVar("T")
1 ignored issue
show
Coding Style Naming introduced by
Class name "T" doesn't conform to PascalCase naming style ('[^\\W\\da-z][^\\W_]+$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
27
28
29
class GetItem(Protocol):
30
    """
31
    Represents a class which implements the __getitem__ property.
32
    """
33
    def __getitem__(self, key: int) -> Any:
34
        ...
35
36
37
def get_index(
38
        collection: GetItem,
39
        index: int,
40
        fallback: Optional[T] = None
41
) -> Optional[T]:
42
    """
43
    Gets an item from a collection through index. Allows you to provide
44
    a fallback for if that index is out of bounds.
45
46
    :param collection: The collection from which the item is retrieved.
47
    :param index: The index of the item in the collection.
48
    :param fallback: The fallback value which will be used if the index
49
        doesn't exist. Default value is None.
50
    """
51
    try:
52
        return collection[index]
53
    except IndexError:
54
        return fallback
55