Skip to content

Commit

Permalink
fix: Fix errors reported by Mypy after enabling type checking for lxml
Browse files Browse the repository at this point in the history
  • Loading branch information
jtrobles-cdd committed Sep 25, 2024
1 parent c43f0e3 commit 91d5274
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/cl_sii/dte/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def parse_dte_xml(xml_doc: XmlElement) -> data_models.DteXmlData:
'ds:Signature', # "Firma Digital sobre Documento"
namespaces=xml_utils.XML_DSIG_NS_MAP,
)
assert signature_em is not None

if liquidacion_em is not None or exportaciones_em is not None:
raise NotImplementedError("XML element 'Documento' is the only one supported.")
Expand Down Expand Up @@ -191,6 +192,7 @@ def parse_dte_xml(xml_doc: XmlElement) -> data_models.DteXmlData:
'sii-dte:Encabezado', # "Identificacion y Totales del Documento"
namespaces=DTE_XMLNS_MAP,
)
assert encabezado_em is not None
# note: excluded because currently it is not useful.
# ted_em = documento_em.find(
# 'sii-dte:TED', # "Timbre Electronico de DTE"
Expand All @@ -215,18 +217,22 @@ def parse_dte_xml(xml_doc: XmlElement) -> data_models.DteXmlData:
'sii-dte:IdDoc', # "Identificacion del DTE"
namespaces=DTE_XMLNS_MAP,
)
assert id_doc_em is not None
emisor_em = encabezado_em.find(
'sii-dte:Emisor', # "Datos del Emisor"
namespaces=DTE_XMLNS_MAP,
)
assert emisor_em is not None
receptor_em = encabezado_em.find(
'sii-dte:Receptor', # "Datos del Receptor"
namespaces=DTE_XMLNS_MAP,
)
assert receptor_em is not None
totales_em = encabezado_em.find(
'sii-dte:Totales', # "Montos Totales del DTE"
namespaces=DTE_XMLNS_MAP,
)
assert totales_em is not None

# 'Documento.Encabezado.IdDoc'
# Excluded elements (optional according to the XML schema but the SII may require some of these
Expand Down Expand Up @@ -453,13 +459,15 @@ def parse_dte_xml(xml_doc: XmlElement) -> data_models.DteXmlData:
'ds:KeyInfo', # "Informacion de Claves Publicas y Certificado"
namespaces=xml_utils.XML_DSIG_NS_MAP,
)
assert signature_key_info_em is not None
# signature_key_info_key_value_em = signature_key_info_em.find(
# 'ds:KeyValue',
# namespaces=xml_utils.XML_DSIG_NS_MAP)
signature_key_info_x509_data_em = signature_key_info_em.find(
'ds:X509Data', # "Informacion del Certificado Publico"
namespaces=xml_utils.XML_DSIG_NS_MAP,
)
assert signature_key_info_x509_data_em is not None
signature_key_info_x509_cert_em = signature_key_info_x509_data_em.find(
'ds:X509Certificate', # "Certificado Publico"
namespaces=xml_utils.XML_DSIG_NS_MAP,
Expand Down Expand Up @@ -523,7 +531,7 @@ def parse_dte_xml(xml_doc: XmlElement) -> data_models.DteXmlData:
)


def _text_strip_or_none(xml_em: XmlElement) -> Optional[str]:
def _text_strip_or_none(xml_em: XmlElement | None) -> Optional[str]:
# note: we need the pair of functions '_text_strip_or_none' and '_text_strip_or_raise'
# because, under certain circumstances, an XML tag:
# - with no content -> `xml_em.text` is None instead of ''
Expand All @@ -539,7 +547,7 @@ def _text_strip_or_none(xml_em: XmlElement) -> Optional[str]:
return stripped_text


def _text_strip_or_raise(xml_em: XmlElement) -> str:
def _text_strip_or_raise(xml_em: XmlElement | None) -> str:
# note: we need the pair of functions '_text_strip_or_none' and '_text_strip_or_raise'
# because, under certain circumstances, an XML tag:
# - with no content -> `xml_em.text` is None instead of ''
Expand Down
12 changes: 12 additions & 0 deletions src/cl_sii/rtc/parse_aec.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ def parse_xml_to_dict(xml_em: XmlElement) -> Mapping[str, object]:
"""
# XPath: //Signature/KeyInfo
key_info_em = xml_em.find('ds:KeyInfo', namespaces=xml_utils.XML_DSIG_NS_MAP)
assert key_info_em is not None

# XPath: //Signature/KeyInfo/X509Data
key_info_x509_data_em = key_info_em.find(
'ds:X509Data', namespaces=xml_utils.XML_DSIG_NS_MAP
)
assert key_info_x509_data_em is not None

# XPath: //Signature
return dict(
Expand Down Expand Up @@ -474,14 +476,17 @@ def parse_xml_to_dict(xml_em: XmlElement) -> Mapping[str, object]:
"""
# XPath: /AEC/DocumentoAEC/Cesiones/Cesion/DocumentoCesion/IdDTE
id_dte_em = xml_em.find('sii-dte:IdDTE', namespaces=DTE_XMLNS_MAP)
assert id_dte_em is not None
id_dte_dict = _IdDte.parse_xml_to_dict(id_dte_em)

