Skip to content

Commit 7ea1955

Browse files
hallvictoriaVictoria Hall
and
Victoria Hall
authored
Linting base extension (#17)
* black & isort * init & test_web missed lint * merge fix --------- Co-authored-by: Victoria Hall <[email protected]>
1 parent c03582f commit 7ea1955

File tree

9 files changed

+449
-340
lines changed

9 files changed

+449
-340
lines changed

azure-functions-extension-base/azure/functions/extension/base/__init__.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,38 @@
33

44
from .meta import (
55
Datum,
6-
_ConverterMeta,
7-
_BaseConverter,
86
InConverter,
97
OutConverter,
8+
_BaseConverter,
9+
_ConverterMeta,
1010
get_binding_registry,
1111
)
1212
from .sdkType import SdkType
1313
from .web import (
14-
WebServer,
15-
WebApp,
14+
HttpV2FeatureChecker,
1615
ModuleTrackerMeta,
1716
RequestTrackerMeta,
17+
ResponseLabels,
1818
ResponseTrackerMeta,
19-
HttpV2FeatureChecker,
20-
ResponseLabels
19+
WebApp,
20+
WebServer,
2121
)
2222

2323
__all__ = [
24-
'Datum',
25-
'_ConverterMeta',
26-
'_BaseConverter',
27-
'InConverter',
28-
'OutConverter',
29-
'SdkType',
30-
'get_binding_registry',
31-
'ModuleTrackerMeta',
32-
'RequestTrackerMeta',
33-
'ResponseTrackerMeta',
34-
'HttpV2FeatureChecker',
35-
'ResponseLabels',
36-
'WebServer',
37-
'WebApp'
24+
"Datum",
25+
"_ConverterMeta",
26+
"_BaseConverter",
27+
"InConverter",
28+
"OutConverter",
29+
"SdkType",
30+
"get_binding_registry",
31+
"ModuleTrackerMeta",
32+
"RequestTrackerMeta",
33+
"ResponseTrackerMeta",
34+
"HttpV2FeatureChecker",
35+
"ResponseLabels",
36+
"WebServer",
37+
"WebApp",
3838
]
3939

4040
__version__ = "1.0.0a2"

azure-functions-extension-base/azure/functions/extension/base/meta.py

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import abc
55
import inspect
66
import json
7+
from typing import Any, Dict, List, Mapping, Optional, Tuple, Union
78

8-
from . import utils
9-
from . import sdkType
10-
11-
from typing import Any, Dict, List, Optional, Mapping, Union, Tuple
9+
from . import sdkType, utils
1210

1311

1412
class Datum:
@@ -20,17 +18,17 @@ def __init__(self, value: Any, type: Optional[str]):
2018
def python_value(self) -> Any:
2119
if self.value is None or self.type is None:
2220
return None
23-
elif self.type in ('bytes', 'string', 'int', 'double'):
21+
elif self.type in ("bytes", "string", "int", "double"):
2422
return self.value
25-
elif self.type == 'json':
23+
elif self.type == "json":
2624
return json.loads(self.value)
27-
elif self.type == 'collection_string':
25+
elif self.type == "collection_string":
2826
return [v for v in self.value.string]
29-
elif self.type == 'collection_bytes':
27+
elif self.type == "collection_bytes":
3028
return [v for v in self.value.bytes]
31-
elif self.type == 'collection_double':
29+
elif self.type == "collection_double":
3230
return [v for v in self.value.double]
33-
elif self.type == 'collection_sint64':
31+
elif self.type == "collection_sint64":
3432
return [v for v in self.value.sint64]
3533
else:
3634
return self.value
@@ -51,27 +49,28 @@ def __hash__(self):
5149
def __repr__(self):
5250
val_repr = repr(self.value)
5351
if len(val_repr) > 10:
54-
val_repr = val_repr[:10] + '...'
55-
return '<Datum {} {}>'.format(self.type, val_repr)
52+
val_repr = val_repr[:10] + "..."
53+
return "<Datum {} {}>".format(self.type, val_repr)
5654

5755

5856
class _ConverterMeta(abc.ABCMeta):
5957

6058
_bindings: Dict[str, type] = {}
6159

62-
def __new__(mcls, name, bases, dct, *,
63-
binding: Optional[str],
64-
trigger: Optional[str] = None):
60+
def __new__(
61+
mcls, name, bases, dct, *, binding: Optional[str], trigger: Optional[str] = None
62+
):
6563
cls = super().__new__(mcls, name, bases, dct)
6664
cls._trigger = trigger # type: ignore
6765
if binding is None:
6866
return cls
6967

7068
if binding in mcls._bindings:
7169
raise RuntimeError(
72-
f'cannot register a converter for {binding!r} binding: '
73-
f'another converter for this binding has already been '
74-
f'registered')
70+
f"cannot register a converter for {binding!r} binding: "
71+
f"another converter for this binding has already been "
72+
f"registered"
73+
)
7574

