11"""Open Packaging Convention (OPC) objects related to package parts."""
22
3+ from __future__ import annotations
4+
5+ from typing import TYPE_CHECKING
6+
37from docx .opc .oxml import serialize_part_xml
48from docx .opc .packuri import PackURI
59from docx .opc .rel import Relationships
610from docx .opc .shared import cls_method_fn , lazyproperty
711from docx .oxml .parser import parse_xml
812
13+ if TYPE_CHECKING :
14+ from docx .opc .package import OpcPackage
15+
916
1017class Part (object ):
1118 """Base class for package parts.
@@ -14,7 +21,13 @@ class Part(object):
1421 to implement specific part behaviors.
1522 """
1623
17- def __init__ (self , partname , content_type , blob = None , package = None ):
24+ def __init__ (
25+ self ,
26+ partname : str ,
27+ content_type : str ,
28+ blob : bytes | None = None ,
29+ package : OpcPackage | None = None ,
30+ ):
1831 super (Part , self ).__init__ ()
1932 self ._partname = partname
2033 self ._content_type = content_type
@@ -96,7 +109,7 @@ def partname(self, partname):
96109 raise TypeError (tmpl % type (partname ).__name__ )
97110 self ._partname = partname
98111
99- def part_related_by (self , reltype ) :
112+ def part_related_by (self , reltype : str ) -> Part :
100113 """Return part to which this part has a relationship of `reltype`.
101114
102115 Raises |KeyError| if no such relationship is found and |ValueError| if more than
@@ -105,9 +118,12 @@ def part_related_by(self, reltype):
105118 """
106119 return self .rels .part_with_reltype (reltype )
107120
108- def relate_to (self , target , reltype , is_external = False ):
109- """Return rId key of relationship of `reltype` to `target`, from an existing
110- relationship if there is one, otherwise a newly created one."""
121+ def relate_to (self , target : Part , reltype : str , is_external : bool = False ) -> str :
122+ """Return rId key of relationship of `reltype` to `target`.
123+
124+ The returned `rId` is from an existing relationship if there is one, otherwise a
125+ new relationship is created.
126+ """
111127 if is_external :
112128 return self .rels .get_or_add_ext_rel (reltype , target )
113129 else :
0 commit comments