Skip to content

Commit eff5099

Browse files
pbrezinaikerexxe
authored andcommitted
tools: fix types to satisfy mypy
``` sssd_test_framework/utils/tools.py:204: error: Argument "name" to "PasswdEntry" has incompatible type "str | int | None"; expected "str" [arg-type] sssd_test_framework/utils/tools.py:205: error: Argument "password" to "PasswdEntry" has incompatible type "str | int | None"; expected "str" [arg-type] sssd_test_framework/utils/tools.py:206: error: Argument "uid" to "PasswdEntry" has incompatible type "str | int | None"; expected "int" [arg-type] sssd_test_framework/utils/tools.py:207: error: Argument "gid" to "PasswdEntry" has incompatible type "str | int | None"; expected "int" [arg-type] sssd_test_framework/utils/tools.py:208: error: Argument "gecos" to "PasswdEntry" has incompatible type "str | int | None"; expected "str" [arg-type] sssd_test_framework/utils/tools.py:209: error: Argument "home" to "PasswdEntry" has incompatible type "str | int | None"; expected "str" [arg-type] sssd_test_framework/utils/tools.py:210: error: Argument "shell" to "PasswdEntry" has incompatible type "str | int | None"; expected "str" [arg-type] sssd_test_framework/utils/tools.py:261: error: Argument "name" to "GroupEntry" has incompatible type "Any | None"; expected "str" [arg-type] sssd_test_framework/utils/tools.py:262: error: Argument "password" to "GroupEntry" has incompatible type "Any | None"; expected "str" [arg-type] sssd_test_framework/utils/tools.py:263: error: Argument "gid" to "GroupEntry" has incompatible type "Any | None"; expected "int" [arg-type] ```
1 parent fb33da7 commit eff5099

1 file changed

Lines changed: 73 additions & 18 deletions

File tree

sssd_test_framework/utils/tools.py

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,16 @@ class PasswdEntry(object):
156156
Result of ``getent passwd``
157157
"""
158158

159-
def __init__(self, name: str, password: str, uid: int, gid: int, gecos: str, home: str, shell: str) -> None:
159+
def __init__(
160+
self,
161+
name: str | None,
162+
password: str | None,
163+
uid: int | None,
164+
gid: int | None,
165+
gecos: str | None,
166+
home: str | None,
167+
shell: str | None,
168+
) -> None:
160169
self.name: str | None = name
161170
"""
162171
User name.
@@ -167,12 +176,12 @@ def __init__(self, name: str, password: str, uid: int, gid: int, gecos: str, hom
167176
User password.
168177
"""
169178

170-
self.uid: int = uid
179+
self.uid: int | None = uid
171180
"""
172181
User id.
173182
"""
174183

175-
self.gid: int = gid
184+
self.gid: int | None = gid
176185
"""
177186
Group id.
178187
"""
@@ -199,15 +208,44 @@ def __repr__(self) -> str:
199208
return str(self)
200209

201210
@classmethod
202-
def FromDict(cls, d: dict[str, Any]) -> PasswdEntry:
211+
def FromDict(cls, d: dict[str, str | int | None]) -> PasswdEntry:
212+
username = d.get("username", None)
213+
password = d.get("password", None)
214+
uid = d.get("uid", None)
215+
gid = d.get("gid", None)
216+
comment = d.get("comment", None)
217+
home = d.get("home", None)
218+
shell = d.get("shell", None)
219+
220+
if username is not None and not isinstance(username, str):
221+
raise ValueError("username is not instance of str")
222+
223+
if password is not None and not isinstance(password, str):
224+
raise ValueError("password is not instance of str")
225+
226+
if uid is not None and not isinstance(uid, int):
227+
raise ValueError("uid is not instance of int")
228+
229+
if gid is not None and not isinstance(gid, int):
230+
raise ValueError("gid is not instance of int")
231+
232+
if comment is not None and not isinstance(comment, str):
233+
raise ValueError("comment is not instance of str")
234+
235+
if home is not None and not isinstance(home, str):
236+
raise ValueError("home is not instance of str")
237+
238+
if shell is not None and not isinstance(shell, str):
239+
raise ValueError("shell is not instance of str")
240+
203241
return cls(
204-
name=d.get("username", None),
205-
password=d.get("password", None),
206-
uid=d.get("uid", None),
207-
gid=d.get("gid", None),
208-
gecos=d.get("comment", None),
209-
home=d.get("home", None),
210-
shell=d.get("shell", None),
242+
name=username,
243+
password=password,
244+
uid=uid,
245+
gid=gid,
246+
gecos=comment,
247+
home=home,
248+
shell=shell,
211249
)
212250

213251
@classmethod
@@ -228,7 +266,7 @@ class GroupEntry(object):
228266
Result of ``getent group``
229267
"""
230268

231-
def __init__(self, name: str, password: str, gid: int, members: list[str]) -> None:
269+
def __init__(self, name: str | None, password: str | None, gid: int | None, members: list[str]) -> None:
232270
self.name: str | None = name
233271
"""
234272
Group name.
@@ -239,7 +277,7 @@ def __init__(self, name: str, password: str, gid: int, members: list[str]) -> No
239277
Group password.
240278
"""
241279

242-
self.gid: int = gid
280+
self.gid: int | None = gid
243281
"""
244282
Group id.
245283
"""
@@ -256,12 +294,29 @@ def __repr__(self) -> str:
256294
return str(self)
257295

258296
@classmethod
259-
def FromDict(cls, d: dict[str, Any]) -> GroupEntry:
297+
def FromDict(cls, d: dict[str, str | int | list[str] | None]) -> GroupEntry:
298+
group_name = d.get("group_name", None)
299+
password = d.get("password", None)
300+
gid = d.get("gid", None)
301+
members = d.get("members", [])
302+
303+
if group_name is not None and not isinstance(group_name, str):
304+
raise ValueError("group_name is not instance of str")
305+
306+
if password is not None and not isinstance(password, str):
307+
raise ValueError("password is not instance of str")
308+
309+
if gid is not None and not isinstance(gid, int):
310+
raise ValueError("gid is not instance of int")
311+
312+
if members is not None or not isinstance(members, list):
313+
raise ValueError("members is not instance of list")
314+
260315
return cls(
261-
name=d.get("group_name", None),
262-
password=d.get("password", None),
263-
gid=d.get("gid", None),
264-
members=d.get("members", []),
316+
name=group_name,
317+
password=password,
318+
gid=gid,
319+
members=members,
265320
)
266321

267322
@classmethod

0 commit comments

Comments
 (0)