Skip to content

Commit 2f7fdaa

Browse files
authored
Update type hints in helpers.write_timdex_records_to_json (#96)
* Update type hints in helpers.write_timdex_records_to_json Why these changes are being introduced: * After some refactoring in 07/2022 that added an iterator pattern to Transformer, it looks as though downstream type hints were not updated. * While this did not affect runtime, it made reading the code somewhat confusing to someone new to the codebase. How this addresses that need: * Update the `records` input argument of `helpers.write_timdex_records_to_json()` from `Iterator[TimdexRecord]` to `Transformer` * rename `records` to `transformer_instance`, matching variable names from calling `cli.py` * Add explicit `TimdexRecord` type hint for result of `next(transformer_instance)` Side effects of this change: * None Relevant ticket(s): * None Additional maintenance: * Updated syntax for isort command in Makefile * Based on new results from isort, update import order in helpers.py * update dependencies and revert click workaround * In PR #91, while updating dependencies, `click` and `mypy` had a conflict. * A temporary workaround was put into place that reordered the `click` decorators, but this deviated from other projects. * Now that `click` has updated again, that workaround is no longer needed. * Resolves #92
1 parent e9c5212 commit 2f7fdaa

File tree

4 files changed

+69
-65
lines changed

4 files changed

+69
-65
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ flake8:
3737
pipenv run flake8 .
3838

3939
isort:
40-
pipenv run isort . --diff
40+
pipenv run isort . --check --diff
4141

4242
mypy:
4343
pipenv run mypy transmogrifier

Pipfile.lock

+59-59
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transmogrifier/cli.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
logger = logging.getLogger(__name__)
2020

2121

22+
@click.command()
2223
@click.option(
2324
"-i",
2425
"--input-file",
@@ -41,7 +42,6 @@
4142
@click.option(
4243
"-v", "--verbose", is_flag=True, help="Pass to log at debug level instead of info"
4344
)
44-
@click.command()
4545
def main(source, input_file, output_file, verbose):
4646
START_TIME = perf_counter()
4747
root_logger = logging.getLogger()

transmogrifier/helpers.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
import os
44
from datetime import datetime
5-
from typing import Iterator, Optional
5+
from typing import TYPE_CHECKING, Iterator, Optional
66

77
from attrs import asdict
88
from bs4 import BeautifulSoup, Tag
@@ -16,6 +16,10 @@
1616
from transmogrifier.config import DATE_FORMATS
1717
from transmogrifier.models import TimdexRecord
1818

19+
# import Transformer only when type checking to avoid circular dependency
20+
if TYPE_CHECKING: # pragma: no cover
21+
from transmogrifier.sources.transformer import Transformer
22+
1923
logger = logging.getLogger(__name__)
2024

2125

@@ -173,11 +177,11 @@ def write_deleted_records_to_file(deleted_records: list[str], output_file_path:
173177

174178

175179
def write_timdex_records_to_json(
176-
records: Iterator[TimdexRecord], output_file_path: str
180+
transformer_instance: "Transformer", output_file_path: str
177181
) -> int:
178182
count = 0
179183
try:
180-
record = next(records)
184+
record: TimdexRecord = next(transformer_instance)
181185
except StopIteration:
182186
return count
183187
with open(output_file_path, "w") as file:
@@ -195,7 +199,7 @@ def write_timdex_records_to_json(
195199
"Status update: %s records written to output file so far!", count
196200
)
197201
try:
198-
record = next(records)
202+
record: TimdexRecord = next(transformer_instance) # type: ignore[no-redef] # noqa: E501
199203
except StopIteration:
200204
break
201205
file.write(",\n")

0 commit comments

Comments
 (0)