8000 rfctr: improve typing · python-openxml/python-docx@b56f516 · GitHub
[go: up one dir, main page]

Skip to content

Commit b56f516

Browse files
committed
rfctr: improve typing
1 parent 96f80f7 commit b56f516

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/docx/__init__.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
from docx.api import Document # noqa
1+
"""Initialize `docx` package.
2+
3+
Export the `Document` constructor function and establish the mapping of part-type to
4+
the part-classe that implements that type.
5+
"""
6+
7+
from __future__ import annotations
8+
9+
from typing import TYPE_CHECKING, Type
10+
11+
from docx.api import Document
12+
13+
if TYPE_CHECKING:
14+
from docx.opc.part import Part
215

316
__version__ = "1.0.0"
417

518

6-
# register custom Part classes with opc package reader
19+
__all__ = ["Document"]
20+
21+
22+
# -- register custom Part classes with opc package reader --
723

824
from docx.opc.constants import CONTENT_TYPE as CT
925
from docx.opc.constants import RELATIONSHIP_TYPE as RT
@@ -17,7 +33,7 @@
1733
from docx.parts.styles import StylesPart
1834

1935

20-
def part_class_selector(content_type, reltype):
36+
def part_class_selector(content_type: str, reltype: str) -> Type[Part] | None:
2137
if reltype == RT.IMAGE:
2238
return ImagePart
2339
return None

src/docx/opc/part.py

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

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING
5+
from typing import TYPE_CHECKING, Callable, Dict, Type
66

77
from docx.opc.oxml import serialize_part_xml
88
from docx.opc.packuri import PackURI
@@ -78,7 +78,7 @@ def drop_rel(self, rId: str):
7878
del self.rels[rId]
7979

8080
@classmethod
81-
def load(cls, partname, content_type, blob, package):
81+
def load(cls, partname: str, content_type: str, blob: bytes, package: Package):
8282
return cls(partname, content_type, blob, package)
8383

8484
def load_rel(self, reltype, target, rId, is_external=False):
@@ -167,12 +167,19 @@ class PartFactory:
167167
the part, which is by default ``opc.package.Part``.
168168
"""
169169

170-
part_class_selector = None
171-
part_type_for = {}
170+
part_class_selector: Callable[[str, str], Type[Part] | None] | None
171+
part_type_for: Dict[str, Type[Part]] = {}
172172
default_part_type = Part
173173

174-
def __new__(cls, partname, content_type, reltype, blob, package):
175-
PartClass = None
174+
def __new__(
175+
cls,
176+
partname: str,
177+
content_type: str,
178+
reltype: str,
179+
blob: bytes,
180+
package: Package,
181+
):
182+
PartClass: Type[Part] | None = None
176183
if cls.part_class_selector is not None:
177184
part_class_selector = cls_method_fn(cls, "part_class_selector")
178185
PartClass = part_class_selector(content_type, reltype)
@@ -181,7 +188,7 @@ def __new__(cls, partname, content_type, reltype, blob, package):
181188
return PartClass.load(partname, content_type, blob, package)
182189

183190
@classmethod
184-
def _part_cls_for(cls, content_type):
191+
def _part_cls_for(cls, content_type: str):
185192
"""Return the custom part class registered for `content_type`, or the default
186193
part class if no custom class is registered for `content_type`."""
187194
if content_type in cls.part_type_for:

0 commit comments

Comments
 (0)
0