From ec1453728770dc8b8877f7aef888c7ccca5a3d05 Mon Sep 17 00:00:00 2001 From: Joris Roovers Date: Sat, 31 Oct 2020 12:04:37 +0100 Subject: Replace format() with f-strings Replaces most occurences for format() with f-strings (supported since Python 3.6) - this makes for more readable code in most cases. Relates to #151 --- docs/user_defined_rules.md | 5 ++- examples/my_commit_rules.py | 4 +-- examples/my_line_rules.py | 2 +- gitlint/cli.py | 16 ++++----- gitlint/config.py | 56 ++++++++++++++--------------- gitlint/git.py | 8 ++--- gitlint/hooks.py | 10 +++--- gitlint/lint.py | 13 ++++--- gitlint/options.py | 21 ++++++----- gitlint/rule_finder.py | 48 ++++++++++++------------- gitlint/rules.py | 26 +++++++------- gitlint/tests/base.py | 8 ++--- gitlint/tests/cli/test_cli.py | 20 +++++------ gitlint/tests/cli/test_cli_hooks.py | 7 ++-- gitlint/tests/config/test_config.py | 6 ++-- gitlint/tests/config/test_config_builder.py | 8 ++--- gitlint/tests/contrib/test_contrib_rules.py | 3 +- gitlint/tests/git/test_git.py | 2 +- gitlint/tests/git/test_git_commit.py | 6 ++-- gitlint/tests/rules/test_title_rules.py | 2 +- gitlint/tests/rules/test_user_rules.py | 5 +-- gitlint/tests/test_hooks.py | 10 +++--- gitlint/tests/test_lint.py | 4 +-- gitlint/tests/test_options.py | 9 +++-- qa/base.py | 3 +- qa/samples/user_rules/extra/extra_rules.py | 16 ++++----- qa/test_commits.py | 2 +- qa/test_gitlint.py | 7 ++-- qa/test_hooks.py | 6 ++-- 29 files changed, 160 insertions(+), 173 deletions(-) diff --git a/docs/user_defined_rules.md b/docs/user_defined_rules.md index 88d1dd8..fd944d1 100644 --- a/docs/user_defined_rules.md +++ b/docs/user_defined_rules.md @@ -152,7 +152,7 @@ class SpecialChars(LineRule): # options can be accessed by looking them up by their name in self.options for char in self.options['special-chars'].value: if char in line: - msg = "Title contains the special character '{0}'".format(char) + msg = f"Title contains the special character '{char}'" violation = RuleViolation(self.id, msg, line) violations.append(violation) @@ -262,8 +262,7 @@ class BodyMaxLineCount(CommitRule): line_count = len(commit.message.body) max_line_count = self.options['max-line-count'].value if line_count > max_line_count: - message = "Body contains too many lines ({0} > {1})".format(line_count, - max_line_count) + message = f"Body contains too many lines ({line_count} > {max_line_count})" return [RuleViolation(self.id, message, line_nr=1)] ``` diff --git a/examples/my_commit_rules.py b/examples/my_commit_rules.py index 5e8ce08..5a66c94 100644 --- a/examples/my_commit_rules.py +++ b/examples/my_commit_rules.py @@ -35,7 +35,7 @@ class BodyMaxLineCount(CommitRule): line_count = len(commit.message.body) max_line_count = self.options['max-line-count'].value if line_count > max_line_count: - message = "Body contains too many lines ({0} > {1})".format(line_count, max_line_count) + message = f"Body contains too many lines ({line_count} > {max_line_count})" return [RuleViolation(self.id, message, line_nr=1)] @@ -88,7 +88,7 @@ class BranchNamingConventions(CommitRule): break if not valid_branch_name: - msg = "Branch name '{0}' does not start with one of {1}".format(branch, allowed_branch_prefixes) + msg = f"Branch name '{branch}' does not start with one of {allowed_branch_prefixes}" violations.append(RuleViolation(self.id, msg, line_nr=1)) return violations diff --git a/examples/my_line_rules.py b/examples/my_line_rules.py index 820024a..3a1ef36 100644 --- a/examples/my_line_rules.py +++ b/examples/my_line_rules.py @@ -45,7 +45,7 @@ class SpecialChars(LineRule): # options can be accessed by looking them up by their name in self.options for char in self.options['special-chars'].value: if char in line: - msg = "Title contains the special character '{0}'".format(char) + msg = f"Title contains the special character '{char}'" violation = RuleViolation(self.id, msg, line) violations.append(violation) diff --git a/gitlint/cli.py b/gitlint/cli.py index a42d4c6..76a9734 100644 --- a/gitlint/cli.py +++ b/gitlint/cli.py @@ -187,7 +187,7 @@ class ContextObj: type=click.Path(exists=True, resolve_path=True, file_okay=False, readable=True), help="Path of the target git repository. [default: current working directory]") @click.option('-C', '--config', type=click.Path(exists=True, dir_okay=False, readable=True, resolve_path=True), - help="Config file location [default: {0}]".format(DEFAULT_CONFIG_FILE)) + help=f"Config file location [default: {DEFAULT_CONFIG_FILE}]") @click.option('-c', multiple=True, help="Config flags in format .