stanza: fix a bunch of type errors

This commit is contained in:
mathieui 2021-07-05 22:29:37 +02:00
parent 0971bab30a
commit af958fd1fe
6 changed files with 25 additions and 17 deletions

View File

@ -3,7 +3,7 @@
# This file is part of Slixmpp. # This file is part of Slixmpp.
# See the file LICENSE for copying permission. # See the file LICENSE for copying permission.
from __future__ import annotations from __future__ import annotations
from typing import Optional from typing import Optional, Dict, Type, ClassVar
from slixmpp.xmlstream import ElementBase, ET from slixmpp.xmlstream import ElementBase, ET
@ -52,8 +52,8 @@ class Error(ElementBase):
interfaces = {'code', 'condition', 'text', 'type', interfaces = {'code', 'condition', 'text', 'type',
'gone', 'redirect', 'by'} 'gone', 'redirect', 'by'}
sub_interfaces = {'text'} sub_interfaces = {'text'}
plugin_attrib_map = {} plugin_attrib_map: ClassVar[Dict[str, Type[ElementBase]]] = {}
plugin_tag_map = {} plugin_tag_map: ClassVar[Dict[str, Type[ElementBase]]] = {}
conditions = {'bad-request', 'conflict', 'feature-not-implemented', conditions = {'bad-request', 'conflict', 'feature-not-implemented',
'forbidden', 'gone', 'internal-server-error', 'forbidden', 'gone', 'internal-server-error',
'item-not-found', 'jid-malformed', 'not-acceptable', 'item-not-found', 'jid-malformed', 'not-acceptable',
@ -83,7 +83,9 @@ class Error(ElementBase):
self['type'] = 'cancel' self['type'] = 'cancel'
self['condition'] = 'feature-not-implemented' self['condition'] = 'feature-not-implemented'
if self.parent is not None: if self.parent is not None:
self.parent()['type'] = 'error' parent = self.parent()
if parent:
parent['type'] = 'error'
def get_condition(self) -> str: def get_condition(self) -> str:
"""Return the condition element's name.""" """Return the condition element's name."""
@ -106,7 +108,7 @@ class Error(ElementBase):
self.xml.append(ET.Element("{%s}%s" % (self.condition_ns, value))) self.xml.append(ET.Element("{%s}%s" % (self.condition_ns, value)))
return self return self
def del_condition(self) -> None: def del_condition(self) -> Error:
"""Remove the condition element.""" """Remove the condition element."""
for child in self.xml: for child in self.xml:
if "{%s}" % self.condition_ns in child.tag: if "{%s}" % self.condition_ns in child.tag:

View File

@ -4,6 +4,7 @@
# See the file LICENSE for copying permission. # See the file LICENSE for copying permission.
from slixmpp.xmlstream import StanzaBase from slixmpp.xmlstream import StanzaBase
from typing import Optional
class Handshake(StanzaBase): class Handshake(StanzaBase):
@ -18,7 +19,7 @@ class Handshake(StanzaBase):
def set_value(self, value: str): def set_value(self, value: str):
self.xml.text = value self.xml.text = value
def get_value(self) -> str: def get_value(self) -> Optional[str]:
return self.xml.text return self.xml.text
def del_value(self): def del_value(self):

View File

@ -5,6 +5,7 @@
# See the file LICENSE for copying permission. # See the file LICENSE for copying permission.
from slixmpp.stanza.rootstanza import RootStanza from slixmpp.stanza.rootstanza import RootStanza
from slixmpp.xmlstream import StanzaBase, ET from slixmpp.xmlstream import StanzaBase, ET
from slixmpp.basexmpp import BaseXMPP
ORIGIN_NAME = '{urn:xmpp:sid:0}origin-id' ORIGIN_NAME = '{urn:xmpp:sid:0}origin-id'
@ -61,7 +62,7 @@ class Message(RootStanza):
""" """
StanzaBase.__init__(self, *args, **kwargs) StanzaBase.__init__(self, *args, **kwargs)
if not recv and self['id'] == '': if not recv and self['id'] == '':
if self.stream is not None and self.stream.use_message_ids: if isinstance(self.stream, BaseXMPP) and self.stream.use_message_ids:
self['id'] = self.stream.new_id() self['id'] = self.stream.new_id()
else: else:
del self['origin_id'] del self['origin_id']
@ -93,7 +94,7 @@ class Message(RootStanza):
self.xml.attrib['id'] = value self.xml.attrib['id'] = value
if self.stream and not self.stream.use_origin_id: if isinstance(self.stream, BaseXMPP) and not self.stream.use_origin_id:
return None return None
sub = self.xml.find(ORIGIN_NAME) sub = self.xml.find(ORIGIN_NAME)

View File

@ -5,6 +5,7 @@
# See the file LICENSE for copying permission. # See the file LICENSE for copying permission.
from slixmpp.stanza.rootstanza import RootStanza from slixmpp.stanza.rootstanza import RootStanza
from slixmpp.xmlstream import StanzaBase from slixmpp.xmlstream import StanzaBase
from slixmpp.basexmpp import BaseXMPP
class Presence(RootStanza): class Presence(RootStanza):
@ -69,7 +70,7 @@ class Presence(RootStanza):
""" """
StanzaBase.__init__(self, *args, **kwargs) StanzaBase.__init__(self, *args, **kwargs)
if not recv and self['id'] == '': if not recv and self['id'] == '':
if self.stream is not None and self.stream.use_presence_ids: if isinstance(self.stream, BaseXMPP) and self.stream.use_presence_ids:
self['id'] = self.stream.new_id() self['id'] = self.stream.new_id()
def set_show(self, show: str): def set_show(self, show: str):

View File

@ -4,7 +4,8 @@
# This file is part of Slixmpp. # This file is part of Slixmpp.
# See the file LICENSE for copying permission. # See the file LICENSE for copying permission.
from slixmpp.stanza.error import Error from slixmpp.stanza.error import Error
from slixmpp.xmlstream import StanzaBase from slixmpp.xmlstream import StanzaBase, ET
from typing import Optional, Dict, Union
class StreamError(Error, StanzaBase): class StreamError(Error, StanzaBase):
@ -64,17 +65,18 @@ class StreamError(Error, StanzaBase):
'unsupported-version'} 'unsupported-version'}
condition_ns: str = 'urn:ietf:params:xml:ns:xmpp-streams' condition_ns: str = 'urn:ietf:params:xml:ns:xmpp-streams'
def get_see_other_host(self) -> str: def get_see_other_host(self) -> Union[str, Dict[str, str]]:
ns = self.condition_ns ns = self.condition_ns
return self._get_sub_text('{%s}see-other-host' % ns, '') return self._get_sub_text('{%s}see-other-host' % ns, '')
def set_see_other_host(self, value: str) -> None: def set_see_other_host(self, value: str) -> Optional[ET.Element]:
if value: if value:
del self['condition'] del self['condition']
ns = self.condition_ns ns = self.condition_ns
return self._set_sub_text('{%s}see-other-host' % ns, value) return self._set_sub_text('{%s}see-other-host' % ns, value)
elif self['condition'] == 'see-other-host': elif self['condition'] == 'see-other-host':
del self['condition'] del self['condition']
return None
def del_see_other_host(self) -> None: def del_see_other_host(self) -> None:
self._del_sub('{%s}see-other-host' % self.condition_ns) self._del_sub('{%s}see-other-host' % self.condition_ns)

View File

@ -3,7 +3,8 @@
# Copyright (C) 2010 Nathanael C. Fritz # Copyright (C) 2010 Nathanael C. Fritz
# This file is part of Slixmpp. # This file is part of Slixmpp.
# See the file LICENSE for copying permission. # See the file LICENSE for copying permission.
from slixmpp.xmlstream import StanzaBase from slixmpp.xmlstream import StanzaBase, ElementBase
from typing import ClassVar, Dict, Type
class StreamFeatures(StanzaBase): class StreamFeatures(StanzaBase):
@ -15,8 +16,8 @@ class StreamFeatures(StanzaBase):
namespace = 'http://etherx.jabber.org/streams' namespace = 'http://etherx.jabber.org/streams'
interfaces = {'features', 'required', 'optional'} interfaces = {'features', 'required', 'optional'}
sub_interfaces = interfaces sub_interfaces = interfaces
plugin_tag_map = {} plugin_attrib_map: ClassVar[Dict[str, Type[ElementBase]]] = {}
plugin_attrib_map = {} plugin_tag_map: ClassVar[Dict[str, Type[ElementBase]]] = {}
def setup(self, xml): def setup(self, xml):
StanzaBase.setup(self, xml) StanzaBase.setup(self, xml)