# XPath: /AEC/DocumentoAEC/Cesiones/Cesion/DocumentoCesion/Cedente
cedente_em = xml_em.find('sii-dte:Cedente', namespaces=DTE_XMLNS_MAP)
assert cedente_em is not None
cedente_dict = _Cedente.parse_xml_to_dict(cedente_em)

# XPath: /AEC/DocumentoAEC/Cesiones/Cesion/DocumentoCesion/Cesionario
cesionario_em = xml_em.find('sii-dte:Cesionario', namespaces=DTE_XMLNS_MAP)
assert cesionario_em is not None
cesionario_dict = _Cesionario.parse_xml_to_dict(cesionario_em)

# XPath: /AEC/DocumentoAEC/Cesiones/Cesion/DocumentoCesion
Expand Down Expand Up @@ -543,6 +548,7 @@ def parse_xml_to_dict(xml_em: XmlElement) -> Mapping[str, object]:
"""
# XPath: /AEC/DocumentoAEC/Cesiones/Cesion/DocumentoCesion
doc_cesion_em = xml_em.find('sii-dte:DocumentoCesion', namespaces=DTE_XMLNS_MAP)
assert doc_cesion_em is not None
doc_cesion_dict = _DocumentoCesion.parse_xml_to_dict(doc_cesion_em)

# Signature over 'DocumentoCesion'
Expand Down Expand Up @@ -689,6 +695,7 @@ def parse_xml_to_dict(xml_em: XmlElement) -> Mapping[str, object]:
'sii-dte:DocumentoDTECedido',
namespaces=DTE_XMLNS_MAP,
)
assert doc_dte_cedido_em is not None

# Signature over 'DocumentoDTECedido'
# XPath: /AEC/DocumentoAEC/Cesiones/DTECedido/Signature
Expand Down Expand Up @@ -819,13 +826,16 @@ def parse_xml_to_dict(xml_em: XmlElement) -> Mapping[str, object]:
"""
# XPath: /AEC/DocumentoAEC/Caratula
caratula_em = xml_em.find('sii-dte:Caratula', namespaces=DTE_XMLNS_MAP)
assert caratula_em is not None
caratula_dict = _Caratula.parse_xml_to_dict(caratula_em)

# XPath: /AEC/DocumentoAEC/Cesiones
cesiones_em = xml_em.find('sii-dte:Cesiones', namespaces=DTE_XMLNS_MAP)
assert cesiones_em is not None

# XPath: /AEC/DocumentoAEC/Cesiones/DTECedido
dte_cedido_em = cesiones_em.find('sii-dte:DTECedido', namespaces=DTE_XMLNS_MAP)
assert dte_cedido_em is not None
dte_cedido_dict = _DteCedido.parse_xml_to_dict(dte_cedido_em)

# XPath: /AEC/DocumentoAEC/Cesiones/Cesion
Expand Down Expand Up @@ -919,6 +929,7 @@ def parse_xml_to_dict(xml_doc: XmlElement) -> Mapping[str, object]:

# XPath: /AEC/DocumentoAEC
doc_aec_em = aec_em.find('sii-dte:DocumentoAEC', namespaces=DTE_XMLNS_MAP)
assert doc_aec_em is not None
doc_aec_dict = _DocumentoAec.parse_xml_to_dict(doc_aec_em)

# Signature over 'DocumentoAEC'
Expand All @@ -927,6 +938,7 @@ def parse_xml_to_dict(xml_doc: XmlElement) -> Mapping[str, object]:
'ds:Signature',
namespaces=xml_utils.XML_DSIG_NS_MAP,
)
assert signature_over_doc_aec_em is not None
signature_over_doc_aec_dict = _XmlSignature.parse_xml_to_dict(signature_over_doc_aec_em)

# XPath: /AEC
Expand Down

0 comments on commit 91d5274

Please sign in to comment.