Skip to content

Commit 5dcb24c

Browse files
committed
test: upgrade mock helpers to autospec by default
1 parent 1eda7f3 commit 5dcb24c

28 files changed

+719
-923
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/docs/.build/
44
/*.egg-info
55
*.pyc
6+
.pytest_cache/
67
_scratch/
78
Session.vim
89
/.tox/

docx/blkcntnr.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
# encoding: utf-8
22

3-
"""
4-
Block item container, used by body, cell, header, etc. Block level items are
5-
things like paragraph and table, although there are a few other specialized
6-
ones like structured document tags.
3+
"""Block item container, used by body, cell, header, etc.
4+
5+
Block level items are things like paragraph and table, although there are a few other
6+
specialized ones like structured document tags.
77
"""
88

9-
from __future__ import absolute_import, print_function
9+
from __future__ import absolute_import, division, print_function, unicode_literals
1010

11-
from .oxml.table import CT_Tbl
12-
from .shared import Parented
13-
from .text.paragraph import Paragraph
11+
from docx.oxml.table import CT_Tbl
12+
from docx.shared import Parented
13+
from docx.text.paragraph import Paragraph
1414

1515

1616
class BlockItemContainer(Parented):
17+
"""Base class for proxy objects that can contain block items.
18+
19+
These containers include _Body, _Cell, header, footer, footnote, endnote, comment,
20+
and text box objects. Provides the shared functionality to add a block item like
21+
a paragraph or table.
1722
"""
18-
Base class for proxy objects that can contain block items, such as _Body,
19-
_Cell, header, footer, footnote, endnote, comment, and text box objects.
20-
Provides the shared functionality to add a block item like a paragraph or
21-
table.
22-
"""
23+
2324
def __init__(self, element, parent):
2425
super(BlockItemContainer, self).__init__(parent)
2526
self._element = element

docx/document.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
# encoding: utf-8
22

3-
"""
4-
|Document| and closely related objects
5-
"""
3+
"""|Document| and closely related objects"""
64

7-
from __future__ import (
8-
absolute_import, division, print_function, unicode_literals
9-
)
5+
from __future__ import absolute_import, division, print_function, unicode_literals
106

11-
from .blkcntnr import BlockItemContainer
12-
from .enum.section import WD_SECTION
13-
from .enum.text import WD_BREAK
14-
from .section import Section, Sections
15-
from .shared import ElementProxy, Emu
7+
from docx.blkcntnr import BlockItemContainer
8+
from docx.enum.section import WD_SECTION
9+
from docx.enum.text import WD_BREAK
10+
from docx.section import Section, Sections
11+
from docx.shared import ElementProxy, Emu
1612

1713

1814
class Document(ElementProxy):
19-
"""
20-
WordprocessingML (WML) document. Not intended to be constructed directly.
21-
Use :func:`docx.Document` to open or create a document.
15+
"""WordprocessingML (WML) document.
16+
17+
Not intended to be constructed directly. Use :func:`docx.Document` to open or create
18+
a document.
2219
"""
2320

2421
__slots__ = ('_part', '__body')
@@ -28,25 +25,21 @@ def __init__(self, element, part):
2825
self._part = part
2926
self.__body = None
3027

31-
def add_heading(self, text='', level=1):
32-
"""
33-
Return a heading paragraph newly added to the end of the document,
34-
containing *text* and having its paragraph style determined by
35-
*level*. If *level* is 0, the style is set to `Title`. If *level* is
36-
1 (or omitted), `Heading 1` is used. Otherwise the style is set to
37-
`Heading {level}`. Raises |ValueError| if *level* is outside the
38-
range 0-9.
28+
def add_heading(self, text="", level=1):
29+
"""Return a heading paragraph newly added to the end of the document.
30+
31+
The heading paragraph will contain *text* and have its paragraph style
32+
determined by *level*. If *level* is 0, the style is set to `Title`. If *level*
33+
is 1 (or omitted), `Heading 1` is used. Otherwise the style is set to `Heading
34+
{level}`. Raises |ValueError| if *level* is outside the range 0-9.
3935
"""
4036
if not 0 <= level <= 9:
4137
raise ValueError("level must be in range 0-9, got %d" % level)
42-
style = 'Title' if level == 0 else 'Heading %d' % level
38+
style = "Title" if level == 0 else "Heading %d" % level
4339
return self.add_paragraph(text, style)
4440

4541
def add_page_break(self):
46-
"""
47-
Return a paragraph newly added to the end of the document and
48-
containing only a page break.
49-
"""
42+
"""Return newly |Paragraph| object containing only a page break."""
5043
paragraph = self.add_paragraph()
5144
paragraph.add_run().add_break(WD_BREAK.PAGE)
5245
return paragraph

docx/opc/package.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class OpcPackage(object):
2323
the :meth:`open` class method with a path to a package file or file-like
2424
object containing one.
2525
"""
26+
2627
def __init__(self):
2728
super(OpcPackage, self).__init__()
2829

@@ -174,10 +175,8 @@ def _core_properties_part(self):
174175

175176

176177
class Unmarshaller(object):
177-
"""
178-
Hosts static methods for unmarshalling a package from a |PackageReader|
179-
instance.
180-
"""
178+
"""Hosts static methods for unmarshalling a package from a |PackageReader|."""
179+
181180
@staticmethod
182181
def unmarshal(pkg_reader, package, part_factory):
183182
"""

docx/oxml/table.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,8 @@ def width(self, value):
367367

368368

369369
class CT_Tc(BaseOxmlElement):
370-
"""
371-
``<w:tc>`` table cell element
372-
"""
370+
"""`w:tc` table cell element"""
371+
373372
tcPr = ZeroOrOne('w:tcPr') # bunches of successors, overriding insert
374373
p = OneOrMore('w:p')
375374
tbl = OneOrMore('w:tbl')

docx/package.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# encoding: utf-8
22

3-
"""
4-
WordprocessingML Package class and related objects
5-
"""
3+
"""WordprocessingML Package class and related objects"""
64

7-
from __future__ import absolute_import, print_function, unicode_literals
5+
from __future__ import absolute_import, division, print_function, unicode_literals
86

97
from docx.image.image import Image
108
from docx.opc.constants import RELATIONSHIP_TYPE as RT
@@ -47,12 +45,9 @@ def _gather_image_parts(self):
4745

4846

4947
class ImageParts(object):
50-
"""
51-
Collection of |ImagePart| instances corresponding to each image part in
52-
the package.
53-
"""
48+
"""Collection of |ImagePart| objects corresponding to images in the package"""
49+
5450
def __init__(self):
55-
super(ImageParts, self).__init__()
5651
self._image_parts = []
5752

5853
def __contains__(self, item):
@@ -68,10 +63,10 @@ def append(self, item):
6863
self._image_parts.append(item)
6964

7065
def get_or_add_image_part(self, image_descriptor):
71-
"""
72-
Return an |ImagePart| instance containing the image identified by
73-
*image_descriptor*, newly created if a matching one is not present in
74-
the collection.
66+
"""Return |ImagePart| object containing image identified by *image_descriptor*.
67+
68+
The image-part is newly created if a matching one is not present in the
69+
collection.
7570
"""
7671
image = Image.from_file(image_descriptor)
7772
matching_image_part = self._get_by_sha1(image.sha1)

docx/parts/document.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
# encoding: utf-8
22

3-
"""
4-
|DocumentPart| and closely related objects
5-
"""
3+
"""|DocumentPart| and closely related objects"""
64

7-
from __future__ import (
8-
absolute_import, division, print_function, unicode_literals
9-
)
5+
from __future__ import absolute_import, division, print_function, unicode_literals
106

11-
from ..document import Document
12-
from .numbering import NumberingPart
13-
from ..opc.constants import RELATIONSHIP_TYPE as RT
14-
from ..opc.part import XmlPart
15-
from ..oxml.shape import CT_Inline
16-
from ..shape import InlineShapes
17-
from ..shared import lazyproperty
18-
from .settings import SettingsPart
19-
from .styles import StylesPart
7+
from docx.document import Document
8+
from docx.opc.constants import RELATIONSHIP_TYPE as RT
9+
from docx.opc.part import XmlPart
10+
from docx.oxml.shape import CT_Inline
11+
from docx.parts.numbering import NumberingPart
12+
from docx.parts.settings import SettingsPart
13+
from docx.parts.styles import StylesPart
14+
from docx.shape import InlineShapes
15+
from docx.shared import lazyproperty
2016

2117

2218
class DocumentPart(XmlPart):
19+
"""Main document part of a WordprocessingML (WML) package, aka a .docx file.
20+
21+
Acts as broker to other parts such as image, core properties, and style parts. It
22+
also acts as a convenient delegate when a mid-document object needs a service
23+
involving a remote ancestor. The `Parented.part` property inherited by many content
24+
objects provides access to this part object for that purpose.
2325
"""
24-
Main document part of a WordprocessingML (WML) package, aka a .docx file.
25-
Acts as broker to other parts such as image, core properties, and style
26-
parts. It also acts as a convenient delegate when a mid-document object
27-
needs a service involving a remote ancestor. The `Parented.part` property
28-
inherited by many content objects provides access to this part object for
29-
that purpose.
30-
"""
26+
3127
@property
3228
def core_properties(self):
3329
"""
@@ -84,10 +80,10 @@ def inline_shapes(self):
8480
return InlineShapes(self._element.body, self)
8581

8682
def new_pic_inline(self, image_descriptor, width, height):
87-
"""
88-
Return a newly-created `w:inline` element containing the image
89-
specified by *image_descriptor* and scaled based on the values of
90-
*width* and *height*.
83+
"""Return a newly-created `w:inline` element.
84+
85+
The element contains the image specified by *image_descriptor* and is scaled
86+
based on the values of *width* and *height*.
9187
"""
9288
rId, image = self.get_or_add_image(image_descriptor)
9389
cx, cy = image.scaled_dimensions(width, height)

docx/styles/styles.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
# encoding: utf-8
22

3-
"""
4-
Styles object, container for all objects in the styles part.
5-
"""
3+
"""Styles object, container for all objects in the styles part"""
64

7-
from __future__ import (
8-
absolute_import, division, print_function, unicode_literals
9-
)
5+
from __future__ import absolute_import, division, print_function, unicode_literals
106

117
from warnings import warn
128

13-
from . import BabelFish
14-
from .latent import LatentStyles
15-
from ..shared import ElementProxy
16-
from .style import BaseStyle, StyleFactory
9+
from docx.shared import ElementProxy
10+
from docx.styles import BabelFish
11+
from docx.styles.latent import LatentStyles
12+
from docx.styles.style import BaseStyle, StyleFactory
1713

1814

1915
class Styles(ElementProxy):
20-
"""
21-
A collection providing access to the styles defined in a document.
22-
Accessed using the :attr:`.Document.styles` property. Supports ``len()``,
23-
iteration, and dictionary-style access by style name.
16+
"""Provides access to the styles defined in a document.
17+
18+
Accessed using the :attr:`.Document.styles` property. Supports ``len()``, iteration,
19+
and dictionary-style access by style name.
2420
"""
2521

2622
__slots__ = ()
@@ -87,9 +83,9 @@ def default(self, style_type):
8783
return StyleFactory(style)
8884

8985
def get_by_id(self, style_id, style_type):
90-
"""
91-
Return the style of *style_type* matching *style_id*. Returns the
92-
default for *style_type* if *style_id* is not found or is |None|, or
86+
"""Return the style of *style_type* matching *style_id*.
87+
88+
Returns the default for *style_type* if *style_id* is not found or is |None|, or
9389
if the style having *style_id* is not of *style_type*.
9490
"""
9591
if style_id is None:

tests/image/test_bmp.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@
1212
from docx.image.constants import MIME_TYPE
1313
from docx.image.bmp import Bmp
1414

15-
from ..unitutil.mock import initializer_mock
15+
from ..unitutil.mock import ANY, initializer_mock
1616

1717

1818
class DescribeBmp(object):
1919

20-
def it_can_construct_from_a_bmp_stream(self, from_stream_fixture):
21-
stream, Bmp__init__, cx, cy, horz_dpi, vert_dpi = from_stream_fixture
20+
def it_can_construct_from_a_bmp_stream(self, Bmp__init__):
21+
cx, cy, horz_dpi, vert_dpi = 26, 43, 200, 96
22+
bytes_ = (
23+
b'fillerfillerfiller\x1A\x00\x00\x00\x2B\x00\x00\x00'
24+
b'fillerfiller\xB8\x1E\x00\x00\x00\x00\x00\x00'
25+
)
26+
stream = BytesIO(bytes_)
27+
2228
bmp = Bmp.from_stream(stream)
23-
Bmp__init__.assert_called_once_with(cx, cy, horz_dpi, vert_dpi)
29+
30+
Bmp__init__.assert_called_once_with(ANY, cx, cy, horz_dpi, vert_dpi)
2431
assert isinstance(bmp, Bmp)
2532

2633
def it_knows_its_content_type(self):
@@ -33,16 +40,6 @@ def it_knows_its_default_ext(self):
3340

3441
# fixtures -------------------------------------------------------
3542

36-
@pytest.fixture
37-
def from_stream_fixture(self, Bmp__init__):
38-
cx, cy, horz_dpi, vert_dpi = 26, 43, 200, 96
39-
bytes_ = (
40-
b'fillerfillerfiller\x1A\x00\x00\x00\x2B\x00\x00\x00'
41-
b'fillerfiller\xB8\x1E\x00\x00\x00\x00\x00\x00'
42-
)
43-
stream = BytesIO(bytes_)
44-
return stream, Bmp__init__, cx, cy, horz_dpi, vert_dpi
45-
4643
@pytest.fixture
4744
def Bmp__init__(self, request):
4845
return initializer_mock(request, Bmp)

0 commit comments

Comments
 (0)