Skip to content

Commit 3f1cba2

Browse files
committed
more typing
1 parent 591ada7 commit 3f1cba2

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

src/pyff/samlmd.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import traceback
22
from copy import deepcopy
3-
from datetime import datetime
3+
from datetime import datetime, timedelta
44
from distutils.util import strtobool
5+
from io import BytesIO
56
from itertools import chain
6-
from typing import Any, Dict, Mapping
7+
from typing import Any, Dict, Mapping, Optional, Tuple
78

89
from lxml import etree
910
from lxml.builder import ElementMaker
10-
from lxml.etree import DocumentInvalid
11+
from lxml.etree import DocumentInvalid, ElementTree
1112
from xmlsec.crypto import CertDict
1213

1314
from .constants import ATTRS, NF_URI, NS, config
@@ -80,15 +81,15 @@ def find_merge_strategy(strategy_name):
8081

8182

8283
def parse_saml_metadata(
83-
source,
84-
key=None,
85-
base_url=None,
86-
fail_on_error=False,
87-
filter_invalid=True,
84+
source: BytesIO,
85+
key: Optional[str] = None,
86+
base_url: Optional[str] = None,
87+
fail_on_error: bool = False,
88+
filter_invalid: bool = True,
8889
cleanup=None,
89-
validate=True,
90-
validation_errors=None,
91-
):
90+
validate: bool = True,
91+
validation_errors: Optional[Dict[str, Any]] = None,
92+
) -> Tuple[ElementTree, Optional[timedelta], Optional[Exception]]:
9293
"""Parse a piece of XML and return an EntitiesDescriptor element after validation.
9394
9495
:param source: a file-like object containing SAML metadata
@@ -100,6 +101,7 @@ def parse_saml_metadata(
100101
:param validation_errors: A dict that will be used to return validation errors to the caller
101102
:param cleanup: A list of callables that can be used to pre-process parsed metadata before validation. Use as a clue-bat.
102103
104+
:return: Tuple with t (ElementTree), expire_time_offset, exception
103105
"""
104106

105107
if validation_errors is None:
@@ -283,7 +285,7 @@ def _update_entities(_t, **kwargs):
283285
add_parser(MDServiceListParser())
284286

285287

286-
def metadata_expiration(t):
288+
def metadata_expiration(t: ElementTree) -> Optional[timedelta]:
287289
relt = root(t)
288290
if relt.tag in ('{%s}EntityDescriptor' % NS['md'], '{%s}EntitiesDescriptor' % NS['md']):
289291
cache_duration = config.default_cache_duration
@@ -316,7 +318,11 @@ def filter_invalids_from_document(t, base_url, validation_errors):
316318
return t
317319

318320

319-
def filter_or_validate(t, filter_invalid=False, base_url="", source=None, validation_errors=dict()):
321+
def filter_or_validate(
322+
t, filter_invalid: bool = False, base_url: str = "", source=None, validation_errors: Optional[Dict[str, Any]] = None
323+
):
324+
if validation_errors is None:
325+
validation_errors = {}
320326
log.debug("Filtering invalids from {}".format(base_url))
321327
if filter_invalid:
322328
t = filter_invalids_from_document(t, base_url=base_url, validation_errors=validation_errors)

src/pyff/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from itertools import chain
2525
from threading import local
2626
from time import gmtime, strftime
27-
from typing import Optional, Union
27+
from typing import BinaryIO, Optional, Union
2828

2929
import pkg_resources
3030
import requests
@@ -36,6 +36,7 @@
3636
from apscheduler.schedulers.background import BackgroundScheduler
3737
from cachetools import LRUCache
3838
from lxml import etree
39+
from lxml.etree import ElementTree
3940
from requests import Session
4041
from requests.adapters import BaseAdapter, HTTPAdapter, Response
4142
from requests.packages.urllib3.util.retry import Retry
@@ -263,7 +264,7 @@ def redis():
263264
return thread_data.redis
264265

265266

266-
def check_signature(t, key, only_one_signature=False):
267+
def check_signature(t: ElementTree, key: Optional[str], only_one_signature: bool = False) -> ElementTree:
267268
if key is not None:
268269
log.debug("verifying signature using %s" % key)
269270
refs = xmlsec.verified(t, key, drop_signature=True)
@@ -512,7 +513,7 @@ def hex_digest(data, hn='sha1'):
512513
return m.hexdigest()
513514

514515

515-
def parse_xml(io, base_url=None):
516+
def parse_xml(io: BinaryIO, base_url: Optional[str] = None) -> ElementTree:
516517
huge_xml = config.huge_xml
517518
return etree.parse(
518519
io, base_url=base_url, parser=etree.XMLParser(resolve_entities=False, collect_ids=False, huge_tree=huge_xml)

0 commit comments

Comments
 (0)