Skip to content

Commit a104212

Browse files
authored
Merge pull request #51 from invertase/types-readability
refactor: improve types readability + CI job for formatting
2 parents eb1bb15 + 98d4bfd commit a104212

File tree

15 files changed

+104
-80
lines changed

15 files changed

+104
-80
lines changed

.github/workflows/ci.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,23 @@ jobs:
8484
with:
8585
name: reference-docs
8686
path: ./docs/build/
87+
88+
format:
89+
runs-on: ubuntu-latest
90+
steps:
91+
- uses: actions/checkout@v3
92+
- name: Set up Python
93+
uses: actions/setup-python@v4
94+
with:
95+
python-version: "3.10"
96+
- name: Install dependencies
97+
run: |
98+
python3.10 -m venv venv
99+
source venv/bin/activate
100+
pip3 install --upgrade pip
101+
python3.10 -m pip install -r requirements.txt
102+
python3.10 setup.py install
103+
- name: Check Formatting
104+
run: |
105+
source venv/bin/activate
106+
yapf -d -r -p .

docs/theme/devsite_translator/html.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515

1616
from sphinx.writers import html
1717

18-
19-
_DESCTYPE_NAMES = {'class': 'Classes',
20-
'data': 'Constants',
21-
'function': 'Functions',
22-
'method': 'Methods',
23-
'attribute': 'Attributes',
24-
'exception': 'Exceptions'}
18+
_DESCTYPE_NAMES = {
19+
'class': 'Classes',
20+
'data': 'Constants',
21+
'function': 'Functions',
22+
'method': 'Methods',
23+
'attribute': 'Attributes',
24+
'exception': 'Exceptions'
25+
}
2526

2627
# Use the default translator for these node types
2728
_RENDER_WITH_DEFAULT = ['method', 'staticmethod', 'attribute']
@@ -48,12 +49,14 @@ def visit_desc(self, node):
4849
if node.parent.tagname == 'section':
4950
self.insert_header = True
5051
if node['desctype'] != self.current_section:
51-
self.body.append(f"<h2>{_DESCTYPE_NAMES[node['desctype']]}</h2>")
52+
self.body.append(
53+
f"<h2>{_DESCTYPE_NAMES[node['desctype']]}</h2>")
5254
self.current_section = node['desctype']
5355
if node['desctype'] in _RENDER_WITH_DEFAULT:
5456
html.HTMLTranslator.visit_desc(self, node)
5557
else:
56-
self.body.append(self.starttag(node, 'table', CLASS=node['objtype']))
58+
self.body.append(self.starttag(node, 'table',
59+
CLASS=node['objtype']))
5760

5861
def depart_desc(self, node):
5962
if node['desctype'] in _RENDER_WITH_DEFAULT:
@@ -68,7 +71,8 @@ def visit_desc_signature(self, node):
6871
self.body.append('<tr>')
6972
self.body.append(self.starttag(node, 'th'))
7073
if self.insert_header:
71-
self.body.append(f"<h3 class=\"sphinx-hidden\">{node['fullname']}</h3>")
74+
self.body.append(
75+
f"<h3 class=\"sphinx-hidden\">{node['fullname']}</h3>")
7276
self.insert_header = False
7377

7478
def depart_desc_signature(self, node):

