• 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

69.01
/nixnet/database/_dbc_attributes.py
1
from collections.abc import Mapping
1✔
2
import six
1✔
3
import typing  # NOQA: F401
1✔
4

5
from nixnet import _funcs
1✔
6
from nixnet import constants
7

1✔
8

×
9
class DbcAttributeCollection(Mapping):
NEW
10
    """Collection for accessing DBC attributes."""
×
11

1✔
12
    def __init__(self, handle):
1✔
13
        # type: (int) -> None
14
        self._handle = handle
1✔
15

1✔
16
        # Here, we are caching the attribute names and enums to work around a driver issue.
17
        # The issue results in an empty attribute value after intermixing calls to get attribute values and enums.
18
        # We can avoid this issue if we get all attribute enums first, before getting any attribute values.
1✔
19
        self._cache = dict(
20
            (name, self._get_enums(name))
21
            for name in self._get_names()
1✔
22
        )
23

1✔
24
    def __repr__(self):
25
        return '{}(handle={})'.format(type(self).__name__, self._handle)
26

27
    def __eq__(self, other):
28
        if isinstance(other, self.__class__):
1✔
29
            return self._handle == typing.cast(DbcAttributeCollection, other)._handle
30
        else:
31
            return NotImplemented
32

33
    def __ne__(self, other):
1✔
34
        result = self.__eq__(other)
1✔
35
        if result is NotImplemented:
36
            return result
1✔
37
        else:
1✔
38
            return not result
1✔
39

40
    def __hash__(self):
×
41
        return hash(self._handle)
42

1✔
43
    def __len__(self):
1✔
44
        return len(list(self.keys()))
1✔
45

×
46
    def __iter__(self):
47
        return self.keys()
1✔
48

49
    def __getitem__(self, key):
1✔
50
        # type: (typing.Text) -> typing.Tuple[typing.Text, bool]
1✔
51
        """Return the attribute value and whether it's the default value.
52

1✔
53
            Args:
1✔
54
                key(str): attribute name.
55
            Returns:
1✔
56
                tuple(str, bool): attribute value and whether it's the default value.
1✔
57
        """
58
        if isinstance(key, six.string_types):
1✔
59
            return self._get_value(key)
60
        else:
61
            raise TypeError(key)
62

63
    def keys(self):
64
        """Return all attribute names in the collection.
65

66
            Yields:
67
                An iterator to all attribute names in the collection.
1✔
68
        """
1✔
69
        for name in self._cache:
70
            yield name
1✔
71

72
    def values(self):
1✔
73
        """Return all attribute values in the collection.
74

75
            Yields:
76
                An iterator to all attribute values in the collection.
77
        """
78
        for name in self._cache:
1✔
79
            yield self._get_value(name)
×
80

81
    def items(self):
1✔
82
        """Return all attribute names and values in the collection.
83

84
            Yields:
85
                An iterator to tuple pairs of attribute names and values in the collection.
86
        """
87
        for name in self._cache:
1✔
88
            yield name, self._get_value(name)
×
89

90
    def _get_names(self):
1✔
91
        # type: () -> typing.List[typing.Text]
92
        mode = constants.GetDbcAttributeMode.ATTRIBUTE_LIST
93
        attribute_size = _funcs.nxdb_get_dbc_attribute_size(self._handle, mode, '')
94
        attribute_info = _funcs.nxdb_get_dbc_attribute(self._handle, mode, '', attribute_size)
95
        name_string = attribute_info[0]
96
        name_list = [
1✔
97
            name
×
98
            for name in name_string.split(',')
99
            if name.strip()
1✔
100
        ]
101
        return name_list
1✔
102

1✔
103
    def _get_enums(self, name):
1✔
104
        # type: (typing.Text) -> typing.List[typing.Text]
1✔
105
        mode = constants.GetDbcAttributeMode.ENUMERATION_LIST
1✔
106
        attribute_size = _funcs.nxdb_get_dbc_attribute_size(self._handle, mode, name)
107
        attribute_info = _funcs.nxdb_get_dbc_attribute(self._handle, mode, name, attribute_size)
108
        enum_string = attribute_info[0]
109
        enum_list = [
110
            enum
1✔
111
            for enum in enum_string.split(',')
112
            if enum.strip()
1✔
113
        ]
114
        return enum_list
×
115

×
116
    def _get_value(self, name):
×
117
        # type: (typing.Text) -> typing.Tuple[typing.Text, bool]
×
118
        if name not in self._cache:
×
119
            raise KeyError('Attribute name %s not found in DBC attributes' % name)
120

121
        mode = constants.GetDbcAttributeMode.ATTRIBUTE
122
        attribute_size = _funcs.nxdb_get_dbc_attribute_size(self._handle, mode, name)
123
        attribute_info = _funcs.nxdb_get_dbc_attribute(self._handle, mode, name, attribute_size)
×
124
        enums = self._cache[name]
125
        if not enums:
1✔
126
            return attribute_info
127

1✔
128
        # This attribute is an enum. Replace the enum index with the enum string.
1✔
129
        index = int(attribute_info[0])
130
        attribute_info = (enums[index], attribute_info[1])
×
131
        return attribute_info
×
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