8000 rfctr: rename private classes now used for typing · python-openxml/python-docx@160e709 · GitHub
[go: up one dir, main page]

Skip to content

Commit 160e709

Browse files
committed
rfctr: rename private classes now used for typing
Also for example `BaseStoryPart` -> `StoryPart` for the same reason.
1 parent 8280771 commit 160e709

File tree

8000

6 files changed

+54
-46
lines changed

6 files changed

+54
-46
lines changed

src/docx/parts/document.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
from docx.parts.hdrftr import FooterPart, HeaderPart
66
from docx.parts.numbering import NumberingPart
77
from docx.parts.settings import SettingsPart
8-
from docx.parts.story import BaseStoryPart
8+
from docx.parts.story import StoryPart
99
from docx.parts.styles import StylesPart
1010
from docx.shape import InlineShapes
1111
from docx.shared import lazyproperty
1212

1313

14-
class DocumentPart(BaseStoryPart):
14+
class DocumentPart(StoryPart):
1515
"""Main document part of a WordprocessingML (WML) package, aka a .docx file.
1616
1717
Acts as broker to other parts such as image, core properties, and style parts. It

src/docx/parts/hdrftr.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
from docx.opc.constants import CONTENT_TYPE as CT
66
from docx.oxml import parse_xml
7-
from docx.parts.story import BaseStoryPart
7+
from docx.parts.story import StoryPart
88

99

10-
class FooterPart(BaseStoryPart):
10+
class FooterPart(StoryPart):
1111
"""Definition of a section footer."""
1212

1313
@classmethod
@@ -29,7 +29,7 @@ def _default_footer_xml(cls):
2929
return xml_bytes
3030

3131

32-
class HeaderPart(BaseStoryPart):
32+
class HeaderPart(StoryPart):
3333
"""Definition of a section header."""
3434

3535
@classmethod

src/docx/parts/story.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
"""|BaseStoryPart| and related objects."""
1+
"""|StoryPart| and related objects."""
22

33
from docx.opc.constants import RELATIONSHIP_TYPE as RT
44
from docx.opc.part import XmlPart
55
from docx.oxml.shape import CT_Inline
66
from docx.shared import lazyproperty
77

88

9-
class BaseStoryPart(XmlPart):
9+
class StoryPart(XmlPart):
1010
"""Base class for story parts.
1111
1212
A story part is one that can contain textual content, such as the document-part and

src/docx/styles/style.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def StyleFactory(style_elm):
1111
"""Return a style object of the appropriate |BaseStyle| subclass, according to the
1212
type of `style_elm`."""
1313
style_cls = {
14-
WD_STYLE_TYPE.PARAGRAPH: _ParagraphStyle,
15-
WD_STYLE_TYPE.CHARACTER: _CharacterStyle,
14+
WD_STYLE_TYPE.PARAGRAPH: ParagraphStyle,
15+
WD_STYLE_TYPE.CHARACTER: CharacterStyle,
1616
WD_STYLE_TYPE.TABLE: _TableStyle,
1717
WD_STYLE_TYPE.LIST: _NumberingStyle,
1818
}[style_elm.type]
@@ -153,7 +153,7 @@ def unhide_when_used(self, value):
153153
self._element.unhideWhenUsed_val = value
154154

155155

156-
class _CharacterStyle(BaseStyle):
156+
class CharacterStyle(BaseStyle):
157157
"""A character style.
158158
159159
A character style is applied to a |Run| object and primarily provides character-
@@ -181,7 +181,11 @@ def font(self):
181181
return Font(self._element)
182182

183183

184-
class _ParagraphStyle(_CharacterStyle):
184+
# -- just in case someone uses the old name in an extension function --
185+
_CharacterStyle = CharacterStyle
186+
187+
188+
class ParagraphStyle(CharacterStyle):
185189
"""A paragraph style.
186190
187191
A paragraph style provides both character formatting and paragraph formatting such
@@ -220,7 +224,11 @@ def paragraph_format(self):
220224
return ParagraphFormat(self._element)
221225

222226