pyproject.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ pythonpath = [
1212

1313
[tool.coverage.report]
1414
skip_empty = true
15+
[tool.yapf]
16+
based_on_style = "google"
17+
indent_width = 4
18+
[tool.yapfignore]
19+
ignore_patterns = [
20+
"venv",
21+
"build",
22+
"dist",
23+
]

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ pylint>=2.16.1
66
pytest-cov>=3.0.0
77
mypy>=1.0.0
88
sphinx>=6.1.3
9-
sphinxcontrib-napoleon>=0.7
9+
sphinxcontrib-napoleon>=0.7
10+
yapf>=0.32.0
11+
toml>=0.10.2

setup.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
15-
1614
"""
1715
Setup for Firebase Functions Python.
1816
"""

src/firebase_functions/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
15-
1614
"""
1715
Firebase Functions for Python.
1816
"""

src/firebase_functions/core.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
15-
1614
"""
1715
Public code that is shared across modules.
1816
"""
@@ -64,4 +62,3 @@ class CloudEvent(_typing.Generic[T]):
6462
"""
6563
The resource, provided by source, that this event relates to
6664
"""
67-

src/firebase_functions/https_fn.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class HttpsError(Exception):
233233
determines the HTTP status code of the response.
234234
"""
235235

236-
details: _typing.Optional[_typing.Any] = None
236+
details: _typing.Any | None = None
237237
"""
238238
Extra data to be converted to JSON and included in the error response.
239239
"""
@@ -247,7 +247,7 @@ def __init__(
247247
self,
248248
code: FunctionsErrorCode,
249249
message: str,
250-
details: _typing.Optional[_typing.Any] = None,
250+
details: _typing.Any | None = None,
251251
):
252252
self.code = code
253253
self.message = message
@@ -326,17 +326,17 @@ class CallableRequest(_typing.Generic[_core.T]):
326326
The raw request handled by the callable.
327327
"""
328328

329-
app: _typing.Optional[AppCheckData] = None
329+
app: AppCheckData | None = None
330330
"""
331331
The result of decoding and verifying a Firebase AppCheck token.
332332
"""
333333

334-
auth: _typing.Optional[AuthData] = None
334+
auth: AuthData | None = None
335335
""""
336336
The result of decoding and verifying a Firebase Auth ID token.
337337
"""
338338

339-
instance_id_token: _typing.Optional[str] = None
339+
instance_id_token: str | None = None
340340
"""
341341
An unverified token for a Firebase Instance ID.
342342
"""

src/firebase_functions/options.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ class PubSubOptions(RuntimeOptions):
325325
Internal use only.
326326
"""
327327

328-
retry: _typing.Optional[bool] = None
328+
retry: bool | None = None
329329
"""
330330
Whether failed executions should be delivered again.
331331
"""
@@ -364,7 +364,7 @@ class StorageOptions(RuntimeOptions):
364364
Internal use only.
365365
"""
366366

367-
bucket: _typing.Optional[str] = None
367+
bucket: str | None = None
368368
"""
369369
The name of the bucket to watch for Storage events.
370370
"""
@@ -416,7 +416,7 @@ class DatabaseOptions(RuntimeOptions):
416416
Examples: '/foo/bar', '/foo/{bar}'
417417
"""
418418

419-
instance: _typing.Optional[str] = None
419+
instance: str | None = None
420420
"""
421421
Specify the handler to trigger on a database instance(s).
422422
If present, this value can either be a single instance or a pattern.
@@ -471,7 +471,7 @@ class HttpsOptions(RuntimeOptions):
471471
Invoker to set access control on https functions.
472472
"""
473473

474-
cors: _typing.Optional[CorsOptions] = None
474+
cors: CorsOptions | None = None
475475
"""
476476
Optionally set CORS options for Https functions.
477477
"""

src/firebase_functions/params.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class SelectOptions(_typing.Generic[_T]):
104104
value: _T
105105
"""The value of the option."""
106106

107-
label: _typing.Optional[str] = None
107+
label: str | None = None
108108
"""The displayed label for the option."""
109109

110110

@@ -127,18 +127,18 @@ class TextInput:
127127
validation_regex, if present, will be retried.
128128
"""
129129

130-
example: _typing.Optional[str] = None
130+
example: str | None = None
131131
"""
132132
An example of the input required that will be displayed alongside the input prompt.
133133
"""
134134

135-
validation_regex: _typing.Optional[str] = None
135+
validation_regex: str | None = None
136136
"""
137137
Validation regex for the input.
138138
Input that does not match this regex, if present, will be retried.
139139
"""
140140

141-
validation_error_message: _typing.Optional[str] = None
141+
validation_error_message: str | None = None
142142
"""
143143
An error message that is displayed to the user if validation_regex fails.
144144
"""
@@ -169,29 +169,28 @@ class Param(Expression[_T]):
169169
The environment variable of this parameter. Must be upper case.
170170
"""
171171

172-
default: _typing.Optional[_T] = None
172+
default: _T | None = None
173173
"""
174174
The default value to assign to this param if none provided.
175175
"""
176176

177-
label: _typing.Optional[str] = None
177+
label: str | None = None
178178
"""
179179
A label that is displayed to the user for this param.
180180
"""
181181

182-
description: _typing.Optional[str] = None
182+
description: str | None = None
183183
"""
184184
Description of this param that is displayed to the user.
185185
"""
186186

187-
immutable: _typing.Optional[bool] = None
187+
immutable: bool | None = None
188188
"""
189189
Whether the value of this parameter can change between function
190190
deployments.
191191
"""
192192

193-
input: _typing.Union[TextInput, ResourceInput,
194-
SelectInput[_T]] = TextInput()
193+
input: TextInput | ResourceInput | SelectInput[_T] = TextInput()
195194
"""
196195
The type of input that is required for this param, e.g. TextInput.
197196
"""
@@ -233,17 +232,17 @@ class SecretParam(Expression[str]):
233232
The environment variable of this parameter. Must be upper case.
234233
"""
235234

236-
label: _typing.Optional[str] = None
235+
label: str | None = None
237236
"""
238237
A label that is displayed to the user for this param.
239238
"""
240239

241-
description: _typing.Optional[str] = None
240+
description: str | None = None
242241
"""
243242
Description of this param that is displayed to the user.
244243
"""
245244

246-
immutable: _typing.Optional[bool] = None
245+
immutable: bool | None = None
247246
"""
248247
Whether the value of this parameter can change between function
249248
deployments.

src/firebase_functions/private/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
15-
1614
"""
1715
Firebase Functions for Python - Private/Internals
1816
"""

src/firebase_functions/private/manifest.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,29 +94,28 @@ class VpcSettings(_typing.TypedDict):
9494
class ManifestEndpoint:
9595
"""A definition of a function as appears in the Manifest."""
9696

97-
entryPoint: _typing.Optional[str] = None
98-
region: _typing.Optional[list[str]] = _dataclasses.field(
99-
default_factory=list[str])
100-
platform: _typing.Optional[str] = "gcfv2"
97+
entryPoint: str | None = None
98+
region: list[str] | None = _dataclasses.field(default_factory=list[str])
99+
platform: str | None = "gcfv2"
101100
availableMemoryMb: int | _params.Expression[
102101
int] | _util.Sentinel | None = None
103102
maxInstances: int | _params.Expression[int] | _util.Sentinel | None = None
104103
minInstances: int | _params.Expression[int] | _util.Sentinel | None = None
105104
concurrency: int | _params.Expression[int] | _util.Sentinel | None = None
106-
serviceAccountEmail: _typing.Optional[str | _util.Sentinel] = None
105+
serviceAccountEmail: str | _util.Sentinel | None = None
107106
timeoutSeconds: int | _params.Expression[int] | _util.Sentinel | None = None
108107
cpu: int | str | _util.Sentinel | None = None
109-
vpc: _typing.Optional[VpcSettings] = None
110-
labels: _typing.Optional[dict[str, str]] = None
111-
ingressSettings: _typing.Optional[str] | _util.Sentinel = None
112-
secretEnvironmentVariables: _typing.Optional[
113-
list[SecretEnvironmentVariable] | _util.Sentinel] = _dataclasses.field(
108+
vpc: VpcSettings | None = None
109+
labels: dict[str, str] | None = None
110+
ingressSettings: str | None | _util.Sentinel = None
111+
secretEnvironmentVariables: list[
112+
SecretEnvironmentVariable] | _util.Sentinel | None = _dataclasses.field(
114113
default_factory=list[SecretEnvironmentVariable])
115-
httpsTrigger: _typing.Optional[HttpsTrigger] = None
116-
callableTrigger: _typing.Optional[CallableTrigger] = None
117-
eventTrigger: _typing.Optional[EventTrigger] = None
118-
scheduleTrigger: _typing.Optional[ScheduleTrigger] = None
119-
blockingTrigger: _typing.Optional[BlockingTrigger] = None
114+
httpsTrigger: HttpsTrigger | None = None
115+
callableTrigger: CallableTrigger | None = None
116+
eventTrigger: EventTrigger | None = None
117+
scheduleTrigger: ScheduleTrigger | None = None
118+
blockingTrigger: BlockingTrigger | None = None
120119

121120

122121
class ManifestRequiredApi(_typing.TypedDict):

src/firebase_functions/private/util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def _on_call_valid_method(request: _Request) -> bool:
108108

109109
def _on_call_valid_content_type(request: _Request) -> bool:
110110
"""Validate content"""
111-
content_type: _typing.Optional[str] = request.headers.get("Content-Type")
111+
content_type: str | None = request.headers.get("Content-Type")
112112

113113
if content_type is None:
114114
_logging.warning("Request is missing Content-Type.", content_type)
@@ -159,9 +159,9 @@ class _OnCallTokenVerification:
159159
"""
160160

161161
app: OnCallTokenState = OnCallTokenState.INVALID
162-
app_token: _typing.Optional[dict[str, _typing.Any]] = None
162+
app_token: dict[str, _typing.Any] | None = None
163163
auth: OnCallTokenState = OnCallTokenState.INVALID
164-
auth_token: _typing.Optional[dict] = None
164+
auth_token: dict | None = None
165165

166166
def as_dict(self) -> dict:
167167
"""Set dictionary"""
@@ -256,7 +256,7 @@ class FirebaseConfig():
256256
initialize a firebase App.
257257
"""
258258

259-
storage_bucket: _typing.Optional[str]
259+
storage_bucket: str | None
260260
"""
261261
The name of the Google Cloud Storage bucket used for storing application data.
262262
This is the bucket name without any prefixes or additions (without "gs://").

src/firebase_functions/pubsub_fn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Message(_typing.Generic[T]):
6161
"""
6262

6363
@property
64-
def json(self) -> _typing.Optional[T]:
64+
def json(self) -> T | None:
6565
try:
6666
if self.data is not None:
6767
return _json.loads(_base64.b64decode(self.data).decode("utf-8"))

0 commit comments

Comments
 (0)