8000 rfctr: resolve some import cycles · python-openxml/python-docx@0a09474 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a09474

Browse files
committed
rfctr: resolve some import cycles
1 parent 5a80006 commit 0a09474

File tree

9 files changed

+25
-33
lines changed

9 files changed

+25
-33
lines changed

src/docx/blkcntnr.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from docx.text.paragraph import Paragraph
1919

2020
if TYPE_CHECKING:
21-
from docx import types as t
21+
import docx.types as t
2222
from docx.oxml.document import CT_Body
2323
from docx.oxml.section import CT_HdrFtr
2424
from docx.oxml.table import CT_Tc
@@ -41,9 +41,7 @@ def __init__(self, element: BlockItemElement, parent: t.ProvidesStoryPart):
4141
super(BlockItemContainer, self).__init__(parent)
4242
self._element = element
4343

44-
def add_paragraph(
45-
self, text: str = "", style: str | ParagraphStyle | None = None
46-
) -> Paragraph:
44+
def add_paragraph(self, text: str = "", style: str | ParagraphStyle | None = None) -> Paragraph:
4745
"""Return paragraph newly added to the end of the content in this container.
4846
4947
The paragraph has `text` in a single run if present, and is given paragraph
@@ -77,11 +75,7 @@ def iter_inner_content(self) -> Iterator[Paragraph | Table]:
7775
from docx.table import Table
7876

7977
for element in self._element.inner_content_elements:
80-
yield (
81-
Paragraph(element, self)
82-
if isinstance(element, CT_P)
83-
else Table(element, self)
84-
)
78+
yield (Paragraph(element, self) if isinstance(element, CT_P) else Table(element, self))
8579

8680
@property
8781
def paragraphs(self):

src/docx/document.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from docx.shared import ElementProxy, Emu
1515

1616
if TYPE_CHECKING:
17-
from docx import types as t
17+
import docx.types as t
1818
from docx.oxml.document import CT_Body, CT_Document
1919
from docx.parts.document import DocumentPart
2020
from docx.settings import Settings
@@ -56,9 +56,7 @@ def add_page_break(self):
5656
paragraph.add_run().add_break(WD_BREAK.PAGE)
5757
return paragraph
5858

59-
def add_paragraph(
60-
self, text: str = "", style: str | ParagraphStyle | None = None
61-
) -> Paragraph:
59+
def add_paragraph(self, text: str = "", style: str | ParagraphStyle | None = None) -> Paragraph:
6260
"""Return paragraph newly added to the end of the document.
6361
6462
The paragraph is populated with `text` and having paragraph style `style`.

src/docx/drawing/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
from __future__ import annotations
44

5-
from docx import types as t
5+
from typing import TYPE_CHECKING
6+
67
from docx.oxml.drawing import CT_Drawing
78
from docx.shared import Parented
89

10+
if TYPE_CHECKING:
11+
import docx.types as t
12+
913

1014
class Drawing(Parented):
1115
"""Container for a DrawingML object."""

src/docx/shared.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)
1717

