Skip to content

Commit

Permalink
style: Use f-strings wherever possible
Browse files Browse the repository at this point in the history
They're faster.
  • Loading branch information
DimitriPapadopoulos authored and adrienverge committed Oct 6, 2023
1 parent 816d575 commit f63e56f
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 132 deletions.
124 changes: 61 additions & 63 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ def test_run_with_implicit_extends_config(self):

with RunContext(self) as ctx:
cli.run(('-d', 'default', '-f', 'parsable', path))
expected_out = ('%s:1:1: [warning] missing document start "---" '
'(document-start)\n' % path)
expected_out = (f'{path}:1:1: [warning] missing document start "---" '
f'(document-start)\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (0, expected_out, ''))

Expand Down Expand Up @@ -424,9 +424,9 @@ def test_run_one_problem_file(self):
cli.run(('-f', 'parsable', path))
self.assertEqual(ctx.returncode, 1)
self.assertEqual(ctx.stdout, (
'%s:2:4: [error] trailing spaces (trailing-spaces)\n'
'%s:3:4: [error] no new line character at the end of file '
'(new-line-at-end-of-file)\n' % (path, path)))
f'{path}:2:4: [error] trailing spaces (trailing-spaces)\n'
f'{path}:3:4: [error] no new line character at the end of file '
f'(new-line-at-end-of-file)\n'))
self.assertEqual(ctx.stderr, '')

def test_run_one_warning(self):
Expand Down Expand Up @@ -476,8 +476,8 @@ def test_run_multiple_files(self):
cli.run(['-f', 'parsable'] + items)
self.assertEqual((ctx.returncode, ctx.stderr), (1, ''))
self.assertEqual(ctx.stdout, (
'%s:3:1: [error] duplication of key "key" in mapping '
'(key-duplicates)\n') % path)
f'{path}:3:1: [error] duplication of key "key" in mapping '
f'(key-duplicates)\n'))

def test_run_piped_output_nocolor(self):
path = os.path.join(self.wd, 'a.yaml')
Expand All @@ -486,11 +486,11 @@ def test_run_piped_output_nocolor(self):
cli.run((path, ))
self.assertEqual((ctx.returncode, ctx.stderr), (1, ''))
self.assertEqual(ctx.stdout, (
'%s\n'
' 2:4 error trailing spaces (trailing-spaces)\n'
' 3:4 error no new line character at the end of file '
'(new-line-at-end-of-file)\n'
'\n' % path))
f'{path}\n'
f' 2:4 error trailing spaces (trailing-spaces)\n'
f' 3:4 error no new line character at the end of file '
f'(new-line-at-end-of-file)\n'
f'\n'))

def test_run_default_format_output_in_tty(self):
path = os.path.join(self.wd, 'a.yaml')
Expand All @@ -517,25 +517,25 @@ def test_run_default_format_output_in_tty(self):
output.close()

self.assertEqual(out, (
'\033[4m%s\033[0m\n'
' \033[2m2:4\033[0m \033[31merror\033[0m '
'trailing spaces \033[2m(trailing-spaces)\033[0m\n'
' \033[2m3:4\033[0m \033[31merror\033[0m '
'no new line character at the end of file '
'\033[2m(new-line-at-end-of-file)\033[0m\n'
'\n' % path))
f'\033[4m{path}\033[0m\n'
f' \033[2m2:4\033[0m \033[31merror\033[0m '
f'trailing spaces \033[2m(trailing-spaces)\033[0m\n'
f' \033[2m3:4\033[0m \033[31merror\033[0m '
f'no new line character at the end of file '
f'\033[2m(new-line-at-end-of-file)\033[0m\n'
f'\n'))

def test_run_default_format_output_without_tty(self):
path = os.path.join(self.wd, 'a.yaml')

with RunContext(self) as ctx:
cli.run((path, ))
expected_out = (
'%s\n'
' 2:4 error trailing spaces (trailing-spaces)\n'
' 3:4 error no new line character at the end of file '
'(new-line-at-end-of-file)\n'
'\n' % path)
f'{path}\n'
f' 2:4 error trailing spaces (trailing-spaces)\n'
f' 3:4 error no new line character at the end of file '
f'(new-line-at-end-of-file)\n'
f'\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