223-
class _TableStyle(_ParagraphStyle):
227+
# -- just in case someone uses the old name in an extension function --
228+
_ParagraphStyle = ParagraphStyle
229+
230+
231+
class _TableStyle(ParagraphStyle):
224232
"""A table style.
225233
226234
A table style provides character and paragraph formatting for its contents as well

tests/parts/test_story.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
from docx.package import Package
99
from docx.parts.document import DocumentPart
1010
from docx.parts.image import ImagePart
11-
from docx.parts.story import BaseStoryPart
11+
from docx.parts.story import StoryPart
1212
from docx.styles.style import BaseStyle
1313

1414
from ..unitutil.cxml import element
1515
from ..unitutil.file import snippet_text
1616
from ..unitutil.mock import instance_mock, method_mock, property_mock
1717

1818

19-
class DescribeBaseStoryPart(object):
19+
class DescribeStoryPart(object):
2020
def it_can_get_or_add_an_image(self, package_, image_part_, image_, relate_to_):
2121
package_.get_or_add_image_part.return_value = image_part_
2222
relate_to_.return_value = "rId42"
2323
image_part_.image = image_
24-
story_part = BaseStoryPart(None, None, None, package_)
24+
story_part = StoryPart(None, None, None, package_)
2525

2626
rId, image = story_part.get_or_add_image("image.png")
2727

@@ -37,7 +37,7 @@ def it_can_get_a_style_by_id_and_type(
3737
style_type = WD_STYLE_TYPE.PARAGRAPH
3838
_document_part_prop_.return_value = document_part_
3939
document_part_.get_style.return_value = style_
40-
story_part = BaseStoryPart(None, None, None, None)
40+
story_part = StoryPart(None, None, None, None)
4141

4242
style = story_part.get_style(style_id, style_type)
4343

@@ -50,7 +50,7 @@ def it_can_get_a_style_id_by_style_or_name_and_type(
5050
style_type = WD_STYLE_TYPE.PARAGRAPH
5151
_document_part_prop_.return_value = document_part_
5252
document_part_.get_style_id.return_value = "BodyText"
53-
story_part = BaseStoryPart(None, None, None, None)
53+
story_part = StoryPart(None, None, None, None)
5454

5555
style_id = story_part.get_style_id(style_, style_type)
5656

@@ -63,7 +63,7 @@ def it_can_create_a_new_pic_inline(self, get_or_add_image_, image_, next_id_prop
6363
image_.filename = "bar.png"
6464
next_id_prop_.return_value = 24
6565
expected_xml = snippet_text("inline")
66-
story_part = BaseStoryPart(None, None, None, None)
66+
story_part = StoryPart(None, None, None, None)
6767

6868
inline = story_part.new_pic_inline("foo/bar.png", width=100, height=200)
6969

@@ -73,15 +73,15 @@ def it_can_create_a_new_pic_inline(self, get_or_add_image_, image_, next_id_prop
7373

7474
def it_knows_the_next_available_xml_id(self, next_id_fixture):
7575
story_element, expected_value = next_id_fixture
76-
story_part = BaseStoryPart(None, None, story_element, None)
76+
story_part = StoryPart(None, None, story_element, None)
7777

7878
next_id = story_part.next_id
7979

8080
assert next_id == expected_value
8181

8282
def it_knows_the_main_document_part_to_help(self, package_, document_part_):
8383
package_.main_document_part = document_part_
84-
story_part = BaseStoryPart(None, None, None, package_)
84+
story_part = StoryPart(None, None, None, package_)
8585

8686
document_part = story_part._document_part
8787

@@ -115,11 +115,11 @@ def document_part_(self, request):
115115

116116
@pytest.fixture
117117
def _document_part_prop_(self, request):
118-
return property_mock(request, BaseStoryPart, "_document_part")
118+
return property_mock(request, StoryPart, "_document_part")
119119

120120
@pytest.fixture
121121
def get_or_add_image_(self, request):
122-
return method_mock(request, BaseStoryPart, "get_or_add_image")
122+
return method_mock(request, StoryPart, "get_or_add_image")
123123

124124
@pytest.fixture
125125
def image_(self, request):
@@ -131,15 +131,15 @@ def image_part_(self, request):
131131

132132
@pytest.fixture
133133
def next_id_prop_(self, request):
134-
return property_mock(request, BaseStoryPart, "next_id")
134+
return property_mock(request, StoryPart, "next_id")
135135

136136
@pytest.fixture
137137
def package_(self, request):
138138
return instance_mock(request, Package)
139139

140140
@pytest.fixture
141141
def relate_to_(self, request):
142-
return method_mock(request, BaseStoryPart, "relate_to")
142+
return method_mock(request, StoryPart, "relate_to")
143143

144144
@pytest.fixture
145145
def style_(self, request):

tests/styles/test_style.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from docx.enum.style import WD_STYLE_TYPE
66
from docx.styles.style import (
77
BaseStyle,
8+
CharacterStyle,
9+
ParagraphStyle,
810
StyleFactory,
9-
_CharacterStyle,
1011
_NumberingStyle,
11-
_ParagraphStyle,
1212
_TableStyle,
1313
)
1414
from docx.text.font import Font
@@ -32,18 +32,18 @@ def factory_fixture(
3232
self,
3333
request,
3434
paragraph_style_,
35-
_ParagraphStyle_,
35+
ParagraphStyle_,
3636
character_style_,
37-
_CharacterStyle_,
37+
CharacterStyle_,
3838
table_style_,
3939
_TableStyle_,
4040
numbering_style_,
4141
_NumberingStyle_,
4242
):
4343
type_attr_val = request.param
4444
StyleCls_, style_mock = {
45-
"paragraph": (_ParagraphStyle_, paragraph_style_),
46-
"character": (_CharacterStyle_, character_style_),
45+
"paragraph": (ParagraphStyle_, paragraph_style_),
46+
"character": (CharacterStyle_, character_style_),
4747
"table": (_TableStyle_, table_style_),
4848
"numbering": (_NumberingStyle_, numbering_style_),
4949
}[request.param]
@@ -54,24 +54,24 @@ def factory_fixture(
5454
# fixture components -----------------------------------
5555

5656
@pytest.fixture
57-
def _ParagraphStyle_(self, request, paragraph_style_):
57+
def ParagraphStyle_(self, request, paragraph_style_):
5858
return class_mock(
59-
request, "docx.styles.style._ParagraphStyle", return_value=paragraph_style_
59+
request, "docx.styles.style.ParagraphStyle", return_value=paragraph_style_
6060
)
6161

6262
@pytest.fixture
6363
def paragraph_style_(self, request):
64-
return instance_mock(request, _ParagraphStyle)
64+
return instance_mock(request, ParagraphStyle)
6565

6666
@pytest.fixture
67-
def _CharacterStyle_(self, request, character_style_):
67+
def CharacterStyle_(self, request, character_style_):
6868
return class_mock(
69-
request, "docx.styles.style._CharacterStyle", return_value=character_style_
69+
request, "docx.styles.style.CharacterStyle", return_value=character_style_
7070
)
7171

7272
@pytest.fixture
7373
def character_style_(self, request):
74-
return instance_mock(request, _CharacterStyle)
74+
return instance_mock(request, CharacterStyle)
7575

7676
@pytest.fixture
7777
def _TableStyle_(self, request, table_style_):
@@ -396,7 +396,7 @@ def unhide_set_fixture(self, request):
396396
return style, value, expected_xml
397397

398398

399-
class Describe_CharacterStyle(object):
399+
class DescribeCharacterStyle(object):
400400
def it_knows_which_style_it_is_based_on(self, base_get_fixture):
401401
style, StyleFactory_, StyleFactory_calls, base_style_ = base_get_fixture
402402
base_style = style.base_style
@@ -427,7 +427,7 @@ def it_provides_access_to_its_font(self, font_fixture):
427427
def base_get_fixture(self, request, StyleFactory_):
428428
styles_cxml, style_idx, base_style_idx = request.param
429429
styles = element(styles_cxml)
430-
style = _CharacterStyle(styles[style_idx])
430+
style = CharacterStyle(styles[style_idx])
431431
if base_style_idx >= 0:
432432
base_style = styles[base_style_idx]
433433
StyleFactory_calls = [call(base_style)]
@@ -446,15 +446,15 @@ def base_get_fixture(self, request, StyleFactory_):
446446
)
447447
def base_set_fixture(self, request, style_):
448448
style_cxml, base_style_id, expected_style_cxml = request.param
449-
style = _CharacterStyle(element(style_cxml))
449+
style = CharacterStyle(element(style_cxml))
450450
style_.style_id = base_style_id
451451
base_style = style_ if base_style_id is not None else None
452452
expected_xml = xml(expected_style_cxml)
453453
return style, base_style, expected_xml
454454

455455
@pytest.fixture
456456
def font_fixture(self, Font_, font_):
457-
style = _CharacterStyle(element("w:style"))
457+
style = CharacterStyle(element("w:style"))
458458
return style, Font_, font_
459459

460460
# fixture components ---------------------------------------------
@@ -476,7 +476,7 @@ def StyleFactory_(self, request):
476476
return function_mock(request, "docx.styles.style.StyleFactory")
477477

478478

479-
class Describe_ParagraphStyle(object):
479+
class DescribeParagraphStyle(object):
480480
def it_knows_its_next_paragraph_style(self, next_get_fixture):
481481
style, expected_value = next_get_fixture
482482
assert style.next_paragraph_style == expected_value
@@ -515,8 +515,8 @@ def next_get_fixture(self, request):
515515
style_names = ["H1", "H2", "Body", "Foo", "Char"]
516516
style_elm = styles[style_names.index(style_name)]
517517
next_style_elm = styles[style_names.index(next_style_name)]
518-
style = _ParagraphStyle(style_elm)
519-
next_style = _ParagraphStyle(next_style_elm) if style_name == "H1" else style
518+
style = ParagraphStyle(style_elm)
519+
next_style = ParagraphStyle(next_style_elm) if style_name == "H1" else style
520520
return style, next_style
521521

522522
@pytest.fixture(
@@ -534,18 +534,18 @@ def next_set_fixture(self, request):
534534
"w:style{w:type=paragraph,w:styleId=B})"
535535
)
536536
style_elms = {"H": styles[0], "B": styles[1]}
537-
style = _ParagraphStyle(style_elms[style_name])
537+
style = ParagraphStyle(style_elms[style_name])
538538
next_style = (
539539
None
540540
if next_style_name is None
541-
else _ParagraphStyle(style_elms[next_style_name])
541+
else ParagraphStyle(style_elms[next_style_name])
542542
)
543543
expected_xml = xml(style_cxml)
544544
return style, next_style, expected_xml
545545

546546
@pytest.fixture
547547
def parfmt_fixture(self, ParagraphFormat_, paragraph_format_):
548-
style = _ParagraphStyle(element("w:style"))
548+
style = ParagraphStyle(element("w:style"))
549549
return style, ParagraphFormat_, paragraph_format_
550550

551551
# fixture components ---------------------------------------------

0 commit comments

Comments
 (0)
0