1818
if TYPE_CHECKING:
19-
from docx import types as t
19+
import docx.types as t
2020
from docx.opc.part import XmlPart
2121
from docx.oxml.xmlchemy import BaseOxmlElement
2222
from docx.parts.story import StoryPart
@@ -284,9 +284,7 @@ class ElementProxy:
284284
common type of class in python-docx other than custom element (oxml) classes.
285285
"""
286286

287-
def __init__(
288-
self, element: BaseOxmlElement, parent: t.ProvidesXmlPart | None = None
289-
):
287+
def __init__(self, element: BaseOxmlElement, parent: t.ProvidesXmlPart | None = None):
290288
self._element = element
291289
self._parent = parent
292290

src/docx/table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from typing_extensions import TypeAlias
88

9-
from docx import types as t
109
from docx.blkcntnr import BlockItemContainer
1110
from docx.enum.style import WD_STYLE_TYPE
1211
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT
@@ -15,6 +14,7 @@
1514
from docx.shared import Inches, Parented, StoryChild, lazyproperty
1615

1716
if TYPE_CHECKING:
17+
import docx.types as t
1818
from docx.enum.table import WD_ROW_HEIGHT_RULE, WD_TABLE_ALIGNMENT, WD_TABLE_DIRECTION
1919
from docx.oxml.table import CT_Row, CT_Tbl, CT_TblPr, CT_Tc
2020
from docx.shared import Length
@@ -193,7 +193,7 @@ class _Cell(BlockItemContainer):
193193
"""Table cell."""
194194

195195
def __init__(self, tc: CT_Tc, parent: TableParent):
196-
super(_Cell, self).__init__(tc, cast(t.ProvidesStoryPart, parent))
196+
super(_Cell, self).__init__(tc, cast("t.ProvidesStoryPart", parent))
197197
self._parent = parent
198198
self._tc = self._element = tc
199199

src/docx/text/hyperlink.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
from __future__ import annotations
99

10-
from typing import List
10+
from typing import TYPE_CHECKING
1111

12-
from docx import types as t
13-
from docx.oxml.text.hyperlink import CT_Hyperlink
1412
from docx.shared import Parented
1513
from docx.text.run import Run
1614

15+
if TYPE_CHECKING:
16+
import docx.types as t
17+
from docx.oxml.text.hyperlink import CT_Hyperlink
18+
1719

1820
class Hyperlink(Parented):
1921
"""Proxy object wrapping a `<w:hyperlink>` element.
@@ -78,7 +80,7 @@ def fragment(self) -> str:
7880
return self._hyperlink.anchor or ""
7981

8082
@property
81-
def runs(self) -> List[Run]:
83+
def runs(self) -> list[Run]:
8284
"""List of |Run| instances in this hyperlink.
8385
8486
Together these define the visible text of the hyperlink. The text of a hyperlink

src/docx/text/pagebreak.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
from typing import TYPE_CHECKING
66

7-
from docx import types as t
87
from docx.oxml.text.pagebreak import CT_LastRenderedPageBreak
98
from docx.shared import Parented
109

1110
if TYPE_CHECKING:
11+
import docx.types as t
1212
from docx.text.paragraph import Paragraph
1313

1414

src/docx/text/paragraph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from typing import TYPE_CHECKING, Iterator, List, cast
66

7-
from docx import types as t
87
from docx.enum.style import WD_STYLE_TYPE
98
from docx.oxml.text.run import CT_R
109
from docx.shared import StoryChild
@@ -15,6 +14,7 @@
1514
from docx.text.run import Run
1615

1716
if TYPE_CHECKING:
17+
import docx.types as t
1818
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
1919
from docx.oxml.text.paragraph import CT_P
2020
from docx.styles.style import CharacterStyle

src/docx/text/run.py

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

55
from typing import IO, TYPE_CHECKING, Iterator, cast
66

7-
from docx import types as t
87
from docx.drawing import Drawing
98
from docx.enum.style import WD_STYLE_TYPE
109
from docx.enum.text import WD_BREAK
@@ -17,6 +16,7 @@
1716
from docx.text.pagebreak import RenderedPageBreak
1817

1918
if TYPE_CHECKING:
19+
import docx.types as t
2020
from docx.enum.text import WD_UNDERLINE
2121
from docx.oxml.text.run import CT_R, CT_Text
2222
from docx.shared import Length
@@ -170,9 +170,7 @@ def iter_inner_content(self) -> Iterator[str | Drawing | RenderedPageBreak]:
170170
yield item
171171
elif isinstance(item, CT_LastRenderedPageBreak):
172172
yield RenderedPageBreak(item, self)
173-
elif isinstance( # pyright: ignore[reportUnnecessaryIsInstance]
174-
item, CT_Drawing
175-
):
173+
elif isinstance(item, CT_Drawing): # pyright: ignore[reportUnnecessaryIsInstance]
176174
yield Drawing(item, self)
177175

178176
@property
@@ -185,9 +183,7 @@ def style(self) -> CharacterStyle:
185183
property to |None| removes any directly-applied character style.
186184
"""
187185
style_id = self._r.style
188-
return cast(
189-
CharacterStyle, self.part.get_style(style_id, WD_STYLE_TYPE.CHARACTER)
190-
)
186+
return cast(CharacterStyle, self.part.get_style(style_id, WD_STYLE_TYPE.CHARACTER))
191187

192188
@style.setter
193189
def style(self, style_or_name: str | CharacterStyle | None):

0 commit comments

Comments
 (0)
0