Expand All @@ -545,11 +545,11 @@ def test_run_auto_output_without_tty_output(self):
with RunContext(self) as ctx:
cli.run((path, '--format', 'auto'))
expected_out = (
'%s\n'
' 2:4 error trailing spaces (trailing-spaces)\n'
' 3:4 error no new line character at the end of file '
'(new-line-at-end-of-file)\n'
'\n' % path)
f'{path}\n'
f' 2:4 error trailing spaces (trailing-spaces)\n'
f' 3:4 error no new line character at the end of file '
f'(new-line-at-end-of-file)\n'
f'\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

Expand All @@ -559,13 +559,13 @@ def test_run_format_colored(self):
with RunContext(self) as ctx:
cli.run((path, '--format', 'colored'))
expected_out = (
'\033[4m%s\033[0m\n'
' \033[2m2:4\033[0m \033[31merror\033[0m '
'trailing spaces \033[2m(trailing-spaces)\033[0m\n'
' \033[2m3:4\033[0m \033[31merror\033[0m '
'no new line character at the end of file '
'\033[2m(new-line-at-end-of-file)\033[0m\n'
'\n' % path)
f'\033[4m{path}\033[0m\n'
f' \033[2m2:4\033[0m \033[31merror\033[0m '
f'trailing spaces \033[2m(trailing-spaces)\033[0m\n'
f' \033[2m3:4\033[0m \033[31merror\033[0m '
f'no new line character at the end of file '
f'\033[2m(new-line-at-end-of-file)\033[0m\n'
f'\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

Expand All @@ -575,10 +575,10 @@ def test_run_format_colored_warning(self):
with RunContext(self) as ctx:
cli.run((path, '--format', 'colored'))
expected_out = (
'\033[4m%s\033[0m\n'
' \033[2m1:1\033[0m \033[33mwarning\033[0m '
'missing document start "---" \033[2m(document-start)\033[0m\n'
'\n' % path)
f'\033[4m{path}\033[0m\n'
f' \033[2m1:1\033[0m \033[33mwarning\033[0m '
f'missing document start "---" \033[2m(document-start)\033[0m\n'
f'\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (0, expected_out, ''))

Expand All @@ -588,13 +588,12 @@ def test_run_format_github(self):
with RunContext(self) as ctx:
cli.run((path, '--format', 'github'))
expected_out = (
'::group::%s\n'
'::error file=%s,line=2,col=4::2:4 [trailing-spaces] trailing'
' spaces\n'
'::error file=%s,line=3,col=4::3:4 [new-line-at-end-of-file] no'
' new line character at the end of file\n'
'::endgroup::\n\n'
% (path, path, path))
f'::group::{path}\n'
f'::error file={path},line=2,col=4::2:4 [trailing-spaces] trailing'
f' spaces\n'
f'::error file={path},line=3,col=4::3:4 [new-line-at-end-of-file]'
f' no new line character at the end of file\n'
f'::endgroup::\n\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

Expand All @@ -608,13 +607,12 @@ def test_github_actions_detection(self):
os.environ['GITHUB_WORKFLOW'] = 'something'
cli.run((path, ))
expected_out = (
'::group::%s\n'
'::error file=%s,line=2,col=4::2:4 [trailing-spaces] trailing'
' spaces\n'
'::error file=%s,line=3,col=4::3:4 [new-line-at-end-of-file] no'
' new line character at the end of file\n'
'::endgroup::\n\n'
% (path, path, path))
f'::group::{path}\n'
f'::error file={path},line=2,col=4::2:4 [trailing-spaces] trailing'
f' spaces\n'
f'::error file={path},line=3,col=4::3:4 [new-line-at-end-of-file]'
f' no new line character at the end of file\n'
f'::endgroup::\n\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

Expand All @@ -640,11 +638,11 @@ def test_run_no_warnings(self):
with RunContext(self) as ctx:
cli.run((path, '--no-warnings', '-f', 'auto'))
expected_out = (
'%s\n'
' 2:4 error trailing spaces (trailing-spaces)\n'
' 3:4 error no new line character at the end of file '
'(new-line-at-end-of-file)\n'
'\n' % path)
f'{path}\n'
f' 2:4 error trailing spaces (trailing-spaces)\n'
f' 3:4 error no new line character at the end of file '
f'(new-line-at-end-of-file)\n'
f'\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

