From b6162c61dacc3f6d2a54badc89038aae13a8c107 Mon Sep 17 00:00:00 2001 From: Andy Kluger Date: Sat, 11 Sep 2021 01:22:49 -0400 Subject: [PATCH] Address reviews #1477: - Replace the map-based dispatching with more readable blocks - Catch (unlikely) invalid style value now that it won't raise a KeyError, although it should be caught earlier via Click args - Add a little more comment context to the use of 24 as column size Co-authored-by: Albert Tugushev --- piptools/writer.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/piptools/writer.py b/piptools/writer.py index d57559254..0c5e47489 100644 --- a/piptools/writer.py +++ b/piptools/writer.py @@ -277,13 +277,15 @@ def _format_requirement( required_by.add(_comes_from_as_string(ireq)) if required_by: - annotation = { - "split": annotation_style_split, - "line": annotation_style_line, - }[self.annotation_style](required_by) - sep = {"split": "\n ", "line": "\n " if ireq_hashes else " "}[ - self.annotation_style - ] + if self.annotation_style == "split": + annotation = annotation_style_split(required_by) + sep = "\n " + elif self.annotation_style == "line": + annotation = annotation_style_line(required_by) + sep = "\n " if ireq_hashes else " " + else: # pragma: no cover + raise ValueError("Invalid value for annotation style") + # 24 is one reasonable column size to use here, that we've used in the past lines = f"{line:24}{sep}{comment(annotation)}".splitlines() line = "\n".join(ln.rstrip() for ln in lines)