7675
mcls._bindings[binding] = cls
7776
if trigger is not None:
@@ -101,51 +100,59 @@ class _BaseConverter(metaclass=_ConverterMeta, binding=None):
101100

102101
@classmethod
103102
def _decode_typed_data(
104-
cls, data: Datum, *,
105-
python_type: Union[type, Tuple[type, ...]],
106-
context: str = 'data') -> Any:
103+
cls,
104+
data: Datum,
105+
*,
106+
python_type: Union[type, Tuple[type, ...]],
107+
context: str = "data",
108+
) -> Any:
107109
if data is None:
108110
return None
109111

110112
data_type = data.type
111-
if data_type == 'model_binding_data':
113+
if data_type == "model_binding_data":
112114
result = data.value
113115
elif data_type is None:
114116
return None
115117
else:
116-
raise ValueError(
117-
f'unsupported type of {context}: {data_type}')
118+
raise ValueError(f"unsupported type of {context}: {data_type}")
118119

119120
if not isinstance(result, python_type):
120121
if isinstance(python_type, (tuple, list, dict)):
121122
raise ValueError(
122-
f'unexpected value type in {context}: '
123-
f'{type(result).__name__}, expected one of: '
124-
f'{", ".join(t.__name__ for t in python_type)}')
123+
f"unexpected value type in {context}: "
124+
f"{type(result).__name__}, expected one of: "
125+
f'{", ".join(t.__name__ for t in python_type)}'
126+
)
125127
else:
126128
try:
127129
# Try coercing into the requested type
128130
result = python_type(result)
129131
except (TypeError, ValueError) as e:
130132
raise ValueError(
131-
f'cannot convert value of {context} into '
132-
f'{python_type.__name__}: {e}') from None
133+
f"cannot convert value of {context} into "
134+
f"{python_type.__name__}: {e}"
135+
) from None
133136

134137
return result
135138

136139
@classmethod
137140
def _decode_trigger_metadata_field(
138-
cls, trigger_metadata: Mapping[str, Datum],
139-
field: str, *,
140-
python_type: Union[type, Tuple[type, ...]]) \
141-
-> Any:
141+
cls,
142+
trigger_metadata: Mapping[str, Datum],
143+
field: str,
144+
*,
145+
python_type: Union[type, Tuple[type, ...]],
146+
) -> Any:
142147
data = trigger_metadata.get(field)
143148
if data is None:
144149
return None
145150
else:
146151
return cls._decode_typed_data(
147-
data, python_type=python_type,
148-
context=f'field {field!r} in trigger metadata')
152+
data,
153+
python_type=python_type,
154+
context=f"field {field!r} in trigger metadata",
155+
)
149156

150157

151158
class InConverter(_BaseConverter, binding=None):
@@ -175,8 +182,7 @@ def check_output_type_annotation(cls, pytype: type) -> bool:
175182

176183
@classmethod
177184
@abc.abstractmethod
178-
def encode(cls, obj: Any, *,
179-
expected_type: Optional[type]) -> Optional[Datum]:
185+
def encode(cls, obj: Any, *, expected_type: Optional[type]) -> Optional[Datum]:
180186
raise NotImplementedError
181187

182188

0 commit comments

Comments
 (0)