Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating string substitution with modern f-string style in assertMappingEqual. A prerequisite for upcoming changes for a clean diff. #323

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions absl/testing/absltest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
tests.
"""

from collections import abc
import collections
from collections import abc
import contextlib
import dataclasses
import difflib
Expand Down Expand Up @@ -1700,14 +1700,14 @@ def assertMappingEqual(

if not isinstance(a, mapping_type):
self.fail(
'a should be a %s, found type: %s'
% (mapping_type.__name__, type(a).__name__),
f'a should be a {mapping_type.__name__}, found type:'
f' {type(a).__name__}',
msg,
)
if not isinstance(b, mapping_type):
self.fail(
'b should be a %s, found type: %s'
% (mapping_type.__name__, type(b).__name__),
f'b should be a {mapping_type.__name__}, found type:'
f' {type(b).__name__}',
msg,
)

Expand All @@ -1733,9 +1733,9 @@ def Repr(dikt):
# Sort the entries based on their repr, not based on their sort order,
# which will be non-deterministic across executions, for many types.
entries = sorted((safe_repr(k), safe_repr(v)) for k, v in dikt.items())
return '{%s}' % (', '.join('%s: %s' % pair for pair in entries))
return '{' + ', '.join(f'{k}: {v}' for k, v in entries) + '}'

message = ['%s != %s%s' % (Repr(a), Repr(b), ' (%s)' % msg if msg else '')]
message = [f'{Repr(a)} != {Repr(b)}{"("+msg+")" if msg else ""}']

# The standard library default output confounds lexical difference with
# value difference; treat them separately.
Expand All @@ -1751,20 +1751,24 @@ def Repr(dikt):

if unexpected:
message.append(
'Unexpected, but present entries:\n%s' % ''.join(
'%s: %s\n' % (safe_repr(k), safe_repr(v)) for k, v in unexpected))
'Unexpected, but present entries:\n'
+ ''.join(f'{safe_repr(k)}: {safe_repr(v)}\n' for k, v in unexpected)
)

if different:
message.append(
'repr() of differing entries:\n%s' % ''.join(
'%s: %s != %s\n' % (safe_repr(k), safe_repr(a_value),
safe_repr(b_value))
for k, a_value, b_value in different))
'repr() of differing entries:\n'
+ ''.join(
f'{safe_repr(k)}: {safe_repr(a_value)} != {safe_repr(b_value)}\n'
for k, a_value, b_value in different
)
)

if missing:
message.append(
'Missing entries:\n%s' % ''.join(
('%s: %s\n' % (safe_repr(k), safe_repr(v)) for k, v in missing)))
'Missing entries:\n'
+ ''.join(f'{safe_repr(k)}: {safe_repr(v)}\n' for k, v in missing)
)

raise self.failureException('\n'.join(message))

Expand Down
Loading