Skip to content

Commit a229c4a

Browse files
committed
style: apply ruff format to entire codebase
1 parent 604a202 commit a229c4a

45 files changed

Lines changed: 2692 additions & 2383 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎assertpy2/assertpy.py‎

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,31 @@
5656
from .snapshot import SnapshotMixin
5757
from .string import StringMixin
5858

59-
__version__ = '2.0.0'
59+
__version__ = "2.0.0"
6060

6161
__tracebackhide__ = True # clean tracebacks via py.test integration
6262
contextlib.__tracebackhide__ = True # monkey patch contextlib with clean py.test tracebacks
6363

6464
# assertpy files
65-
ASSERTPY_FILES = [os.path.join('assertpy2', file) for file in [
66-
'assertpy.py',
67-
'base.py',
68-
'collection.py',
69-
'contains.py',
70-
'date.py',
71-
'dict.py',
72-
'dynamic.py',
73-
'exception.py',
74-
'extracting.py',
75-
'file.py',
76-
'helpers.py',
77-
'numeric.py',
78-
'snapshot.py',
79-
'string.py'
80-
]]
65+
ASSERTPY_FILES = [
66+
os.path.join("assertpy2", file)
67+
for file in [
68+
"assertpy.py",
69+
"base.py",
70+
"collection.py",
71+
"contains.py",
72+
"date.py",
73+
"dict.py",
74+
"dynamic.py",
75+
"exception.py",
76+
"extracting.py",
77+
"file.py",
78+
"helpers.py",
79+
"numeric.py",
80+
"snapshot.py",
81+
"string.py",
82+
]
83+
]
8184

8285
# soft assertions
8386
_soft_ctx = 0
@@ -136,13 +139,13 @@ def soft_assertions() -> Iterator[None]:
136139
_soft_ctx -= 1
137140

138141
if _soft_err and _soft_ctx == 0:
139-
out = 'soft assertion failures:\n' + '\n'.join('%d. %s' % (i + 1, msg) for i, msg in enumerate(_soft_err))
142+
out = "soft assertion failures:\n" + "\n".join("%d. %s" % (i + 1, msg) for i, msg in enumerate(_soft_err))
140143
_soft_err = []
141144
raise AssertionError(out)
142145

143146

144147
# factory methods
145-
def assert_that(val, description=''):
148+
def assert_that(val, description=""):
146149
"""Set the value to be tested, plus an optional description, and allow assertions to be called.
147150
148151
This is a factory method for the :class:`AssertionBuilder`, and the single most important
@@ -165,11 +168,11 @@ def test_something():
165168
"""
166169
global _soft_ctx
167170
if _soft_ctx:
168-
return _builder(val, description, 'soft')
171+
return _builder(val, description, "soft")
169172
return _builder(val, description)
170173

171174

172-
def assert_warn(val, description='', logger=None):
175+
def assert_warn(val, description="", logger=None):
173176
"""Set the value to be tested, and optional description and logger, and allow assertions to be
174177
called, but never fail, only log warnings.
175178
@@ -211,10 +214,10 @@ def assert_warn(val, description='', logger=None):
211214
Use :meth:`assert_warn` if and only if you have a *really* good reason to log assertion
212215
failures instead of failing.
213216
"""
214-
return _builder(val, description, 'warn', logger=logger)
217+
return _builder(val, description, "warn", logger=logger)
215218

216219

217-
def fail(msg=''):
220+
def fail(msg=""):
218221
"""Force immediate test failure with the given message.
219222
220223
Args:
@@ -239,10 +242,10 @@ def test_adder_bad_arg():
239242
except TypeError as e:
240243
assert_that(str(e)).contains('unsupported operand')
241244
"""
242-
raise AssertionError('Fail: %s!' % msg if msg else 'Fail!')
245+
raise AssertionError("Fail: %s!" % msg if msg else "Fail!")
243246

244247

245-
def soft_fail(msg=''):
248+
def soft_fail(msg=""):
246249
"""Within a :meth:`soft_assertions` context, append the failure message to the soft error list,
247250
but do not halt test execution.
248251
@@ -273,7 +276,7 @@ def soft_fail(msg=''):
273276
global _soft_ctx
274277
if _soft_ctx:
275278
global _soft_err
276-
_soft_err.append('Fail: %s!' % msg if msg else 'Fail!')
279+
_soft_err.append("Fail: %s!" % msg if msg else "Fail!")
277280
return
278281
fail(msg)
279282

@@ -311,7 +314,7 @@ def test_6():
311314
# 6 is NOT 5!
312315
"""
313316
if not callable(func):
314-
raise TypeError('func must be callable')
317+
raise TypeError("func must be callable")
315318
_extensions[func.__name__] = func
316319

317320

