From 2f7fdaaf5d8cfe7602816e6c267c81e91577fc91 Mon Sep 17 00:00:00 2001 From: Graham Hukill Date: Tue, 18 Jul 2023 10:34:40 -0400 Subject: [PATCH] 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 https://github.com/MITLibraries/transmogrifier/pull/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 --- Makefile | 2 +- Pipfile.lock | 118 +++++++++++++++++++------------------- transmogrifier/cli.py | 2 +- transmogrifier/helpers.py | 12 ++-- 4 files changed, 69 insertions(+), 65 deletions(-) diff --git a/Makefile b/Makefile index 51143ce..d9995fd 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ flake8: pipenv run flake8 . isort: - pipenv run isort . --diff + pipenv run isort . --check --diff mypy: pipenv run mypy transmogrifier diff --git a/Pipfile.lock b/Pipfile.lock index 6cd6a2f..30be99e 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -34,18 +34,18 @@ }, "boto3": { "hashes": [ - "sha256:0d53fe604dc30edded21906bc56b30a7684f0715f4f6897307d53f8184997368", - "sha256:9933e40dc9ac72deac45cecce2df020e3bf8d0d537538d2b361c17d1cee807cc" + "sha256:1f4b9c23dfcad910b6f8e74aac9fe507c1e75fcdd832e25ed2ff1e6d7a99cddf", + "sha256:92c0631ab91b4c5aa0e18a90b4d12df361723c6df1ef7e346db71f2ad0803ab3" ], - "version": "==1.28.2" + "version": "==1.28.4" }, "botocore": { "hashes": [ - "sha256:67a475bec9e52d495a358b34e219ef7f62907e83b87e5bc712528f998bd46dab", - "sha256:d368ac0b58e2b9025b9c397e4a4f86d71788913ee619263506885a866a4f6811" + "sha256:1c14ac4521af707a7a407cee0e22695ce3e95c0f1a0c974e21cb25a3ce78a538", + "sha256:f9738a23b03c55c2958ebdee65273afeda80deaeefebe595887fc3251e48293a" ], "markers": "python_version >= '3.7'", - "version": "==1.31.2" + "version": "==1.31.4" }, "certifi": { "hashes": [ @@ -57,11 +57,11 @@ }, "click": { "hashes": [ - "sha256:2739815aaa5d2c986a88f1e9230c55e17f0caad3d958a5e13ad0797c166db9e3", - "sha256:b97d0c74955da062a7d4ef92fadb583806a585b2ea81958a81bd72726cbb8e37" + "sha256:4be4b1af8d665c6d942909916d31a213a106800c47d0eeba73d34da3cbc11367", + "sha256:e576aa487d679441d7d30abb87e1b43d24fc53bffb8758443b1a9e1cee504548" ], "index": "pypi", - "version": "==8.1.4" + "version": "==8.1.5" }, "jmespath": { "hashes": [ @@ -187,11 +187,11 @@ }, "sentry-sdk": { "hashes": [ - "sha256:2281ba98011cfa9bc9bb15c1074b6dd9f7fdfce94033cd25b50f18f078ffed4c", - "sha256:b8b363aaa3f3d6a3acc1aa571efa4db29fb440339fed03560bb1276b8d2c2509" + "sha256:6bdb25bd9092478d3a817cb0d01fa99e296aea34d404eac3ca0037faa5c2aa0a", + "sha256:dcd88c68aa64dae715311b5ede6502fd684f70d00a7cd4858118f0ba3153a3ae" ], "index": "pypi", - "version": "==1.28.0" + "version": "==1.28.1" }, "six": { "hashes": [ @@ -357,11 +357,11 @@ }, "click": { "hashes": [ - "sha256:2739815aaa5d2c986a88f1e9230c55e17f0caad3d958a5e13ad0797c166db9e3", - "sha256:b97d0c74955da062a7d4ef92fadb583806a585b2ea81958a81bd72726cbb8e37" + "sha256:4be4b1af8d665c6d942909916d31a213a106800c47d0eeba73d34da3cbc11367", + "sha256:e576aa487d679441d7d30abb87e1b43d24fc53bffb8758443b1a9e1cee504548" ], "index": "pypi", - "version": "==8.1.4" + "version": "==8.1.5" }, "coverage": { "hashes": [ @@ -571,11 +571,11 @@ }, "platformdirs": { "hashes": [ - "sha256:cec7b889196b9144d088e4c57d9ceef7374f6c39694ad1577a0aab50d27ea28c", - "sha256:f87ca4fcff7d2b0f81c6a748a77973d7af0f4d526f98f308477c3c436c74d528" + "sha256:1b42b450ad933e981d56e59f1b97495428c9bd60698baab9f3eb3d00d5822421", + "sha256:ad8291ae0ae5072f66c16945166cb11c63394c7a3ad1b1bc9828ca3162da8c2f" ], "markers": "python_version >= '3.7'", - "version": "==3.8.1" + "version": "==3.9.1" }, "pluggy": { "hashes": [ @@ -619,49 +619,49 @@ }, "pyyaml": { "hashes": [ - "sha256:01b45c0191e6d66c470b6cf1b9531a771a83c1c4208272ead47a3ae4f2f603bf", - "sha256:0283c35a6a9fbf047493e3a0ce8d79ef5030852c51e9d911a27badfde0605293", - "sha256:055d937d65826939cb044fc8c9b08889e8c743fdc6a32b33e2390f66013e449b", - "sha256:07751360502caac1c067a8132d150cf3d61339af5691fe9e87803040dbc5db57", - "sha256:0b4624f379dab24d3725ffde76559cff63d9ec94e1736b556dacdfebe5ab6d4b", - "sha256:0ce82d761c532fe4ec3f87fc45688bdd3a4c1dc5e0b4a19814b9009a29baefd4", - "sha256:1e4747bc279b4f613a09eb64bba2ba602d8a6664c6ce6396a4d0cd413a50ce07", - "sha256:213c60cd50106436cc818accf5baa1aba61c0189ff610f64f4a3e8c6726218ba", - "sha256:231710d57adfd809ef5d34183b8ed1eeae3f76459c18fb4a0b373ad56bedcdd9", - "sha256:277a0ef2981ca40581a47093e9e2d13b3f1fbbeffae064c1d21bfceba2030287", - "sha256:2cd5df3de48857ed0544b34e2d40e9fac445930039f3cfe4bcc592a1f836d513", - "sha256:40527857252b61eacd1d9af500c3337ba8deb8fc298940291486c465c8b46ec0", - "sha256:432557aa2c09802be39460360ddffd48156e30721f5e8d917f01d31694216782", - "sha256:473f9edb243cb1935ab5a084eb238d842fb8f404ed2193a915d1784b5a6b5fc0", - "sha256:48c346915c114f5fdb3ead70312bd042a953a8ce5c7106d5bfb1a5254e47da92", - "sha256:50602afada6d6cbfad699b0c7bb50d5ccffa7e46a3d738092afddc1f9758427f", - "sha256:68fb519c14306fec9720a2a5b45bc9f0c8d1b9c72adf45c37baedfcd949c35a2", - "sha256:77f396e6ef4c73fdc33a9157446466f1cff553d979bd00ecb64385760c6babdc", - "sha256:81957921f441d50af23654aa6c5e5eaf9b06aba7f0a19c18a538dc7ef291c5a1", - "sha256:819b3830a1543db06c4d4b865e70ded25be52a2e0631ccd2f6a47a2822f2fd7c", - "sha256:897b80890765f037df3403d22bab41627ca8811ae55e9a722fd0392850ec4d86", - "sha256:98c4d36e99714e55cfbaaee6dd5badbc9a1ec339ebfc3b1f52e293aee6bb71a4", - "sha256:9df7ed3b3d2e0ecfe09e14741b857df43adb5a3ddadc919a2d94fbdf78fea53c", - "sha256:9fa600030013c4de8165339db93d182b9431076eb98eb40ee068700c9c813e34", - "sha256:a80a78046a72361de73f8f395f1f1e49f956c6be882eed58505a15f3e430962b", - "sha256:afa17f5bc4d1b10afd4466fd3a44dc0e245382deca5b3c353d8b757f9e3ecb8d", - "sha256:b3d267842bf12586ba6c734f89d1f5b871df0273157918b0ccefa29deb05c21c", - "sha256:b5b9eccad747aabaaffbc6064800670f0c297e52c12754eb1d976c57e4f74dcb", - "sha256:bfaef573a63ba8923503d27530362590ff4f576c626d86a9fed95822a8255fd7", - "sha256:c5687b8d43cf58545ade1fe3e055f70eac7a5a1a0bf42824308d868289a95737", - "sha256:cba8c411ef271aa037d7357a2bc8f9ee8b58b9965831d9e51baf703280dc73d3", - "sha256:d15a181d1ecd0d4270dc32edb46f7cb7733c7c508857278d3d378d14d606db2d", - "sha256:d4b0ba9512519522b118090257be113b9468d804b19d63c71dbcf4a48fa32358", - "sha256:d4db7c7aef085872ef65a8fd7d6d09a14ae91f691dec3e87ee5ee0539d516f53", - "sha256:d4eccecf9adf6fbcc6861a38015c2a64f38b9d94838ac1810a9023a0609e1b78", - "sha256:d67d839ede4ed1b28a4e8909735fc992a923cdb84e618544973d7dfc71540803", - "sha256:daf496c58a8c52083df09b80c860005194014c3698698d1a57cbcfa182142a3a", - "sha256:dbad0e9d368bb989f4515da330b88a057617d16b6a8245084f1b05400f24609f", - "sha256:e61ceaab6f49fb8bdfaa0f92c4b57bcfbea54c09277b1b4f7ac376bfb7a7c174", - "sha256:f84fbc98b019fef2ee9a1cb3ce93e3187a6df0b2538a651bfb890254ba9f90b5" + "sha256:062582fca9fabdd2c8b54a3ef1c978d786e0f6b3a1510e0ac93ef59e0ddae2bc", + "sha256:1635fd110e8d85d55237ab316b5b011de701ea0f29d07611174a1b42f1444741", + "sha256:184c5108a2aca3c5b3d3bf9395d50893a7ab82a38004c8f61c258d4428e80206", + "sha256:18aeb1bf9a78867dc38b259769503436b7c72f7a1f1f4c93ff9a17de54319b27", + "sha256:1d4c7e777c441b20e32f52bd377e0c409713e8bb1386e1099c2415f26e479595", + "sha256:1e2722cc9fbb45d9b87631ac70924c11d3a401b2d7f410cc0e3bbf249f2dca62", + "sha256:1fe35611261b29bd1de0070f0b2f47cb6ff71fa6595c077e42bd0c419fa27b98", + "sha256:28c119d996beec18c05208a8bd78cbe4007878c6dd15091efb73a30e90539696", + "sha256:42f8152b8dbc4fe7d96729ec2b99c7097d656dc1213a3229ca5383f973a5ed6d", + "sha256:4fb147e7a67ef577a588a0e2c17b6db51dda102c71de36f8549b6816a96e1867", + "sha256:50550eb667afee136e9a77d6dc71ae76a44df8b3e51e41b77f6de2932bfe0f47", + "sha256:510c9deebc5c0225e8c96813043e62b680ba2f9c50a08d3724c7f28a747d1486", + "sha256:5773183b6446b2c99bb77e77595dd486303b4faab2b086e7b17bc6bef28865f6", + "sha256:596106435fa6ad000c2991a98fa58eeb8656ef2325d7e158344fb33864ed87e3", + "sha256:6965a7bc3cf88e5a1c3bd2e0b5c22f8d677dc88a455344035f03399034eb3007", + "sha256:69b023b2b4daa7548bcfbd4aa3da05b3a74b772db9e23b982788168117739938", + "sha256:704219a11b772aea0d8ecd7058d0082713c3562b4e271b849ad7dc4a5c90c13c", + "sha256:7e07cbde391ba96ab58e532ff4803f79c4129397514e1413a7dc761ccd755735", + "sha256:81e0b275a9ecc9c0c0c07b4b90ba548307583c125f54d5b6946cfee6360c733d", + "sha256:9046c58c4395dff28dd494285c82ba00b546adfc7ef001486fbf0324bc174fba", + "sha256:9eb6caa9a297fc2c2fb8862bc5370d0303ddba53ba97e71f08023b6cd73d16a8", + "sha256:a0cd17c15d3bb3fa06978b4e8958dcdc6e0174ccea823003a106c7d4d7899ac5", + "sha256:afd7e57eddb1a54f0f1a974bc4391af8bcce0b444685d936840f125cf046d5bd", + "sha256:b1275ad35a5d18c62a7220633c913e1b42d44b46ee12554e5fd39c70a243d6a3", + "sha256:b786eecbdf8499b9ca1d697215862083bd6d2a99965554781d0d8d1ad31e13a0", + "sha256:ba336e390cd8e4d1739f42dfe9bb83a3cc2e80f567d8805e11b46f4a943f5515", + "sha256:baa90d3f661d43131ca170712d903e6295d1f7a0f595074f151c0aed377c9b9c", + "sha256:bc1bf2925a1ecd43da378f4db9e4f799775d6367bdb94671027b73b393a7c42c", + "sha256:bd4af7373a854424dabd882decdc5579653d7868b8fb26dc7d0e99f823aa5924", + "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34", + "sha256:bfdf460b1736c775f2ba9f6a92bca30bc2095067b8a9d77876d1fad6cc3b4a43", + "sha256:c8098ddcc2a85b61647b2590f825f3db38891662cfc2fc776415143f599bb859", + "sha256:d2b04aac4d386b172d5b9692e2d2da8de7bfb6c387fa4f801fbf6fb2e6ba4673", + "sha256:d858aa552c999bc8a8d57426ed01e40bef403cd8ccdd0fc5f6f04a00414cac2a", + "sha256:f003ed9ad21d6a4713f0a9b5a7a0a79e08dd0f221aff4525a2be4c346ee60aab", + "sha256:f22ac1c3cac4dbc50079e965eba2c1058622631e526bd9afd45fedd49ba781fa", + "sha256:faca3bdcf85b2fc05d06ff3fbc1f83e1391b3e724afa3feba7d13eeab355484c", + "sha256:fca0e3a251908a499833aa292323f32437106001d436eca0e6e7833256674585", + "sha256:fd1592b3fdf65fff2ad0004b5e363300ef59ced41c2e6b3a99d4089fa8c5435d", + "sha256:fd66fc5d0da6d9815ba2cebeb4205f95818ff4b79c3ebe268e75d961704af52f" ], "markers": "python_version >= '3.6'", - "version": "==6.0" + "version": "==6.0.1" }, "requests": { "hashes": [ diff --git a/transmogrifier/cli.py b/transmogrifier/cli.py index 5597e88..f9ea804 100644 --- a/transmogrifier/cli.py +++ b/transmogrifier/cli.py @@ -19,6 +19,7 @@ logger = logging.getLogger(__name__) +@click.command() @click.option( "-i", "--input-file", @@ -41,7 +42,6 @@ @click.option( "-v", "--verbose", is_flag=True, help="Pass to log at debug level instead of info" ) -@click.command() def main(source, input_file, output_file, verbose): START_TIME = perf_counter() root_logger = logging.getLogger() diff --git a/transmogrifier/helpers.py b/transmogrifier/helpers.py index 3deb0aa..7a4fdc3 100644 --- a/transmogrifier/helpers.py +++ b/transmogrifier/helpers.py @@ -2,7 +2,7 @@ import logging import os from datetime import datetime -from typing import Iterator, Optional +from typing import TYPE_CHECKING, Iterator, Optional from attrs import asdict from bs4 import BeautifulSoup, Tag @@ -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__) @@ -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: @@ -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[no-redef] # noqa: E501 except StopIteration: break file.write(",\n")