#17351: merge with 3.3.
This commit is contained in:
commit
a41fb4be5b
|
@ -1034,7 +1034,7 @@ arbitrary object as a message format string, and that the logging package will
|
||||||
call ``str()`` on that object to get the actual format string. Consider the
|
call ``str()`` on that object to get the actual format string. Consider the
|
||||||
following two classes::
|
following two classes::
|
||||||
|
|
||||||
class BraceMessage(object):
|
class BraceMessage:
|
||||||
def __init__(self, fmt, *args, **kwargs):
|
def __init__(self, fmt, *args, **kwargs):
|
||||||
self.fmt = fmt
|
self.fmt = fmt
|
||||||
self.args = args
|
self.args = args
|
||||||
|
@ -1043,7 +1043,7 @@ following two classes::
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.fmt.format(*self.args, **self.kwargs)
|
return self.fmt.format(*self.args, **self.kwargs)
|
||||||
|
|
||||||
class DollarMessage(object):
|
class DollarMessage:
|
||||||
def __init__(self, fmt, **kwargs):
|
def __init__(self, fmt, **kwargs):
|
||||||
self.fmt = fmt
|
self.fmt = fmt
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
|
@ -1370,7 +1370,7 @@ works::
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
|
||||||
class MyHandler(object):
|
class MyHandler:
|
||||||
"""
|
"""
|
||||||
A simple handler for logging events. It runs in the listener process and
|
A simple handler for logging events. It runs in the listener process and
|
||||||
dispatches events to loggers based on the name in the received record,
|
dispatches events to loggers based on the name in the received record,
|
||||||
|
|
|
@ -225,7 +225,7 @@ function. The following wrapper makes that easy to do::
|
||||||
|
|
||||||
def cmp_to_key(mycmp):
|
def cmp_to_key(mycmp):
|
||||||
'Convert a cmp= function into a key= function'
|
'Convert a cmp= function into a key= function'
|
||||||
class K(object):
|
class K:
|
||||||
def __init__(self, obj, *args):
|
def __init__(self, obj, *args):
|
||||||
self.obj = obj
|
self.obj = obj
|
||||||
def __lt__(self, other):
|
def __lt__(self, other):
|
||||||
|
|
|
@ -381,7 +381,7 @@ and maps them to the context management protocol::
|
||||||
|
|
||||||
from contextlib import contextmanager, ExitStack
|
from contextlib import contextmanager, ExitStack
|
||||||
|
|
||||||
class ResourceManager(object):
|
class ResourceManager:
|
||||||
|
|
||||||
def __init__(self, acquire_resource, release_resource, check_resource_ok=None):
|
def __init__(self, acquire_resource, release_resource, check_resource_ok=None):
|
||||||
self.acquire_resource = acquire_resource
|
self.acquire_resource = acquire_resource
|
||||||
|
|
|
@ -324,7 +324,7 @@ are always available. They are listed here in alphabetical order.
|
||||||
'__initializing__', '__loader__', '__name__', '__package__',
|
'__initializing__', '__loader__', '__name__', '__package__',
|
||||||
'_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
|
'_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
|
||||||
'unpack', 'unpack_from']
|
'unpack', 'unpack_from']
|
||||||
>>> class Shape(object):
|
>>> class Shape:
|
||||||
... def __dir__(self):
|
... def __dir__(self):
|
||||||
... return ['area', 'perimeter', 'location']
|
... return ['area', 'perimeter', 'location']
|
||||||
>>> s = Shape()
|
>>> s = Shape()
|
||||||
|
|
|
@ -45,7 +45,7 @@ the correct arguments.
|
||||||
This example tests that calling `ProductionClass().method` results in a call to
|
This example tests that calling `ProductionClass().method` results in a call to
|
||||||
the `something` method:
|
the `something` method:
|
||||||
|
|
||||||
>>> class ProductionClass(object):
|
>>> class ProductionClass:
|
||||||
... def method(self):
|
... def method(self):
|
||||||
... self.something(1, 2, 3)
|
... self.something(1, 2, 3)
|
||||||
... def something(self, a, b, c):
|
... def something(self, a, b, c):
|
||||||
|
@ -69,7 +69,7 @@ in the correct way.
|
||||||
The simple `ProductionClass` below has a `closer` method. If it is called with
|
The simple `ProductionClass` below has a `closer` method. If it is called with
|
||||||
an object then it calls `close` on it.
|
an object then it calls `close` on it.
|
||||||
|
|
||||||
>>> class ProductionClass(object):
|
>>> class ProductionClass:
|
||||||
... def closer(self, something):
|
... def closer(self, something):
|
||||||
... something.close()
|
... something.close()
|
||||||
...
|
...
|
||||||
|
@ -412,7 +412,7 @@ ends:
|
||||||
Where you use `patch` to create a mock for you, you can get a reference to the
|
Where you use `patch` to create a mock for you, you can get a reference to the
|
||||||
mock using the "as" form of the with statement:
|
mock using the "as" form of the with statement:
|
||||||
|
|
||||||
>>> class ProductionClass(object):
|
>>> class ProductionClass:
|
||||||
... def method(self):
|
... def method(self):
|
||||||
... pass
|
... pass
|
||||||
...
|
...
|
||||||
|
@ -460,7 +460,7 @@ testable way in the first place...
|
||||||
|
|
||||||
So, suppose we have some code that looks a little bit like this:
|
So, suppose we have some code that looks a little bit like this:
|
||||||
|
|
||||||
>>> class Something(object):
|
>>> class Something:
|
||||||
... def __init__(self):
|
... def __init__(self):
|
||||||
... self.backend = BackendProvider()
|
... self.backend = BackendProvider()
|
||||||
... def method(self):
|
... def method(self):
|
||||||
|
@ -568,7 +568,7 @@ mock this using a `MagicMock`.
|
||||||
|
|
||||||
Here's an example class with an "iter" method implemented as a generator:
|
Here's an example class with an "iter" method implemented as a generator:
|
||||||
|
|
||||||
>>> class Foo(object):
|
>>> class Foo:
|
||||||
... def iter(self):
|
... def iter(self):
|
||||||
... for i in [1, 2, 3]:
|
... for i in [1, 2, 3]:
|
||||||
... yield i
|
... yield i
|
||||||
|
@ -678,7 +678,7 @@ function will be turned into a bound method if it is fetched from an instance.
|
||||||
It will have `self` passed in as the first argument, which is exactly what I
|
It will have `self` passed in as the first argument, which is exactly what I
|
||||||
wanted:
|
wanted:
|
||||||
|
|
||||||
>>> class Foo(object):
|
>>> class Foo:
|
||||||
... def foo(self):
|
... def foo(self):
|
||||||
... pass
|
... pass
|
||||||
...
|
...
|
||||||
|
@ -1197,7 +1197,7 @@ for us.
|
||||||
You can see in this example how a 'standard' call to `assert_called_with` isn't
|
You can see in this example how a 'standard' call to `assert_called_with` isn't
|
||||||
sufficient:
|
sufficient:
|
||||||
|
|
||||||
>>> class Foo(object):
|
>>> class Foo:
|
||||||
... def __init__(self, a, b):
|
... def __init__(self, a, b):
|
||||||
... self.a, self.b = a, b
|
... self.a, self.b = a, b
|
||||||
...
|
...
|
||||||
|
@ -1224,7 +1224,7 @@ A comparison function for our `Foo` class might look something like this:
|
||||||
And a matcher object that can use comparison functions like this for its
|
And a matcher object that can use comparison functions like this for its
|
||||||
equality operation would look something like this:
|
equality operation would look something like this:
|
||||||
|
|
||||||
>>> class Matcher(object):
|
>>> class Matcher:
|
||||||
... def __init__(self, compare, some_obj):
|
... def __init__(self, compare, some_obj):
|
||||||
... self.compare = compare
|
... self.compare = compare
|
||||||
... self.some_obj = some_obj
|
... self.some_obj = some_obj
|
||||||
|
|
|
@ -715,7 +715,7 @@ apply to method calls on the mock object.
|
||||||
Fetching a `PropertyMock` instance from an object calls the mock, with
|
Fetching a `PropertyMock` instance from an object calls the mock, with
|
||||||
no args. Setting it calls the mock with the value being set.
|
no args. Setting it calls the mock with the value being set.
|
||||||
|
|
||||||
>>> class Foo(object):
|
>>> class Foo:
|
||||||
... @property
|
... @property
|
||||||
... def foo(self):
|
... def foo(self):
|
||||||
... return 'something'
|
... return 'something'
|
||||||
|
@ -1051,7 +1051,7 @@ can set the `return_value` to be anything you want.
|
||||||
To configure return values on methods of *instances* on the patched class
|
To configure return values on methods of *instances* on the patched class
|
||||||
you must do this on the `return_value`. For example:
|
you must do this on the `return_value`. For example:
|
||||||
|
|
||||||
>>> class Class(object):
|
>>> class Class:
|
||||||
... def method(self):
|
... def method(self):
|
||||||
... pass
|
... pass
|
||||||
...
|
...
|
||||||
|
@ -1224,7 +1224,7 @@ deleting and either iteration or membership test. This corresponds to the
|
||||||
magic methods `__getitem__`, `__setitem__`, `__delitem__` and either
|
magic methods `__getitem__`, `__setitem__`, `__delitem__` and either
|
||||||
`__iter__` or `__contains__`.
|
`__iter__` or `__contains__`.
|
||||||
|
|
||||||
>>> class Container(object):
|
>>> class Container:
|
||||||
... def __init__(self):
|
... def __init__(self):
|
||||||
... self.values = {}
|
... self.values = {}
|
||||||
... def __getitem__(self, name):
|
... def __getitem__(self, name):
|
||||||
|
@ -1398,7 +1398,7 @@ inform the patchers of the different prefix by setting `patch.TEST_PREFIX`:
|
||||||
>>> value = 3
|
>>> value = 3
|
||||||
>>>
|
>>>
|
||||||
>>> @patch('__main__.value', 'not three')
|
>>> @patch('__main__.value', 'not three')
|
||||||
... class Thing(object):
|
... class Thing:
|
||||||
... def foo_one(self):
|
... def foo_one(self):
|
||||||
... print value
|
... print value
|
||||||
... def foo_two(self):
|
... def foo_two(self):
|
||||||
|
@ -2157,7 +2157,7 @@ created in the `__init__` method and not to exist on the class at all.
|
||||||
`autospec` can't know about any dynamically created attributes and restricts
|
`autospec` can't know about any dynamically created attributes and restricts
|
||||||
the api to visible attributes.
|
the api to visible attributes.
|
||||||
|
|
||||||
>>> class Something(object):
|
>>> class Something:
|
||||||
... def __init__(self):
|
... def __init__(self):
|
||||||
... self.a = 33
|
... self.a = 33
|
||||||
...
|
...
|
||||||
|
@ -2200,7 +2200,7 @@ class attributes (shared between instances of course) is faster too. e.g.
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
class Something(object):
|
class Something:
|
||||||
a = 33
|
a = 33
|
||||||
|
|
||||||
This brings up another issue. It is relatively common to provide a default
|
This brings up another issue. It is relatively common to provide a default
|
||||||
|
@ -2211,7 +2211,7 @@ spec, and probably indicates a member that will normally of some other type,
|
||||||
`autospec` doesn't use a spec for members that are set to `None`. These will
|
`autospec` doesn't use a spec for members that are set to `None`. These will
|
||||||
just be ordinary mocks (well - `MagicMocks`):
|
just be ordinary mocks (well - `MagicMocks`):
|
||||||
|
|
||||||
>>> class Something(object):
|
>>> class Something:
|
||||||
... member = None
|
... member = None
|
||||||
...
|
...
|
||||||
>>> mock = create_autospec(Something)
|
>>> mock = create_autospec(Something)
|
||||||
|
@ -2226,7 +2226,7 @@ production class. Both of these require you to use an alternative object as
|
||||||
the spec. Thankfully `patch` supports this - you can simply pass the
|
the spec. Thankfully `patch` supports this - you can simply pass the
|
||||||
alternative object as the `autospec` argument:
|
alternative object as the `autospec` argument:
|
||||||
|
|
||||||
>>> class Something(object):
|
>>> class Something:
|
||||||
... def __init__(self):
|
... def __init__(self):
|
||||||
... self.a = 33
|
... self.a = 33
|
||||||
...
|
...
|
||||||
|
|
Loading…
Reference in New Issue