Skip to content

Commit 9b04ac9

Browse files
committed
more typing
1 parent bed8574 commit 9b04ac9

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

src/pyff/pipes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import functools
88
import os
99
import traceback
10-
from typing import Any, Callable, Dict, Iterable, List, Optional
10+
from typing import Any, Callable, Dict, Iterable, Optional
1111
from typing import Type
1212

1313
import yaml

src/pyff/repo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from .constants import config
44
from .logs import get_log
5-
from .resource import IconHandler, Resource, ResourceOpts
5+
from .resource import Resource, ResourceOpts
66
from .samlmd import entitiesdescriptor, root
77
from .store import make_icon_store_instance, make_store_instance
88
from .utils import is_text, make_default_scheduler
@@ -52,7 +52,7 @@ def lookup(self, member, xp=None, store=None):
5252
:param xp: An optional xpath filter
5353
:type xp: basestring
5454
:param store: the store to operate on
55-
:return: An interable of EntityDescriptor elements
55+
:return: An iterable of EntityDescriptor elements
5656
:rtype: etree.Element
5757
5858
@@ -66,7 +66,7 @@ def lookup(self, member, xp=None, store=None):
6666
6767
The first form results in the intersection of the results of doing a lookup on the selectors. The second form
6868
results in the EntityDescriptor elements from the source (defaults to all EntityDescriptors) that match the
69-
xpath expression. The attribute-value forms resuls in the EntityDescriptors that contain the specified entity
69+
xpath expression. The attribute-value forms results in the EntityDescriptors that contain the specified entity
7070
attribute pair. If non of these forms apply, the lookup is done using either source ID (normally @Name from
7171
the EntitiesDescriptor) or the entityID of single EntityDescriptors. If member is a URI but isn't part of
7272
the metadata repository then it is fetched an treated as a list of (one per line) of selectors. If all else

src/pyff/resource.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from collections import deque
1111
from datetime import datetime
1212
from threading import Condition, Lock
13-
from typing import Any, Callable, Deque, Dict, List, Optional, TYPE_CHECKING, Tuple, Union
13+
from typing import Any, Callable, Deque, Dict, Iterable, List, Optional, TYPE_CHECKING, Tuple, Union
1414
from urllib.parse import quote as urlescape
1515

1616
import requests
@@ -22,11 +22,22 @@
2222
from .exceptions import ResourceException
2323
from .fetch import make_fetcher
2424
from .logs import get_log
25-
from .utils import Watchable, hex_digest, img_to_data, non_blocking_lock, resource_string, safe_write, url_get, utc_now
25+
26+
from .utils import (
27+
Watchable,
28+
hex_digest,
29+
img_to_data,
30+
non_blocking_lock,
31+
resource_string,
32+
safe_write,
33+
url_get,
34+
utc_now,
35+
)
2636

2737
if TYPE_CHECKING:
2838
from .parse import PyffParser
2939
from .pipes import PipelineCallback
40+
from .utils import Lambda
3041

3142
requests.packages.urllib3.disable_warnings()
3243

@@ -140,15 +151,20 @@ class ResourceOpts(BaseModel):
140151
alias: Optional[str] = Field(None, alias='as') # TODO: Rename to 'name'?
141152
# a certificate (file) or a SHA1 fingerprint to use for signature verification
142153
verify: Optional[str] = None
143-
via: List[Any] = Field([]) # list of PipelineCallback
144-
# A list of PipelineCallback that can be used to pre-process parsed metadata before validation. Use as a clue-bat.
145-
cleanup: List[Any] = Field([]) # list of PipelineCallback
154+
# TODO: move classes to make the correct typing work: Iterable[Union[Lambda, PipelineCallback]] = Field([])
155+
via: List[Callable] = Field([])
156+
# A list of callbacks that can be used to pre-process parsed metadata before validation. Use as a clue-bat.
157+
# TODO: sort imports to make the correct typing work: Iterable[PipelineCallback] = Field([])
158+
cleanup: List[Callable] = Field([])
146159
fail_on_error: bool = False
147160
# remove invalid EntityDescriptor elements rather than raise an error
148161
filter_invalid: bool = True
149162
# set to False to turn off all XML schema validation
150163
validate_schema: bool = Field(True, alias='validate')
151164

165+
class Config:
166+
arbitrary_types_allowed = True
167+
152168
def to_dict(self) -> Dict[str, Any]:
153169
res = self.dict()
154170
# Compensate for the 'alias' field options
@@ -198,14 +214,17 @@ def local_copy_fn(self):
198214
return os.path.join(config.local_copy_dir, urlescape(self.url))
199215

200216
@property
201-
def post(self) -> List['PipelineCallback']:
217+
def post(
218+
self,
219+
) -> Iterable[Callable]: # TODO: move classes to make this work -> List[Union['Lambda', 'PipelineCallback']]:
202220
return self.opts.via
203221

204-
def add_via(self, callback: 'PipelineCallback') -> None:
222+
def add_via(self, callback: Callable) -> None:
223+
# TODO: move classes to be able to declare callback: Union['Lambda', 'PipelineCallback']
205224
self.opts.via.append(callback)
206225

207226
@property
208-
def cleanup(self) -> List['PipelineCallback']:
227+
def cleanup(self) -> Iterable[Callable]: # TODO: move classes to make this work -> Iterable['PipelineCallback']:
209228
return self.opts.cleanup
210229

211230
def __str__(self):

src/pyff/utils.py

Lines changed: 2 additions & 2 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 BinaryIO, Optional, Union
27+
from typing import Any, BinaryIO, Callable, Optional, Union
2828

2929
import pkg_resources
3030
import requests
@@ -818,7 +818,7 @@ def json_serializer(o):
818818

819819

820820
class Lambda(object):
821-
def __init__(self, cb, *args, **kwargs):
821+
def __init__(self, cb: Callable, *args, **kwargs):
822822
self._cb = cb
823823
self._args = [a for a in args]
824824
self._kwargs = kwargs or {}

0 commit comments

Comments
 (0)