From f5cef349106b3bc7b606f6c6a9bf9a8db107be0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Tue, 11 Sep 2018 00:28:46 +0200 Subject: [PATCH] Add annotations to modules/util.py --- gajim/common/modules/util.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/gajim/common/modules/util.py b/gajim/common/modules/util.py index 17dc5998d..4f74f7454 100644 --- a/gajim/common/modules/util.py +++ b/gajim/common/modules/util.py @@ -14,12 +14,15 @@ # Util module +from typing import Union + import nbxmpp from gajim.common import app -def is_self_message(message, groupchat=False): +def is_self_message(message: nbxmpp.Node, + groupchat: bool = False) -> bool: if groupchat: return False frm = message.getFrom() @@ -27,7 +30,9 @@ def is_self_message(message, groupchat=False): return frm.bareMatch(to) -def is_muc_pm(message, jid, groupchat=False): +def is_muc_pm(message: nbxmpp.Node, + jid: nbxmpp.JID, + groupchat: bool = False) -> bool: if groupchat: return False muc_user = message.getTag('x', namespace=nbxmpp.NS_MUC_USER) @@ -41,10 +46,10 @@ def is_muc_pm(message, jid, groupchat=False): return False -def from_xs_boolean(value): +def from_xs_boolean(value: Union[str, bool]) -> bool: # Convert a xs:boolean ('true', 'false', '1', '0', '') # to a python boolean (True, False) - if value in (True, False): + if isinstance(value, bool): return value if value in ('1', 'true'): @@ -58,16 +63,16 @@ def from_xs_boolean(value): 'Cant convert %s to python boolean' % value) -def to_xs_boolean(value): +def to_xs_boolean(value: Union[bool, None]) -> str: # Convert to xs:boolean ('true', 'false') # from a python boolean (True, False) or None - if value in ('true', 'false'): - return value - if value is True: return 'true' - if value in (False, None): + if value is False: + return 'false' + + if value is None: return 'false' raise ValueError(