• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ni / nixnet-python / #631999863

08 Apr 2025 07:22AM UTC coverage: 67.546% (-0.005%) from 67.551%
#631999863

Pull #302

travis-ci

Pull Request #302: Fix `tox -e mypy` errors with `mypy 1.14.1`

22 of 66 new or added lines in 15 files covered. (33.33%)

10 existing lines in 1 file now uncovered.

4712 of 6976 relevant lines covered (67.55%)

0.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

32.99
/nixnet/_session/collection.py
1
import abc
1✔
2
from collections.abc import Sequence
1✔
3
import typing  # NOQA: F401
1✔
4

5
import six
1✔
6

1✔
7
from nixnet import _props
8

1✔
9

×
10
@six.add_metaclass(abc.ABCMeta)
NEW
11
class Collection(Sequence):
×
12
    """Collection of items in a session."""
1✔
13

14
    def __init__(self, handle):
1✔
15
        # type: (int) -> None
16
        self._handle = handle
1✔
17
        self.__list_cache = None  # type: typing.Optional[typing.List[typing.Text]]
18

19
    def __repr__(self):
1✔
20
        return '{}(handle={})'.format(type(self).__name__, self._handle)
1✔
21

22
    def __len__(self):
23
        # type: () -> int
1✔
24
        return _props.get_session_num_in_list(self._handle)
25

1✔
26
    def __iter__(self):
1✔
27
        item_count = len(self)
28
        item_names = self._list_cache
1✔
29
        assert item_count == len(item_names), \
×
30
            "Frame count ({}) is out of sync with items ({})".format(item_count, item_names)
31
        for index, name in enumerate(item_names):
1✔
32
            yield self._create_item(self._handle, index, name)
33

1✔
34
    def __contains__(self, index):
35
        if isinstance(index, six.integer_types):
1✔
36
            return 0 <= index and index < len(self._list_cache)
×
37
        elif isinstance(index, six.string_types):
×
38
            name = index
×
39
            return name in self._list_cache
40
        else:
×
41
            raise TypeError(index)
×
42

43
    def __getitem__(self, index):
1✔
44
        if isinstance(index, six.integer_types):
×
45
            name = self._list_cache[index]
×
46
        elif isinstance(index, six.string_types):
×
47
            name = index
×
48
            item_names = self._list_cache
×
49
            try:
50
                index = item_names.index(name)
×
51
            except ValueError:
52
                raise KeyError(name)
1✔
53
        else:
×
54
            raise TypeError(index)
×
55

×
56
        return self._create_item(self._handle, index, name)
×
57

×
58
    def get(self, index, default=None):
×
59
        # type: (typing.Union[int, typing.Text], typing.Any) -> Item
×
60
        """Access an item, returning ``default`` on failure.
×
61

×
62
        Args:
63
            index(str or int): Item name or index
×
64
            default: Value to return when lookup fails
65
        """
×
66
        if isinstance(index, six.integer_types):
67
            try:
1✔
68
                name = self._list_cache[index]
69
            except IndexError:
70
                return default
71
        elif isinstance(index, six.string_types):
72
            name = index
73
            item_names = self._list_cache
74
            try:
75
                index = item_names.index(name)
×
76
            except ValueError:
×
77
                return default
×
78
        else:
×
79
            raise TypeError(index)
×
80

×
81
        return self._create_item(self._handle, index, name)
×
82

×
83
    def __eq__(self, other):
×
84
        if isinstance(other, self.__class__):
×
85
            other_collection = typing.cast(Collection, other)
×
86
            return (
×
87
                self._handle == other_collection._handle
88
                and self._list_cache == other_collection._list_cache
×
89
            )
90
        else:
×
91
            return NotImplemented
92

1✔
93
    def __ne__(self, other):
×
94
        result = self.__eq__(other)
×
95
        if result is NotImplemented:
×
96
            return result
97
        else:
98
            return not result
99

×
100
    @property
101
    def _list_cache(self):
1✔
102
        # type: () -> typing.List[typing.Text]
×
103
        if self.__list_cache is None:
×
104
            self.__list_cache = list(_props.get_session_list(self._handle))
×
105
        return self.__list_cache
106

×
107
    @abc.abstractmethod
108
    def _create_item(self, handle, index, name):
1✔
109
        # type: (int, int, typing.Text) -> Item
110
        pass
111

×
112

×
113
class Item(object):
×
114
    """Item configuration for a session."""
115

1✔
116
    def __init__(self, handle, index, name):
117
        # type: (int, int, typing.Text) -> None
118
        self._handle = handle
×
119
        self._index = index
120
        self._name = name
121

1✔
122
    def __repr__(self):
123
        return '{}(handle={})'.format(type(self).__name__, self._handle)
124

1✔
125
    def __eq__(self, other):
126
        if isinstance(other, self.__class__):
×
127
            other_item = typing.cast(Item, other)
×
128
            return (
×
129
                self._handle == other_item._handle
130
                and self._index == other_item._index
1✔
131
            )
×
132
        else:
133
            return NotImplemented
1✔
134

×
135
    def __ne__(self, other):
×
136
        result = self.__eq__(other)
×
137
        if result is NotImplemented:
138
            return result
139
        else:
140
            return not result
×
141

142
    def __int__(self):
1✔
143
        # type: () -> int
×
144
        return self._index
×
145

×
146
    def __str__(self):
147
        # type: () -> typing.Text
×
148
        return self._name
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc