Skip to content

Commit

Permalink
Update type hints in helpers.write_timdex_records_to_json
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ghukill committed Jul 17, 2023
1 parent e9c5212 commit c7afdaa
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions transmogrifier/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import os
from datetime import datetime
from typing import Iterator, Optional
from typing import Iterator, Optional, TYPE_CHECKING

from attrs import asdict
from bs4 import BeautifulSoup, Tag
Expand All @@ -16,6 +16,10 @@
from transmogrifier.config import DATE_FORMATS
from transmogrifier.models import TimdexRecord

# import Transformer only when type checking to avoid circular dependency
if TYPE_CHECKING: # pragma: no cover
from transmogrifier.sources.transformer import Transformer

logger = logging.getLogger(__name__)


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


def write_timdex_records_to_json(
records: Iterator[TimdexRecord], output_file_path: str
transformer_instance: "Transformer", output_file_path: str
) -> int:
count = 0
try:
record = next(records)
record: TimdexRecord = next(transformer_instance)
except StopIteration:
return count
with open(output_file_path, "w") as file:
Expand All @@ -195,7 +199,7 @@ def write_timdex_records_to_json(
"Status update: %s records written to output file so far!", count
)
try:
record = next(records)
record: TimdexRecord = next(transformer_instance) # type: ignore
except StopIteration:
break
file.write(",\n")
Expand Down

0 comments on commit c7afdaa

Please sign in to comment.