From 852f54fcfef0757b44b65f7df9efb7af9a9f9735 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 7 Feb 2024 17:56:26 -0600 Subject: [PATCH 1/7] ruff . --select COM --fix --- src/unihan_etl/_internal/app_dirs.py | 4 +-- src/unihan_etl/core.py | 18 ++++++------- src/unihan_etl/expansion.py | 28 ++++++++++---------- src/unihan_etl/options.py | 2 +- src/unihan_etl/pytest_plugin.py | 10 ++++---- src/unihan_etl/types.py | 2 +- src/unihan_etl/util.py | 4 +-- tests/test_expansion.py | 38 ++++++++++++++-------------- tests/test_pytest_plugin.py | 16 ++++++------ tests/test_unihan.py | 12 ++++----- 10 files changed, 67 insertions(+), 67 deletions(-) diff --git a/src/unihan_etl/_internal/app_dirs.py b/src/unihan_etl/_internal/app_dirs.py index c6e8c667..c0a2830b 100644 --- a/src/unihan_etl/_internal/app_dirs.py +++ b/src/unihan_etl/_internal/app_dirs.py @@ -88,7 +88,7 @@ def __post_init__(self, _app_dirs: "BaseAppDirs") -> None: pathlib.Path( os.path.expanduser( # noqa: PTH111 os.path.expandvars(str(val)), - ).format(**dir_mapping) + ).format(**dir_mapping), ), ) @@ -108,6 +108,6 @@ def __post_init__(self, _app_dirs: "BaseAppDirs") -> None: pathlib.Path( os.path.expanduser( # noqa: PTH111 os.path.expandvars(str(val)), - ).format(**dir_mapping) + ).format(**dir_mapping), ), ) diff --git a/src/unihan_etl/core.py b/src/unihan_etl/core.py index 285678fb..cc397f94 100755 --- a/src/unihan_etl/core.py +++ b/src/unihan_etl/core.py @@ -146,7 +146,7 @@ def get_parser() -> argparse.ArgumentParser: help=f"Output of .csv. Default: {DESTINATION_DIR}/unihan.{{json,csv,yaml}}", ) parser.add_argument( - "-w", "--work-dir", dest="work_dir", help=f"Default: {WORK_DIR}" + "-w", "--work-dir", dest="work_dir", help=f"Default: {WORK_DIR}", ) parser.add_argument( "-F", @@ -315,7 +315,7 @@ def load_data( """ log.info(f"Loading data: {', '.join([str(s) for s in files])}") raw_data = fileinput.FileInput( - files=files, openhook=fileinput.hook_encoded("utf-8") + files=files, openhook=fileinput.hook_encoded("utf-8"), ) log.info("Done loading data.") @@ -412,7 +412,7 @@ def expand_delimiters(normalized_data: "UntypedNormalizedData") -> "ExpandedExpo def listify( - data: "UntypedNormalizedData", fields: t.Sequence[str] + data: "UntypedNormalizedData", fields: t.Sequence[str], ) -> "ListifiedExport": """Convert tabularized data to a CSV-friendly list. @@ -467,7 +467,7 @@ def validate_options( ) -> "TypeGuard[Options]": """Validate unihan-etl options.""" if not is_default_option("input_files", options.input_files) and is_default_option( - "fields", options.fields + "fields", options.fields, ): # Filter fields when only files specified. try: @@ -475,12 +475,12 @@ def validate_options( except (KeyError, FieldNotFound) as e: raise FileNotSupported(str(e)) from e elif not is_default_option("fields", options.fields) and is_default_option( - "input_files", options.input_files + "input_files", options.input_files, ): # Filter files when only field specified. options.input_files = get_files(options.fields) elif not is_default_option("fields", options.fields) and not is_default_option( - "input_files", options.input_files + "input_files", options.input_files, ): # Filter fields when only files specified. fields_in_files = get_fields(filter_manifest(options.input_files)) @@ -520,7 +520,7 @@ def __init__( setup_logger(logger=None, level=options.log_level or DEFAULT_OPTIONS.log_level) merged_options = dataclasses.replace( - DEFAULT_OPTIONS, **dataclasses.asdict(options) + DEFAULT_OPTIONS, **dataclasses.asdict(options), ) self.options = merged_options @@ -561,7 +561,7 @@ def export(self) -> t.Union[None, "UntypedNormalizedData"]: # Replace {ext} with extension to use. self.options.destination = pathlib.Path( - str(self.options.destination).format(ext=self.options.format) + str(self.options.destination).format(ext=self.options.format), ) if not self.options.destination.parent.exists(): @@ -613,7 +613,7 @@ def from_cli(cls, argv: t.Sequence[str]) -> "Packager": try: return cls( - Options(**{k: v for k, v in vars(args).items() if v is not None}) + Options(**{k: v for k, v in vars(args).items() if v is not None}), ) except Exception as e: sys.exit(str(e)) diff --git a/src/unihan_etl/expansion.py b/src/unihan_etl/expansion.py index 7f4a8925..e897c282 100644 --- a/src/unihan_etl/expansion.py +++ b/src/unihan_etl/expansion.py @@ -134,7 +134,7 @@ def expand_kAlternateTotalStrokes( kAlternateTotalStrokesDict( strokes=strokes, sources=sources, - ) + ), ) return expanded @@ -293,13 +293,13 @@ def expand_kTGHZ2013( page=int(g["page"]), position=int(g["position"]), entry_type=int(g["entry_type"]), - ) + ), ) expanded.append( kTGHZ2013Dict( reading=reading, locations=exploded_locations, - ) + ), ) return expanded @@ -353,7 +353,7 @@ def expand_kSMSZD2003Index( kSMSZD2003IndexDict( page=int(g["page"]), position=int(g["position"]), - ) + ), ) return expanded @@ -395,7 +395,7 @@ def expand_kSMSZD2003Readings( kSMSZD2003ReadingsDict( mandarin=mandarin.split(","), cantonese=cantonese.split(","), - ) + ), ) return expanded @@ -448,7 +448,7 @@ def expand_kHanyuPinyin( virtual=int(g["virtual"]), ) expanded[i] = kHanyuPinyinDict( - locations=expanded[i]["locations"], readings=expanded[i]["readings"] + locations=expanded[i]["locations"], readings=expanded[i]["readings"], ) return expanded @@ -511,7 +511,7 @@ def expand_kXHC1983( substituted=g["substituted"] == "*", ) expanded[i] = kXHC1983Dict( - locations=expanded[i]["locations"], reading=expanded[i]["reading"] + locations=expanded[i]["locations"], reading=expanded[i]["reading"], ) return expanded @@ -592,7 +592,7 @@ def expand_kRSAdobe_Japan1_6(value: t.List[str]) -> t.List[kRSAdobe_Japan1_6Dict "radical": int(g["radical"]), "strokes": int(g["strokes"]), "strokes-residue": int(g["strokes_residue"]), - } + }, ) return expanded @@ -630,7 +630,7 @@ def expand_kCihaiT(value: t.List[str]) -> t.List[kCihaiTDict]: "page": int(m["page"]), "row": int(m["row"]), "character": int(m["character"]), - } + }, ) return expanded @@ -731,7 +731,7 @@ def expand_kFenn(value: t.List[str]) -> t.List[kFennDict]: assert g is not None expanded[i] = kFennDict( - {"phonetic": g["phonetic"], "frequency": g["frequency"]} + {"phonetic": g["phonetic"], "frequency": g["frequency"]}, ) return expanded @@ -762,7 +762,7 @@ def expand_kHanyuPinlu(value: t.List[str]) -> t.List[kHanyuPinluDict]: assert g is not None expanded[i] = kHanyuPinluDict( - {"phonetic": g["phonetic"], "frequency": int(g["frequency"])} + {"phonetic": g["phonetic"], "frequency": int(g["frequency"])}, ) return expanded @@ -954,7 +954,7 @@ def expand_kGSR(value: t.List[str]) -> t.List[kGSRDict]: "set": int(g["set"]), "letter": g["letter"], "apostrophe": g["apostrophe"] == "'", - } + }, ) return expanded @@ -1071,7 +1071,7 @@ def expand_kStrange( kStrangeDict( property_type=property_type, characters=characters, - ) + ), ) return expanded @@ -1134,7 +1134,7 @@ def expand_kMojiJoho( serial_number=serial_number, variation_sequence=variation_sequence, standard=serial_number == default_serial, - ) + ), ) return kMojiJohoDict( serial_number=default_serial, diff --git a/src/unihan_etl/options.py b/src/unihan_etl/options.py index 067538be..576bf08d 100644 --- a/src/unihan_etl/options.py +++ b/src/unihan_etl/options.py @@ -26,7 +26,7 @@ class Options: zip_path: pathlib.Path = UNIHAN_ZIP_PATH work_dir: pathlib.Path = WORK_DIR fields: t.Sequence[str] = dataclasses.field( - default_factory=lambda: INDEX_FIELDS + UNIHAN_FIELDS + default_factory=lambda: INDEX_FIELDS + UNIHAN_FIELDS, ) format: t.Literal["json", "csv", "yaml", "python"] = "csv" input_files: t.List[str] = dataclasses.field(default_factory=lambda: UNIHAN_FILES) diff --git a/src/unihan_etl/pytest_plugin.py b/src/unihan_etl/pytest_plugin.py index 2bd558ed..80e9bee8 100644 --- a/src/unihan_etl/pytest_plugin.py +++ b/src/unihan_etl/pytest_plugin.py @@ -71,7 +71,7 @@ def unihan_full_options(unihan_full_path: pathlib.Path) -> UnihanOptions: @pytest.fixture(scope="session") def unihan_full_packager( - unihan_full_path: pathlib.Path, unihan_full_options: "UnihanOptions" + unihan_full_path: pathlib.Path, unihan_full_options: "UnihanOptions", ) -> "Packager": """Return Packager for "full" portion of UNIHAN, return a UnihanOptions.""" return Packager(unihan_full_options) @@ -224,7 +224,7 @@ def unihan_quick_options( @pytest.fixture(scope="session") def unihan_quick_packager( - unihan_quick_path: pathlib.Path, unihan_quick_options: "UnihanOptions" + unihan_quick_path: pathlib.Path, unihan_quick_options: "UnihanOptions", ) -> "Packager": """Bootstrap a small, but effective portion of UNIHAN, return a UnihanOptions.""" return Packager(unihan_quick_options) @@ -358,7 +358,7 @@ def unihan_home_user_name() -> str: @pytest.fixture(scope="session") def unihan_user_path( - unihan_home_path: pathlib.Path, unihan_home_user_name: str + unihan_home_path: pathlib.Path, unihan_home_user_name: str, ) -> pathlib.Path: """Return temporary user directory. @@ -427,7 +427,7 @@ def unihan_mock_test_dir(tmp_path_factory: pytest.TempPathFactory) -> pathlib.Pa @pytest.fixture(scope="session") def unihan_mock_zip_path( - unihan_mock_test_dir: pathlib.Path, unihan_mock_zip_pathname: str + unihan_mock_test_dir: pathlib.Path, unihan_mock_zip_pathname: str, ) -> pathlib.Path: """Return path to Unihan zipfile.""" return unihan_mock_test_dir / unihan_mock_zip_pathname @@ -435,7 +435,7 @@ def unihan_mock_zip_path( @pytest.fixture(scope="session") def unihan_mock_zip( - unihan_mock_zip_path: pathlib.Path, unihan_quick_data: str + unihan_mock_zip_path: pathlib.Path, unihan_quick_data: str, ) -> zipfile.ZipFile: """Return Unihan zipfile.""" zf = zipfile.ZipFile(str(unihan_mock_zip_path), "a") diff --git a/src/unihan_etl/types.py b/src/unihan_etl/types.py index 232f0033..5b43b731 100644 --- a/src/unihan_etl/types.py +++ b/src/unihan_etl/types.py @@ -63,7 +63,7 @@ class ReportHookFn(t.Protocol): """Progress bar callback for download().""" def __call__( - self, count: int, block_size: int, total_size: int, out: t.IO[str] = sys.stdout + self, count: int, block_size: int, total_size: int, out: t.IO[str] = sys.stdout, ) -> object: """Print progress bar during download.""" ... diff --git a/src/unihan_etl/util.py b/src/unihan_etl/util.py index 14bad4a5..63e6d16a 100644 --- a/src/unihan_etl/util.py +++ b/src/unihan_etl/util.py @@ -73,7 +73,7 @@ def ucnstring_to_unicode(ucn_string: str) -> str: def _dl_progress( - count: int, block_size: int, total_size: int, out: t.IO[str] = sys.stdout + count: int, block_size: int, total_size: int, out: t.IO[str] = sys.stdout, ) -> None: """ MIT License: https://github.com/okfn/dpm-old/blob/master/dpm/util.py. @@ -112,7 +112,7 @@ def format_size(_bytes: int) -> str: percent, int(round(percent / 2)) * "=", int(round(50 - percent / 2)) * " ", - ) + ), ) out.flush() if maxdownloaded >= total_size: diff --git a/tests/test_expansion.py b/tests/test_expansion.py index cee82d59..785b982a 100644 --- a/tests/test_expansion.py +++ b/tests/test_expansion.py @@ -62,7 +62,7 @@ def test_expand( ], ) def test_expand_kMandarin( - unihan_quick_expanded_data: ExpandedData, ucn: str, expected: t.Dict[str, str] + unihan_quick_expanded_data: ExpandedData, ucn: str, expected: t.Dict[str, str], ) -> None: """Test expansion of KMandarin. @@ -85,7 +85,7 @@ def test_expand_kMandarin( ], ) def test_expand_kTotalStrokes( - unihan_quick_expanded_data: ExpandedData, ucn: str, expected: t.Dict[str, int] + unihan_quick_expanded_data: ExpandedData, ucn: str, expected: t.Dict[str, int], ) -> None: """Test expansion of kTotalStrokes. @@ -110,7 +110,7 @@ def test_expand_kTotalStrokes( ], ) def test_expand_kIRGHanyuDaZidian( - ucn: str, expected: ExpandedData, unihan_quick_expanded_data: ExpandedData + ucn: str, expected: ExpandedData, unihan_quick_expanded_data: ExpandedData, ) -> None: """Test expansion of kIRGHanyuDaZidian.""" item = next(i for i in unihan_quick_expanded_data if i["ucn"] == ucn) @@ -130,7 +130,7 @@ def test_expand_kIRGHanyuDaZidian( {"volume": 1, "page": 514, "character": 2, "virtual": 0}, ], "readings": ["gǒng"], - } + }, ], ), ( @@ -138,13 +138,13 @@ def test_expand_kIRGHanyuDaZidian( [ { # U+5364 kHanyuPinyin 10093.130:xī,lǔ 74609.020:lǔ,xī "locations": [ - {"volume": 1, "page": 93, "character": 13, "virtual": 0} + {"volume": 1, "page": 93, "character": 13, "virtual": 0}, ], "readings": ["xī", "lǔ"], }, { "locations": [ - {"volume": 7, "page": 4609, "character": 2, "virtual": 0} + {"volume": 7, "page": 4609, "character": 2, "virtual": 0}, ], "readings": ["lǔ", "xī"], }, @@ -159,7 +159,7 @@ def test_expand_kIRGHanyuDaZidian( {"volume": 1, "page": 278, "character": 9, "virtual": 0}, ], "readings": ["sù"], - } + }, ], ), ( @@ -167,10 +167,10 @@ def test_expand_kIRGHanyuDaZidian( [ { # U+34CE kHanyuPinyin 10297.260:qīn,qìn,qǐn "locations": [ - {"volume": 1, "page": 297, "character": 26, "virtual": 0} + {"volume": 1, "page": 297, "character": 26, "virtual": 0}, ], "readings": ["qīn", "qìn", "qǐn"], - } + }, ], ), ], @@ -218,7 +218,7 @@ def test_expand_kHanyuPinyin( "page": 4699, "character": 12, "virtual": 2, - } + }, ], ), ( @@ -241,13 +241,13 @@ def test_expand_kHanyuPinyin( "page": 273, "character": 12, "virtual": 0, - } + }, ], ), ], ) def test_expand_HanYu( - unihan_quick_expanded_data: ExpandedData, ucn: str, expected: ExpandedData + unihan_quick_expanded_data: ExpandedData, ucn: str, expected: ExpandedData, ) -> None: """Test expansion of HanYu. @@ -305,7 +305,7 @@ def test_expand_HanYu( "radical": 7, "strokes": 2, "strokes-residue": 6, - } + }, ], ), # U+4E30 kRSAdobe_Japan1_6 C+14301+2.1.3 V+15386+2.1.3 @@ -411,7 +411,7 @@ def test_expand_kRSUnihan( "strokes": 7, "cangjie": "RMMV", "readings": ["san2", "seon2"], - } + }, ], ), ], @@ -441,7 +441,7 @@ def test_expand_kCheungBauer( "ucn,expected", [ # U+34D6 kCihaiT 170.105 - ("U+34D6", [{"page": 170, "row": 1, "character": 5}]) + ("U+34D6", [{"page": 170, "row": 1, "character": 5}]), ], ) def test_expand_kCihaiT( @@ -683,13 +683,13 @@ def test_expand_kSBGY( [ { "locations": [ - {"page": 295, "character": 1, "entry": 1, "substituted": False} + {"page": 295, "character": 1, "entry": 1, "substituted": False}, ], "reading": "fā", }, { "locations": [ - {"page": 884, "character": 8, "entry": 1, "substituted": False} + {"page": 884, "character": 8, "entry": 1, "substituted": False}, ], "reading": "pō", }, @@ -711,7 +711,7 @@ def test_expand_kSBGY( }, ], "reading": "sóng", - } + }, ], ), # U+5750 kXHC1983 1551.040,1552.011:zuò @@ -735,7 +735,7 @@ def test_expand_kSBGY( }, ], "reading": "zuò", - } + }, ], ), ], diff --git a/tests/test_pytest_plugin.py b/tests/test_pytest_plugin.py index 0093342a..0f9cc887 100644 --- a/tests/test_pytest_plugin.py +++ b/tests/test_pytest_plugin.py @@ -51,8 +51,8 @@ def test_unihan_ensure_full( assert unihan_full_options.work_dir.exists() unihan_readings = unihan_full_options.work_dir / 'Unihan_Readings.txt' assert unihan_readings.stat().st_size > 6_200_000 - """ - ) + """, + ), }, tests_passed=1, ), @@ -90,8 +90,8 @@ def test_ensure_unihan_quick( unihan_readings = unihan_quick_options.work_dir / 'Unihan_Readings.txt' assert unihan_readings.stat().st_size >= 21_631 assert unihan_readings.stat().st_size < 30_000 - """ - ) + """, + ), }, tests_passed=1, ), @@ -99,7 +99,7 @@ def test_ensure_unihan_quick( @pytest.mark.parametrize( - PytestPluginFixture._fields, FIXTURES, ids=[f.test_id for f in FIXTURES] + PytestPluginFixture._fields, FIXTURES, ids=[f.test_id for f in FIXTURES], ) def test_plugin( pytester: pytest.Pytester, @@ -117,7 +117,7 @@ def test_plugin( """ [pytest] addopts=-vv - """.strip() + """.strip(), ), ) pytester.makeconftest( @@ -131,8 +131,8 @@ def setup( request: pytest.FixtureRequest, ) -> None: pass - """ - ) + """, + ), ) tests_path = pytester.path / "tests" first_test_key = next(iter(files.keys())) diff --git a/tests/test_unihan.py b/tests/test_unihan.py index 67ebfaa4..3923f5b0 100644 --- a/tests/test_unihan.py +++ b/tests/test_unihan.py @@ -42,7 +42,7 @@ def test_zip_has_files(unihan_mock_zip: zipfile.ZipFile) -> None: def test_has_valid_zip( - tmp_path: pathlib.Path, unihan_mock_zip: zipfile.ZipFile + tmp_path: pathlib.Path, unihan_mock_zip: zipfile.ZipFile, ) -> None: """Test has_valid_zip() returns whether zip file is valid.""" if UNIHAN_ZIP_PATH.is_file(): @@ -77,7 +77,7 @@ def test_filter_manifest() -> None: "kSpoofingVariant", "kTraditionalVariant", "kZVariant", - ] + ], } result = core.filter_manifest(["Unihan_Variants.txt"]) @@ -159,7 +159,7 @@ def urlretrieve( "work_dir": unihan_mock_test_dir / "downloads", "destination": data_path / "unihan.csv", }, - ) + ), ) p.download(urlretrieve_fn=urlretrieve) assert dest_path.exists() @@ -243,7 +243,7 @@ def test_normalize_only_output_requested_columns( assert [] == not_in_columns, "normalize filters columns not specified." assert set(in_columns).issubset( - set(unihan_quick_columns) + set(unihan_quick_columns), ), "normalize returns correct columns specified + ucn and char." @@ -393,7 +393,7 @@ def test_cli_plus_defaults(unihan_mock_zip_path: pathlib.Path) -> None: option_subset_one_field = {"fields": ["kDefinition"]} pkgr = Packager.from_cli(["-f", "kDefinition"]) assert_dict_contains_subset( - option_subset_one_field, dataclasses.asdict(pkgr.options) + option_subset_one_field, dataclasses.asdict(pkgr.options), ) option_subset_two_fields = {"fields": ["kDefinition", "kXerox"]} @@ -418,7 +418,7 @@ def test_cli_plus_defaults(unihan_mock_zip_path: pathlib.Path) -> None: pkgr = Packager.from_cli(["--format", "json"]) option_subset = {"format": "json"} assert_dict_contains_subset( - option_subset, dataclasses.asdict(pkgr.options), msg="format argument works" + option_subset, dataclasses.asdict(pkgr.options), msg="format argument works", ) From 036efa6588eb060ef6879059a269c860da0ca9f5 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 7 Feb 2024 17:56:38 -0600 Subject: [PATCH 2/7] ruff format (after adding flake8-commas) --- src/unihan_etl/core.py | 23 ++++++++++++++++------- src/unihan_etl/expansion.py | 6 ++++-- src/unihan_etl/pytest_plugin.py | 15 ++++++++++----- src/unihan_etl/types.py | 6 +++++- src/unihan_etl/util.py | 5 ++++- tests/test_expansion.py | 16 ++++++++++++---- tests/test_pytest_plugin.py | 4 +++- tests/test_unihan.py | 10 +++++++--- 8 files changed, 61 insertions(+), 24 deletions(-) diff --git a/src/unihan_etl/core.py b/src/unihan_etl/core.py index cc397f94..f035d209 100755 --- a/src/unihan_etl/core.py +++ b/src/unihan_etl/core.py @@ -146,7 +146,10 @@ def get_parser() -> argparse.ArgumentParser: help=f"Output of .csv. Default: {DESTINATION_DIR}/unihan.{{json,csv,yaml}}", ) parser.add_argument( - "-w", "--work-dir", dest="work_dir", help=f"Default: {WORK_DIR}", + "-w", + "--work-dir", + dest="work_dir", + help=f"Default: {WORK_DIR}", ) parser.add_argument( "-F", @@ -315,7 +318,8 @@ def load_data( """ log.info(f"Loading data: {', '.join([str(s) for s in files])}") raw_data = fileinput.FileInput( - files=files, openhook=fileinput.hook_encoded("utf-8"), + files=files, + openhook=fileinput.hook_encoded("utf-8"), ) log.info("Done loading data.") @@ -412,7 +416,8 @@ def expand_delimiters(normalized_data: "UntypedNormalizedData") -> "ExpandedExpo def listify( - data: "UntypedNormalizedData", fields: t.Sequence[str], + data: "UntypedNormalizedData", + fields: t.Sequence[str], ) -> "ListifiedExport": """Convert tabularized data to a CSV-friendly list. @@ -467,7 +472,8 @@ def validate_options( ) -> "TypeGuard[Options]": """Validate unihan-etl options.""" if not is_default_option("input_files", options.input_files) and is_default_option( - "fields", options.fields, + "fields", + options.fields, ): # Filter fields when only files specified. try: @@ -475,12 +481,14 @@ def validate_options( except (KeyError, FieldNotFound) as e: raise FileNotSupported(str(e)) from e elif not is_default_option("fields", options.fields) and is_default_option( - "input_files", options.input_files, + "input_files", + options.input_files, ): # Filter files when only field specified. options.input_files = get_files(options.fields) elif not is_default_option("fields", options.fields) and not is_default_option( - "input_files", options.input_files, + "input_files", + options.input_files, ): # Filter fields when only files specified. fields_in_files = get_fields(filter_manifest(options.input_files)) @@ -520,7 +528,8 @@ def __init__( setup_logger(logger=None, level=options.log_level or DEFAULT_OPTIONS.log_level) merged_options = dataclasses.replace( - DEFAULT_OPTIONS, **dataclasses.asdict(options), + DEFAULT_OPTIONS, + **dataclasses.asdict(options), ) self.options = merged_options diff --git a/src/unihan_etl/expansion.py b/src/unihan_etl/expansion.py index e897c282..55032425 100644 --- a/src/unihan_etl/expansion.py +++ b/src/unihan_etl/expansion.py @@ -448,7 +448,8 @@ def expand_kHanyuPinyin( virtual=int(g["virtual"]), ) expanded[i] = kHanyuPinyinDict( - locations=expanded[i]["locations"], readings=expanded[i]["readings"], + locations=expanded[i]["locations"], + readings=expanded[i]["readings"], ) return expanded @@ -511,7 +512,8 @@ def expand_kXHC1983( substituted=g["substituted"] == "*", ) expanded[i] = kXHC1983Dict( - locations=expanded[i]["locations"], reading=expanded[i]["reading"], + locations=expanded[i]["locations"], + reading=expanded[i]["reading"], ) return expanded diff --git a/src/unihan_etl/pytest_plugin.py b/src/unihan_etl/pytest_plugin.py index 80e9bee8..b6f10825 100644 --- a/src/unihan_etl/pytest_plugin.py +++ b/src/unihan_etl/pytest_plugin.py @@ -71,7 +71,8 @@ def unihan_full_options(unihan_full_path: pathlib.Path) -> UnihanOptions: @pytest.fixture(scope="session") def unihan_full_packager( - unihan_full_path: pathlib.Path, unihan_full_options: "UnihanOptions", + unihan_full_path: pathlib.Path, + unihan_full_options: "UnihanOptions", ) -> "Packager": """Return Packager for "full" portion of UNIHAN, return a UnihanOptions.""" return Packager(unihan_full_options) @@ -224,7 +225,8 @@ def unihan_quick_options( @pytest.fixture(scope="session") def unihan_quick_packager( - unihan_quick_path: pathlib.Path, unihan_quick_options: "UnihanOptions", + unihan_quick_path: pathlib.Path, + unihan_quick_options: "UnihanOptions", ) -> "Packager": """Bootstrap a small, but effective portion of UNIHAN, return a UnihanOptions.""" return Packager(unihan_quick_options) @@ -358,7 +360,8 @@ def unihan_home_user_name() -> str: @pytest.fixture(scope="session") def unihan_user_path( - unihan_home_path: pathlib.Path, unihan_home_user_name: str, + unihan_home_path: pathlib.Path, + unihan_home_user_name: str, ) -> pathlib.Path: """Return temporary user directory. @@ -427,7 +430,8 @@ def unihan_mock_test_dir(tmp_path_factory: pytest.TempPathFactory) -> pathlib.Pa @pytest.fixture(scope="session") def unihan_mock_zip_path( - unihan_mock_test_dir: pathlib.Path, unihan_mock_zip_pathname: str, + unihan_mock_test_dir: pathlib.Path, + unihan_mock_zip_pathname: str, ) -> pathlib.Path: """Return path to Unihan zipfile.""" return unihan_mock_test_dir / unihan_mock_zip_pathname @@ -435,7 +439,8 @@ def unihan_mock_zip_path( @pytest.fixture(scope="session") def unihan_mock_zip( - unihan_mock_zip_path: pathlib.Path, unihan_quick_data: str, + unihan_mock_zip_path: pathlib.Path, + unihan_quick_data: str, ) -> zipfile.ZipFile: """Return Unihan zipfile.""" zf = zipfile.ZipFile(str(unihan_mock_zip_path), "a") diff --git a/src/unihan_etl/types.py b/src/unihan_etl/types.py index 5b43b731..f4239574 100644 --- a/src/unihan_etl/types.py +++ b/src/unihan_etl/types.py @@ -63,7 +63,11 @@ class ReportHookFn(t.Protocol): """Progress bar callback for download().""" def __call__( - self, count: int, block_size: int, total_size: int, out: t.IO[str] = sys.stdout, + self, + count: int, + block_size: int, + total_size: int, + out: t.IO[str] = sys.stdout, ) -> object: """Print progress bar during download.""" ... diff --git a/src/unihan_etl/util.py b/src/unihan_etl/util.py index 63e6d16a..e6bfb293 100644 --- a/src/unihan_etl/util.py +++ b/src/unihan_etl/util.py @@ -73,7 +73,10 @@ def ucnstring_to_unicode(ucn_string: str) -> str: def _dl_progress( - count: int, block_size: int, total_size: int, out: t.IO[str] = sys.stdout, + count: int, + block_size: int, + total_size: int, + out: t.IO[str] = sys.stdout, ) -> None: """ MIT License: https://github.com/okfn/dpm-old/blob/master/dpm/util.py. diff --git a/tests/test_expansion.py b/tests/test_expansion.py index 785b982a..392402a7 100644 --- a/tests/test_expansion.py +++ b/tests/test_expansion.py @@ -62,7 +62,9 @@ def test_expand( ], ) def test_expand_kMandarin( - unihan_quick_expanded_data: ExpandedData, ucn: str, expected: t.Dict[str, str], + unihan_quick_expanded_data: ExpandedData, + ucn: str, + expected: t.Dict[str, str], ) -> None: """Test expansion of KMandarin. @@ -85,7 +87,9 @@ def test_expand_kMandarin( ], ) def test_expand_kTotalStrokes( - unihan_quick_expanded_data: ExpandedData, ucn: str, expected: t.Dict[str, int], + unihan_quick_expanded_data: ExpandedData, + ucn: str, + expected: t.Dict[str, int], ) -> None: """Test expansion of kTotalStrokes. @@ -110,7 +114,9 @@ def test_expand_kTotalStrokes( ], ) def test_expand_kIRGHanyuDaZidian( - ucn: str, expected: ExpandedData, unihan_quick_expanded_data: ExpandedData, + ucn: str, + expected: ExpandedData, + unihan_quick_expanded_data: ExpandedData, ) -> None: """Test expansion of kIRGHanyuDaZidian.""" item = next(i for i in unihan_quick_expanded_data if i["ucn"] == ucn) @@ -247,7 +253,9 @@ def test_expand_kHanyuPinyin( ], ) def test_expand_HanYu( - unihan_quick_expanded_data: ExpandedData, ucn: str, expected: ExpandedData, + unihan_quick_expanded_data: ExpandedData, + ucn: str, + expected: ExpandedData, ) -> None: """Test expansion of HanYu. diff --git a/tests/test_pytest_plugin.py b/tests/test_pytest_plugin.py index 0f9cc887..98ae87cd 100644 --- a/tests/test_pytest_plugin.py +++ b/tests/test_pytest_plugin.py @@ -99,7 +99,9 @@ def test_ensure_unihan_quick( @pytest.mark.parametrize( - PytestPluginFixture._fields, FIXTURES, ids=[f.test_id for f in FIXTURES], + PytestPluginFixture._fields, + FIXTURES, + ids=[f.test_id for f in FIXTURES], ) def test_plugin( pytester: pytest.Pytester, diff --git a/tests/test_unihan.py b/tests/test_unihan.py index 3923f5b0..0c285f1c 100644 --- a/tests/test_unihan.py +++ b/tests/test_unihan.py @@ -42,7 +42,8 @@ def test_zip_has_files(unihan_mock_zip: zipfile.ZipFile) -> None: def test_has_valid_zip( - tmp_path: pathlib.Path, unihan_mock_zip: zipfile.ZipFile, + tmp_path: pathlib.Path, + unihan_mock_zip: zipfile.ZipFile, ) -> None: """Test has_valid_zip() returns whether zip file is valid.""" if UNIHAN_ZIP_PATH.is_file(): @@ -393,7 +394,8 @@ def test_cli_plus_defaults(unihan_mock_zip_path: pathlib.Path) -> None: option_subset_one_field = {"fields": ["kDefinition"]} pkgr = Packager.from_cli(["-f", "kDefinition"]) assert_dict_contains_subset( - option_subset_one_field, dataclasses.asdict(pkgr.options), + option_subset_one_field, + dataclasses.asdict(pkgr.options), ) option_subset_two_fields = {"fields": ["kDefinition", "kXerox"]} @@ -418,7 +420,9 @@ def test_cli_plus_defaults(unihan_mock_zip_path: pathlib.Path) -> None: pkgr = Packager.from_cli(["--format", "json"]) option_subset = {"format": "json"} assert_dict_contains_subset( - option_subset, dataclasses.asdict(pkgr.options), msg="format argument works", + option_subset, + dataclasses.asdict(pkgr.options), + msg="format argument works", ) From 1ba0e54c72c43eca0359605a00040a9a90f91c7c Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 7 Feb 2024 17:57:31 -0600 Subject: [PATCH 3/7] pyproject(ruff): Add flake8-commas, ignore COM812 for `ruff format` See also: - https://docs.astral.sh/ruff/rules/#flake8-commas-com - https://pypi.org/project/flake8-commas/ Ignoring COM812: - https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules - https://docs.astral.sh/ruff/rules/missing-trailing-comma/ --- pyproject.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index cd0c2b0a..707c3c78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -158,6 +158,7 @@ select = [ "UP", # pyupgrade "B", # flake8-bugbear "C4", # flake8-comprehensions + "COM", # flake8-commas "Q", # flake8-quotes "PTH", # flake8-use-pathlib "SIM", # flake8-simplify @@ -166,6 +167,9 @@ select = [ "RUF", # Ruff-specific rules "D", # pydocstyle ] +ignore = [ + "COM812", # missing trailing comma, ruff format conflict +] [tool.ruff.lint.isort] known-first-party = ["unihan_etl", "cihai"] From 74bf82b24acbe98b66ef1a5315578a95434ccb98 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 7 Feb 2024 17:58:06 -0600 Subject: [PATCH 4/7] pyproject(ruff): flake8-builtins See also: - https://docs.astral.sh/ruff/rules/#flake8-builtins-a - https://pypi.org/project/flake8-builtins/ --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 707c3c78..4ae554b8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -156,6 +156,7 @@ select = [ "F", # pyflakes "I", # isort "UP", # pyupgrade + "A", # flake8-builtins "B", # flake8-bugbear "C4", # flake8-comprehensions "COM", # flake8-commas From a92b6bf6dd60e2576869b32d631126ea98229dc8 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 7 Feb 2024 17:58:30 -0600 Subject: [PATCH 5/7] fix!: Fix shadowing of python builtins docs/conf.py:61:1: A001 Variable `copyright` is shadowing a Python builtin --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 9e7074a5..85c5fbaa 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -58,7 +58,7 @@ master_doc = "index" project = about["__title__"] -copyright = about["__copyright__"] +project_copyright = about["__copyright__"] version = "%s" % (".".join(about["__version__"].split("."))[:2]) release = "%s" % (about["__version__"]) From 7a053e2966a2042c4957b4756d8339477c26fd45 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 7 Feb 2024 19:00:01 -0600 Subject: [PATCH 6/7] pyproject(ruff): Add flake8-errmsg See also: - https://docs.astral.sh/ruff/rules/#flake8-errmsg-em - https://pypi.org/project/flake8-errmsg/ --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 4ae554b8..8e2383a0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -160,6 +160,7 @@ select = [ "B", # flake8-bugbear "C4", # flake8-comprehensions "COM", # flake8-commas + "EM", # flake8-errmsg "Q", # flake8-quotes "PTH", # flake8-use-pathlib "SIM", # flake8-simplify From 5793cbbf8c430f950c7edc4f93418c13436fb913 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Thu, 8 Feb 2024 05:18:18 -0600 Subject: [PATCH 7/7] docs(CHANGES): Note linting improvements --- CHANGES | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/CHANGES b/CHANGES index 5753424b..6a8073d4 100644 --- a/CHANGES +++ b/CHANGES @@ -19,6 +19,25 @@ $ pipx install --suffix=@next unihan-etl --pip-args '\--pre' --force +### Development + +- Strengthen linting (#313) + + - Add flake8-commas (COM) + + - https://docs.astral.sh/ruff/rules/#flake8-commas-com + - https://pypi.org/project/flake8-commas/ + + - Add flake8-builtins (A) + + - https://docs.astral.sh/ruff/rules/#flake8-builtins-a + - https://pypi.org/project/flake8-builtins/ + + - Add flake8-errmsg (EM) + + - https://docs.astral.sh/ruff/rules/#flake8-errmsg-em + - https://pypi.org/project/flake8-errmsg/ + ## unihan-etl 0.32.0 (2024-02-05) ### Documentation