@@ -329,12 +332,12 @@ def remove_extension(func):
329332
remove_extension(is_5)
330333
"""
331334
if not callable(func):
332-
raise TypeError('func must be callable')
335+
raise TypeError("func must be callable")
333336
if func.__name__ in _extensions:
334337
del _extensions[func.__name__]
335338

336339

337-
def _builder(val, description='', kind=None, expected=None, logger=None):
340+
def _builder(val, description="", kind=None, expected=None, logger=None):
338341
"""Internal helper to build a new :class:`AssertionBuilder` instance and glue on any extension methods."""
339342
ab = AssertionBuilder(val, description, kind, expected, logger)
340343
if _extensions:
@@ -366,13 +369,13 @@ def _unwind(frame):
366369
prev = frame
367370

368371
filename, lineno = _unwind(inspect.currentframe())
369-
return '[%s:%d]: %s' % (os.path.basename(filename), lineno, msg), kwargs
372+
return "[%s:%d]: %s" % (os.path.basename(filename), lineno, msg), kwargs
370373

371374

372-
_logger = logging.getLogger('assertpy2')
375+
_logger = logging.getLogger("assertpy2")
373376
_handler = logging.StreamHandler(sys.stdout)
374377
_handler.setLevel(logging.WARNING)
375-
_format = logging.Formatter('%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
378+
_format = logging.Formatter("%(asctime)s %(levelname)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
376379
_handler.setFormatter(_format)
377380
_logger.addHandler(_handler)
378381
_default_logger = WarningLoggingAdapter(_logger, None)
@@ -391,7 +394,7 @@ class AssertionBuilder(
391394
DateMixin,
392395
ContainsMixin,
393396
CollectionMixin,
394-
BaseMixin
397+
BaseMixin,
395398
):
396399
"""The main assertion class. Never call the constructor directly, always use the
397400
:meth:`assert_that` helper instead. Or if you just want warning messages, use the
@@ -407,15 +410,15 @@ class AssertionBuilder(
407410
logger (Logger, optional): the logger for warning messages. Defaults to ``None``
408411
"""
409412

410-
def __init__(self, val, description='', kind=None, expected=None, logger=None):
413+
def __init__(self, val, description="", kind=None, expected=None, logger=None):
411414
"""Never call this constructor directly."""
412415
self.val = val
413416
self.description = description
414417
self.kind = kind
415418
self.expected = expected
416419
self.logger = logger if logger else _default_logger
417420

418-
def builder(self, val, description='', kind=None, expected=None, logger=None):
421+
def builder(self, val, description="", kind=None, expected=None, logger=None):
419422
"""Helper to build a new :class:`AssertionBuilder` instance. Use this only if not chaining to ``self``.
420423
421424
Args:
@@ -453,11 +456,11 @@ def error(self, msg) -> Self:
453456
AssertionBuilder: returns this instance to chain to the next assertion, but only when
454457
``AssertionError`` is not raised, as is the case when ``kind`` is ``warn`` or ``soft``.
455458
"""
456-
out = '%s%s' % ('[%s] ' % self.description if len(self.description) > 0 else '', msg)
457-
if self.kind == 'warn':
459+
out = "%s%s" % ("[%s] " % self.description if len(self.description) > 0 else "", msg)
460+
if self.kind == "warn":
458461
self.logger.warning(out)
459462
return self
460-
elif self.kind == 'soft':
463+
elif self.kind == "soft":
461464
global _soft_err
462465
_soft_err.append(out)
463466
return self

‎assertpy2/base.py‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,14 @@ def is_equal_to(self, other, **kwargs) -> Self:
124124
See Also:
125125
:meth:`~assertpy.string.StringMixin.is_equal_to_ignoring_case` - for case-insensitive string equality
126126
"""
127-
if self._check_dict_like(self.val, check_values=False, return_as_bool=True) and \
128-
self._check_dict_like(other, check_values=False, return_as_bool=True):
129-
if self._dict_not_equal(self.val, other, ignore=kwargs.get('ignore'), include=kwargs.get('include')):
130-
self._dict_err(self.val, other, ignore=kwargs.get('ignore'), include=kwargs.get('include'))
127+
if self._check_dict_like(self.val, check_values=False, return_as_bool=True) and self._check_dict_like(
128+
other, check_values=False, return_as_bool=True
129+
):
130+
if self._dict_not_equal(self.val, other, ignore=kwargs.get("ignore"), include=kwargs.get("include")):
131+
self._dict_err(self.val, other, ignore=kwargs.get("ignore"), include=kwargs.get("include"))
131132
else:
132133
if self.val != other:
133-
return self.error('Expected <%s> to be equal to <%s>, but was not.' % (self.val, other))
134+
return self.error("Expected <%s> to be equal to <%s>, but was not." % (self.val, other))
134135
return self
135136

136137
def is_not_equal_to(self, other) -> Self:
@@ -160,7 +161,7 @@ def is_not_equal_to(self, other) -> Self:
160161
AssertionError: if actual **is** equal to expected
161162
"""
162163
if self.val == other:
163-
return self.error('Expected <%s> to be not equal to <%s>, but was.' % (self.val, other))
164+
return self.error("Expected <%s> to be not equal to <%s>, but was." % (self.val, other))
164165
return self
165166

166167
def is_same_as(self, other) -> Self:
@@ -202,7 +203,7 @@ def is_same_as(self, other) -> Self:
202203
AssertionError: if actual is **not** identical to expected
203204
"""
204205
if self.val is not other:
205-
return self.error('Expected <%s> to be identical to <%s>, but was not.' % (self.val, other))
206+
return self.error("Expected <%s> to be identical to <%s>, but was not." % (self.val, other))
206207
return self
207208

208209
def is_not_same_as(self, other) -> Self:
@@ -233,7 +234,7 @@ def is_not_same_as(self, other) -> Self:
233234
AssertionError: if actual **is** identical to expected
234235
"""
235236
if self.val is other:
236-
return self.error('Expected <%s> to be not identical to <%s>, but was.' % (self.val, other))
237+
return self.error("Expected <%s> to be not identical to <%s>, but was." % (self.val, other))
237238
return self
238239

239240
def is_true(self) -> Self:
@@ -258,7 +259,7 @@ def is_true(self) -> Self:
258259
AssertionError: if val **is** false
259260
"""
260261
if not self.val:
261-
return self.error('Expected <%s> to be <True>, but was not.' % self.val)
262+
return self.error("Expected <%s> to be <True>, but was not." % self.val)
262263
return self
263264

264265
def is_false(self) -> Self:
@@ -283,7 +284,7 @@ def is_false(self) -> Self:
283284
AssertionError: if val **is** true
284285
"""
285286
if self.val:
286-
return self.error('Expected <%s> to be <False>, but was not.' % self.val)
287+
return self.error("Expected <%s> to be <False>, but was not." % self.val)
287288
return self
288289

289290
def is_none(self) -> Self:
@@ -302,7 +303,7 @@ def is_none(self) -> Self:
302303
AssertionError: if val is **not** none
303304
"""
304305
if self.val is not None:
305-
return self.error('Expected <%s> to be <None>, but was not.' % self.val)
306+
return self.error("Expected <%s> to be <None>, but was not." % self.val)
306307
return self
307308

308309
def is_not_none(self) -> Self:
@@ -322,11 +323,11 @@ def is_not_none(self) -> Self:
322323
AssertionError: if val **is** none
323324
"""
324325
if self.val is None:
325-
return self.error('Expected not <None>, but was.')
326+
return self.error("Expected not <None>, but was.")
326327
return self
327328

328329
def _type(self, val):
329-
if hasattr(val, '__name__'):
330+
if hasattr(val, "__name__"):
330331
return val.__name__
331332
return val.__class__.__name__
332333

@@ -355,10 +356,10 @@ def is_type_of(self, some_type) -> Self:
355356
AssertionError: if val is **not** of the given type
356357
"""
357358
if type(some_type) is not type and not issubclass(type(some_type), type):
358-
raise TypeError('given arg must be a type')
359+
raise TypeError("given arg must be a type")
359360
if type(self.val) is not some_type:
360361
t = self._type(self.val)
361-
return self.error('Expected <%s:%s> to be of type <%s>, but was not.' % (self.val, t, some_type.__name__))
362+
return self.error("Expected <%s:%s> to be of type <%s>, but was not." % (self.val, t, some_type.__name__))
362363
return self
363364

364365
def is_instance_of(self, some_class) -> Self:
@@ -395,9 +396,11 @@ class Foo: pass
395396
try:
396397
if not isinstance(self.val, some_class):
397398
t = self._type(self.val)
398-
return self.error('Expected <%s:%s> to be instance of class <%s>, but was not.' % (self.val, t, some_class.__name__))
399+
return self.error(
400+
"Expected <%s:%s> to be instance of class <%s>, but was not." % (self.val, t, some_class.__name__)
401+
)
399402
except TypeError:
400-
raise TypeError('given arg must be a class') from None
403+
raise TypeError("given arg must be a class") from None
401404
return self
402405

403406
def is_length(self, length) -> Self:
@@ -424,9 +427,9 @@ def is_length(self, length) -> Self:
424427
AssertionError: if val is **not** the given length
425428
"""
426429
if type(length) is not int:
427-
raise TypeError('given arg must be an int')
430+
raise TypeError("given arg must be an int")
428431
if length < 0:
429-
raise ValueError('given arg must be a positive int')
432+
raise ValueError("given arg must be a positive int")
430433
if len(self.val) != length:
431-
return self.error('Expected <%s> to be of length <%d>, but was <%d>.' % (self.val, length, len(self.val)))
434+
return self.error("Expected <%s> to be of length <%d>, but was <%d>." % (self.val, length, len(self.val)))
432435
return self

0 commit comments

Comments
 (0)