Expand All @@ -671,10 +669,10 @@ def test_run_non_universal_newline(self):
with RunContext(self) as ctx:
cli.run(('-d', 'rules:\n new-lines:\n type: unix', path))
expected_out = (
'%s\n'
' 1:4 error wrong new line character: expected \\n'
' (new-lines)\n'
'\n' % path)
f'{path}\n'
f' 1:4 error wrong new line character: expected \\n'
f' (new-lines)\n'
f'\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

Expand Down
2 changes: 1 addition & 1 deletion tests/test_spec_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# text = text.replace('\u21d4', '') # byte order mark
# text = text.replace('\u2192', '\t') # right arrow
# text = text.replace('\u00b0', '') # empty scalar
# with open('tests/yaml-1.2-spec-examples/%s' % id, 'w',
# with open(f'tests/yaml-1.2-spec-examples/{id}', 'w',
# encoding='utf-8') as g:
# g.write(text)

Expand Down
43 changes: 16 additions & 27 deletions yamllint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,52 +49,41 @@ def supports_color():
class Format:
@staticmethod
def parsable(problem, filename):
return ('%(file)s:%(line)s:%(column)s: [%(level)s] %(message)s' %
{'file': filename,
'line': problem.line,
'column': problem.column,
'level': problem.level,
'message': problem.message})
return (f'{filename}:{problem.line}:{problem.column}: '
f'[{problem.level}] {problem.message}')

@staticmethod
def standard(problem, filename):
line = ' %d:%d' % (problem.line, problem.column)
line = f' {problem.line}:{problem.column}'
line += max(12 - len(line), 0) * ' '
line += problem.level
line += max(21 - len(line), 0) * ' '
line += problem.desc
if problem.rule:
line += ' (%s)' % problem.rule
line += f' ({problem.rule})'
return line

@staticmethod
def standard_color(problem, filename):
line = ' \033[2m%d:%d\033[0m' % (problem.line, problem.column)
line = f' \033[2m{problem.line}:{problem.column}\033[0m'
line += max(20 - len(line), 0) * ' '
if problem.level == 'warning':
line += '\033[33m%s\033[0m' % problem.level
line += f'\033[33m{problem.level}\033[0m'
else:
line += '\033[31m%s\033[0m' % problem.level
line += f'\033[31m{problem.level}\033[0m'
line += max(38 - len(line), 0) * ' '
line += problem.desc
if problem.rule:
line += ' \033[2m(%s)\033[0m' % problem.rule
line += f' \033[2m({problem.rule})\033[0m'
return line

@staticmethod
def github(problem, filename):
line = '::'
line += problem.level
line += ' file=' + filename + ','
line += 'line=' + format(problem.line) + ','
line += 'col=' + format(problem.column)
line += '::'
line += format(problem.line)
line += ':'
line += format(problem.column)
line += ' '
line = f'::{problem.level} file={format(filename)},' \
f'line={format(problem.line)},col={format(problem.column)}' \
f'::{format(problem.line)}:{format(problem.column)} '
if problem.rule:
line += '[' + problem.rule + '] '
line += f'[{problem.rule}] '
line += problem.desc
return line

Expand All @@ -118,12 +107,12 @@ def show_problems(problems, file, args_format, no_warn):
print(Format.parsable(problem, file))
elif args_format == 'github':
if first:
print('::group::%s' % file)
print(f'::group::{file}')
first = False
print(Format.github(problem, file))
elif args_format == 'colored':
if first:
print('\033[4m%s\033[0m' % file)
print(f'\033[4m{file}\033[0m')
first = False
print(Format.standard_color(problem, file))
else:
Expand Down Expand Up @@ -184,7 +173,7 @@ def run(argv=None):
action='store_true',
help='output only error level problems')
parser.add_argument('-v', '--version', action='version',
version='{} {}'.format(APP_NAME, APP_VERSION))
version=f'{APP_NAME} {APP_VERSION}')

args = parser.parse_args(argv)

Expand All @@ -202,7 +191,7 @@ def run(argv=None):
try:
if args.config_data is not None:
if args.config_data != '' and ':' not in args.config_data:
args.config_data = 'extends: ' + args.config_data
args.config_data = f'extends: {args.config_data}'
conf = YamlLintConfig(content=args.config_data)
elif args.config_file is not None:
conf = YamlLintConfig(file=args.config_file)
Expand Down
Loading

0 comments on commit f63e56f

Please sign in to comment.