1
1
# SPDX-License-Identifier: MIT
2
2
import unittest
3
3
from dataclasses import dataclass
4
+ from typing import List
4
5
5
6
import odxtools
6
7
from odxtools .exceptions import OdxError
@@ -97,7 +98,7 @@ class X:
97
98
value : int
98
99
99
100
foo = NamedItemList ([X ("hello" , 0 ), X ("world" , 1 )])
100
- self .assertEqual (foo .hello , X ("hello" , 0 )) # type: ignore[attr-defined]
101
+ self .assertEqual (foo .hello , X ("hello" , 0 ))
101
102
self .assertEqual (foo [0 ], X ("hello" , 0 ))
102
103
self .assertEqual (foo [1 ], X ("world" , 1 ))
103
104
self .assertEqual (foo [:1 ], [X ("hello" , 0 )])
@@ -106,31 +107,31 @@ class X:
106
107
foo [2 ]
107
108
self .assertEqual (foo ["hello" ], X ("hello" , 0 ))
108
109
self .assertEqual (foo ["world" ], X ("world" , 1 ))
109
- self .assertEqual (foo .hello , X ("hello" , 0 )) # type: ignore[attr-defined]
110
- self .assertEqual (foo .world , X ("world" , 1 )) # type: ignore[attr-defined]
110
+ self .assertEqual (foo .hello , X ("hello" , 0 ))
111
+ self .assertEqual (foo .world , X ("world" , 1 ))
111
112
112
113
foo .append (X ("hello" , 2 ))
113
114
self .assertEqual (foo [2 ], X ("hello" , 2 ))
114
115
self .assertEqual (foo ["hello" ], X ("hello" , 0 ))
115
116
self .assertEqual (foo ["hello_2" ], X ("hello" , 2 ))
116
- self .assertEqual (foo .hello , X ("hello" , 0 )) # type: ignore[attr-defined]
117
- self .assertEqual (foo .hello_2 , X ("hello" , 2 )) # type: ignore[attr-defined]
117
+ self .assertEqual (foo .hello , X ("hello" , 0 ))
118
+ self .assertEqual (foo .hello_2 , X ("hello" , 2 ))
118
119
119
120
# try to append an item that cannot be mapped to a name
120
121
with self .assertRaises (OdxError ):
121
- foo .append ((0 , 3 )) # type: ignore[arg-type]
122
+ foo .append ((0 , 3 )) # type: ignore[arg-type]
122
123
123
124
# add a keyword identifier
124
125
foo .append (X ("as" , 3 ))
125
126
self .assertEqual (foo [3 ], X ("as" , 3 ))
126
127
self .assertEqual (foo ["_as" ], X ("as" , 3 ))
127
- self .assertEqual (foo ._as , X ("as" , 3 )) # type: ignore[attr-defined]
128
+ self .assertEqual (foo ._as , X ("as" , 3 ))
128
129
129
130
# add an object which's name conflicts with a method of the class
130
131
foo .append (X ("sort" , 4 ))
131
132
self .assertEqual (foo [4 ], X ("sort" , 4 ))
132
133
self .assertEqual (foo ["sort_2" ], X ("sort" , 4 ))
133
- self .assertEqual (foo .sort_2 , X ("sort" , 4 )) # type: ignore[attr-defined]
134
+ self .assertEqual (foo .sort_2 , X ("sort" , 4 ))
134
135
135
136
# test the get() function
136
137
self .assertEqual (foo .get (0 ), X ("hello" , 0 ))
@@ -150,6 +151,13 @@ class X:
150
151
self .assertEqual (len (foo .items ()), len (foo ))
151
152
self .assertEqual (len (foo .values ()), len (foo ))
152
153
154
+ # ensure that mypy accepts NamedItemList objecs where List
155
+ # objects are expected
156
+ def bar (x : List [X ]) -> None :
157
+ pass
158
+
159
+ bar (foo )
160
+
153
161
154
162
class TestNavigation (unittest .TestCase ):
155
163
0 commit comments