1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
============
kgb Releases
============
kgb 7.3 (9-December-2025)
=========================
* Added support for Python 3.14.
* Added ``SpyAgency.get_spy()``, a utility method which returns the
``FunctionSpy`` for a given function or method. This is particularly useful
when using type checkers, which are unaware that the function itself has been
replaced.
* Fixed a ``DeprecationWarning`` which would appear when spying on generator
functions.
kgb 7.2 (3-November-2024)
=========================
* Added support for Python 3.13.
We've fixed crashes with some changes made to how Python 3.13 builds and
executes functions, and added explicit testing and support going forward.
kgb 7.1.1 (6-August-2022)
=========================
* Small packaging update to include the ``LICENSE`` file.
No code changes.
kgb 7.1 (4-August-2022)
=======================
* Added support for Python 3.11.
kgb 7 (20-January-2022)
=======================
* Added explicit support for Python 3.10.
* Dropped support for Python 2.6, 3.4, and 3.5.
* kgb now works as a plugin for pytest_.
Unit tests can use the ``spy_agency`` fixture to have a spy agency created
and ready for use. Spies will be automatically unregistered when the test
completes.
* Added snake_case versions of all assertion methods in ``SpyAgency``.
This includes:
* ``assert_has_spy``
* ``assert_spy_call_count``
* ``assert_spy_called_with``
* ``assert_spy_called``
* ``assert_spy_last_called_with``
* ``assert_spy_last_raised_message``
* ``assert_spy_last_raised``
* ``assert_spy_last_returned``
* ``assert_spy_not_called_with``
* ``assert_spy_not_called``
* ``assert_spy_raised_message``
* ``assert_spy_raised``
* ``assert_spy_returned``
* Added standalone assertion methods in ``kgb.asserts``.
This provides all the assertion methods shown above, but as standalone
methods that can work in any test suite.
* Added a ``func_name=`` argument when setting up spies, to avoid problems
with bad decorators.
When spying on an unbound method wrapped in a decorator that doesn't
preserve the function name, errors could occur.
In this case, you can pass ``func_name=`` when setting up the spy, telling
kgb about the original function name it should use.
This is a special situation. Most spies will not need to set this.
* Updated ``SpyCall.__repr__`` to list keyword arguments in sorted order.
* The package now lists the Python versions that are supported.
This will help down the road when we begin deprecating older versions of
Python, ensuring that ``pip`` will install the appropriate version of kgb
for the version of Python.
.. _pytest: https://pytest.org
kgb 6.1 (24-August-2021)
========================
* Added new ``SpyOpReturnInOrder`` and ``SpyOpRaiseInOrder`` spy operations.
``SpyOpReturnInOrder`` takes a list of values to return. Each call made
will return the next value from that list. An exception will be raised
if any further calls are made once the list is exhausted.
``SpyOpRaiseInOrder`` is similar, but takes a list of exceptions to raise.
Examples:
.. code-block:: python
spy_on(our_agent.get_identity, op=kgb.SpyOpReturnInOrder([
'nobody...',
'who?',
'not telling...',
]))
spy_on(pen.emit_poison, op=kgb.SpyOpRaiseInOrder([
PoisonEmptyError(),
Kaboom(),
MissingPenError(),
]))
* ``SpyOpMatchInOrder`` and ``SpyOpMatchAny`` now accept operations in the
expected calls.
These can be set through an ``op`` key, instead of setting ``call_fake``
or ``call_original``.
For example:
.. code-block:: python
spy_on(lockbox.enter_code, op=kgb.SpyOpMatchInOrder([
{
'args': (42, 42, 42, 42, 42, 42),
'op': kgb.SpyOpRaise(Kaboom()),
'call_original': True,
},
]))
Any operation can be provided. This also allows for advanced, reusable
rule sets by nesting, for example, ``SpyOpMatchInOrder`` inside
``SpyOpMatchAny``.
* ``UnexpectedCallError`` now lists the call that was made in the error
message.
kgb 6.0 (3-September-2020)
==========================
* Added a new ``@spy_for`` decorator.
This is an alternative to defining a function and then calling
``spy_on(func, call_fake=...)``. It takes a function or method to spy on
and an optional owner, much like ``spy_on()``.
For example:
.. code-block:: python
def test_doomsday_device(self):
dd = DoomsdayDevice()
@self.spy_for(dd.kaboom)
def _save_world(*args, **kwargs)
print('Sprinkles and ponies!')
* Added new support for Spy Operations.
Spy Operations can be thought of as pre-packaged "fake functions" for a spy,
which can perform some useful operations. There are a few built-in types:
* ``SpyOpMatchAny`` allows a caller to provide a list of all possible sets
of arguments that may be in one or more calls, triggering spy behavior
for the particular match (allowing ``call_original``/``call_fake`` to be
conditional on the arguments). Any call not provided in the list will
raise an ``UnexpectedCallError`` assertion.
* ``SpyOpMatchInOrder`` is similar to ``SpyOpMatchAny``, but the calls
must be in the order specified (which is useful for ensuring an order
of operations).
* ``SpyOpRaise`` takes an exception instance and raises it when the
function is called (preventing a caller from having to define a
wrapping function).
* ``SpyOpReturn`` takes a return value and returns it when the function is
called (similar to defining a simple lambda, but better specifying the
intent).
These are set with an ``op=`` argument, instead of a ``call_fake=``. For
example:
.. code-block:: python
spy_on(pen.emit_poison, op=kgb.SpyOpRaise(PoisonEmptyError()))
Or, for one of the more complex examples:
.. code-block:: python
spy_on(traps.trigger, op=kgb.SpyOpMatchAny([
{
'args': ('hallway_lasers',),
'call_fake': _send_wolves,
},
{
'args': ('trap_tile',),
'call_fake': _spill_hot_oil,
},
{
'args': ('infrared_camera',),
'kwargs': {
'sector': 'underground_passage',
},
'call_original': False,
},
]))
* Added an ``assertSpyNotCalledWith()`` assertion method.
Like the name suggests, it asserts that a spy has not been called with
the provided arguments. It's the inverse of ``assertSpyCalledWith()``.
* ``SpyAgency``'s assertion methods can now be used even without mixing it
into a ``TestCase``.
* Fixed a crash in ``SpyAgency.unspy_all()``.
* Fixed the grammar in an error message about slippery functions.
kgb 5.0 (10-April-2020)
=======================
* Added support for Python 3.8.
Functions with positional-only arguments on Python 3.8 will now work
correctly, and the positional-only arguments will factor into any spy
matching.
* Added several new unit test assertion methods:
* ``assertHasSpy``
* ``assertSpyCalled``
* ``assertSpyNotCalled``
* ``assertSpyCallCount``
* ``assertSpyCalledWith``
* ``assertSpyLastCalledWith``
* ``assertSpyReturned``
* ``assertSpyLastReturned``
* ``assertSpyRaised``
* ``assertSpyLastRaised``
* ``assertSpyRaisedMessage``
* ``assertSpyLastRaisedMessage``
We recommend using these for unit tests instead of checking individual
properties of calls, as they'll provide better output and help you find out
why spies have gone rogue.
* Added support for spying on "slippery" functions.
A slippery function is defined (by us) as a function on an object that is
actually a different function every time you access it. In other words, if
you were to just reference a slippery function as an attribute two times,
you'd end up with two separate copies of that function, each with their own
ID.
This can happen if the "function" is actually some decorator that returns a
new function every time it's accessed. A real-world example would be the
Python Stripe module's API functions, like ``stripe.Customer.delete``.
In previous versions of kgb, you wouldn't be able to spy on these
functions. With 5.0, you can spy on them just fine by passing
``owner=<instance>`` when setting up the spy:
.. code-block:: python
spy_on(myobj.slippery_func,
owner=myobj)
* Lots of internal changes to help keep the codebase organized and
manageable, as Python support increases.
kgb 4.0 (30-July-2019)
======================
* Added ``call_original()``, which calls the original spied-on function.
The call will not be logged, and will invoke the original behavior of
the function. This is useful when a spy simply needs to wrap another
function.
* Updated the Python 3 support to use the modern, non-deprecated support
for inspecting and formatting function/method signatures.
kgb 3.0 (23-March-2019)
=======================
* Added an argument to ``spy_on()`` for specifying an explicit owner class
for unbound methods, and warn if missing.
Python 3.x doesn't have a real way of determining the owning class for
unbound methods, and attempting to spy on an unbound method can end up
causing a number of problems, potentially interfering with spies that
are a subclass or superclass of the spied object.
``spy_on()`` now accepts an ``owner=`` parameter for unbound methods in
order to explicitly specify the class. It will warn if this is missing,
providing details on what it thinks the owner is and the recommended
changes to make to the call.
* Fixed spying on unbound methods originally defined on the parent class
of a specified or determined owning class.
* Fixed spying on old-syle classes (those not inheriting from ``object``)
on Python 2.6 and early versions of 2.7.
kgb 2.0.3 (18-August-2018)
==========================
* Added a version classifier for Python 3.7.
* Fixed a regression on Python 2.6.
kgb 2.0.2 (9-July-2018)
=======================
* Fixed spying on instances of classes with a custom ``__setattr__``.
* Fixed spying on classmethods defined in the parent of a class.
kgb 2.0.1 (12-March-2018)
=========================
* Fixed a regression in spying on classmethods.
* Fixed copying function annotations and keyword-only defaults in Python 3.
* Fixed problems executing some types of functions on Python 3.6.
kgb 2.0 (5-February-2018)
=========================
* Added compatibility with Python 3.6.
* Spy methods for standard functions no longer need to be accessed like:
.. code-block:: python
func.spy.last_call
Now you can call them the same way you could with methods:
.. code-block:: python
func.last_call
* The ``args`` and ``kwargs`` information recorded for a spy now correspond to
the function signature and not the way the function was called.
* ``called_with()`` now allows providing keyword arguments to check positional
arguments by name.
* When spying on a function fails for some reason, the error output is a
lot more helpful.
kgb 1.1 (5-December-2017)
=========================
* Added ``returned()``, ``last_returned()``, ``raised()``, ``last_raised()``,
``raised_with_message()``, and ``last_raised_with_message()`` methods to
function spies.
See the README for how this works.
* Added ``called_with()``, ``returned()``, ``raised()``, and
``raised_with_message()`` to the individual ``SpyCall`` objects.
These are accessed through ``spy.calls``, and allow for more conveniently
checking the results of specific calls in tests.
* ``called_with()`` and ``last_called_with()`` now accept matching subsets of
arguments.
Any number of leading positional arguments and any subset of keyword
arguments can be specified. Prior to 1.0, subsets of keyword arguments
were supported, but 1.0 temporarily made this more strict.
This is helpful when testing function calls containing many default
arguments or when the function takes ``*args`` and ``**kwargs``.
kgb 1.0 (31-October-2017)
=========================
* Added support for Python 3, including keyword-only arguments.
* Function signatures for spies now mimic that of the spied-on functions,
allowing Python's ``getargspec()`` to work.
kgb 0.5.3 (28-November-2015)
============================
* Objects that evaluate to false (such as objects inheriting from ``dict``)
can now be spied upon.
kgb 0.5.2 (17-March-2015)
=========================
* Expose the spy when using ``spy_on`` as a context manager.
Patch by Todd Wolfson.
kgb 0.5.1 (2-June-2014)
=======================
* Added support for spying on unbound member functions on classes.
kgb 0.5.0 (23-May-2013)
=======================
* First public release.
|