Skip to content

Commit 94802e4

Browse files
committed
fix: fix some shortlist issues
1 parent 89b399b commit 94802e4

File tree

6 files changed

+30
-51
lines changed

6 files changed

+30
-51
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ classifiers = [
2323
]
2424
dependencies = [
2525
"lxml>=3.1.0",
26-
"typing_extensions",
26+
"typing_extensions>=4.9.0",
2727
]
2828
description = "Create, read, and update Microsoft Word .docx files."
2929
dynamic = ["version"]

src/docx/enum/base.py

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

55
import enum
66
import textwrap
7-
from typing import Any, Dict, Type, TypeVar
7+
from typing import TYPE_CHECKING, Any, Dict, Type, TypeVar
88

9-
from typing_extensions import Self
9+
if TYPE_CHECKING:
10+
from typing_extensions import Self
1011

1112
_T = TypeVar("_T", bound="BaseXmlEnum")
1213

@@ -69,7 +70,7 @@ def to_xml(cls: Type[_T], value: int | _T | None) -> str | None:
6970
"""XML value of this enum member, generally an XML attribute value."""
7071
# -- presence of multi-arg `__new__()` method fools type-checker, but getting a
7172
# -- member by its value using EnumCls(val) works as usual.
72-
return cls(value).xml_value # pyright: ignore[reportGeneralTypeIssues]
73+
return cls(value).xml_value
7374

7475

7576
class DocsPageFormatter:
@@ -129,9 +130,7 @@ def _member_defs(self):
129130
"""A single string containing the aggregated member definitions section of the
130131
documentation page."""
131132
members = self._clsdict["__members__"]
132-
member_defs = [
133-
self._member_def(member) for member in members if member.name is not None
134-
]
133+
member_defs = [self._member_def(member) for member in members if member.name is not None]
135134
return "\n".join(member_defs)
136135

137136
@property

src/docx/image/image.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import os
1212
from typing import IO, Tuple
1313

14-
from typing_extensions import Self
15-
1614
from docx.image.exceptions import UnrecognizedImageError
1715
from docx.shared import Emu, Inches, Length, lazyproperty
1816

@@ -28,14 +26,14 @@ def __init__(self, blob: bytes, filename: str, image_header: BaseImageHeader):
2826
self._image_header = image_header
2927

3028
@classmethod
31-
def from_blob(cls, blob: bytes) -> Self:
29+
def from_blob(cls, blob: bytes) -> Image:
3230
"""Return a new |Image| subclass instance parsed from the image binary contained
3331
in `blob`."""
3432
stream = io.BytesIO(blob)
3533
return cls._from_stream(stream, blob)
3634

3735
@classmethod
38-
def from_file(cls, image_descriptor):
36+
def from_file(cls, image_descriptor: str | IO[bytes]):
3937
"""Return a new |Image| subclass instance loaded from the image file identified
4038
by `image_descriptor`, a path or file-like object."""
4139
if isinstance(image_descriptor, str):
@@ -57,7 +55,7 @@ def blob(self):
5755
return self._blob
5856

5957
@property
60-
def content_type(self):
58+
def content_type(self) -> str:
6159
"""MIME content type for this image, e.g. ``'image/jpeg'`` for a JPEG image."""
6260
return self._image_header.content_type
6361

@@ -167,12 +165,11 @@ def _from_stream(
167165
return cls(blob, filename, image_header)
168166

169167

170-
def _ImageHeaderFactory(stream):
171-
"""Return a |BaseImageHeader| subclass instance that knows how to parse the headers
172-
of the image in `stream`."""
168+
def _ImageHeaderFactory(stream: IO[bytes]):
169+
"""A |BaseImageHeader| subclass instance that can parse headers of image in `stream`."""
173170
from docx.image import SIGNATURES
174171

175-
def read_32(stream):
172+
def read_32(stream: IO[bytes]):
176173
stream.seek(0)
177174
return stream.read(32)
178175

@@ -188,32 +185,27 @@ def read_32(stream):
188185
class BaseImageHeader:
189186
"""Base class for image header subclasses like |Jpeg| and |Tiff|."""
190187

191-
def __init__(self, px_width, px_height, horz_dpi, vert_dpi):
188+
def __init__(self, px_width: int, px_height: int, horz_dpi: int, vert_dpi: int):
192189
self._px_width = px_width
193190
self._px_height = px_height
194191
self._horz_dpi = horz_dpi
195192
self._vert_dpi = vert_dpi
196193

197194
@property
198-
def content_type(self):
195+
def content_type(self) -> str:
199196
"""Abstract property definition, must be implemented by all subclasses."""
200-
msg = (
201-
"content_type property must be implemented by all subclasses of "
202-
"BaseImageHeader"
203-
)
197+
msg = "content_type property must be implemented by all subclasses of " "BaseImageHeader"
204198
raise NotImplementedError(msg)
205199

206200
@property
207-
def default_ext(self):
201+
def default_ext(self) -> str:
208202
"""Default filename extension for images of this type.
209203
210204
An abstract property definition, must be implemented by all subclasses.
211205
"""
212-
msg = (
213-
"default_ext property must be implemented by all subclasses of "
214-
"BaseImageHeader"
206+
raise NotImplementedError(
207+
"default_ext property must be implemented by all subclasses of " "BaseImageHeader"
215208
)
216-
raise NotImplementedError(msg)
217209

218210
@property
219211
def px_width(self):

src/docx/oxml/ns.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""Namespace-related objects."""
22

3-
from typing import Any, Dict
3+
from __future__ import annotations
44

5-
from typing_extensions import Self
5+
from typing import Any, Dict
66

77
nsmap = {
88
"a": "http://schemas.openxmlformats.org/drawingml/2006/main",
@@ -41,7 +41,7 @@ def clark_name(self) -> str:
4141
return "{%s}%s" % (self._ns_uri, self._local_part)
4242

4343
@classmethod
44-
def from_clark_name(cls, clark_name: str) -> Self:
44+
def from_clark_name(cls, clark_name: str) -> NamespacePrefixedTag:
4545
nsuri, local_name = clark_name[1:].split("}")
4646
nstag = "%s:%s" % (pfxmap[nsuri], local_name)
4747
return cls(nstag)

src/docx/oxml/simpletypes.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,18 @@ def convert_from_xml(cls, str_value: str) -> Any:
3636
return int(str_value)
3737

3838
@classmethod
39-
def convert_to_xml(cls, value: Any) -> str:
40-
...
39+
def convert_to_xml(cls, value: Any) -> str: ...
4140

4241
@classmethod
43-
def validate(cls, value: Any) -> None:
44-
...
42+
def validate(cls, value: Any) -> None: ...
4543

4644
@classmethod
4745
def validate_int(cls, value: object):
4846
if not isinstance(value, int):
4947
raise TypeError("value must be <type 'int'>, got %s" % type(value))
5048

5149
@classmethod
52-
def validate_int_in_range(
53-
cls, value: int, min_inclusive: int, max_inclusive: int
54-
) -> None:
50+
def validate_int_in_range(cls, value: int, min_inclusive: int, max_inclusive: int) -> None:
5551
cls.validate_int(value)
5652
if value < min_inclusive or value > max_inclusive:
5753
raise ValueError(
@@ -129,8 +125,7 @@ def convert_to_xml(cls, value: bool) -> str:
129125
def validate(cls, value: Any) -> None:
130126
if value not in (True, False):
131127
raise TypeError(
132-
"only True or False (and possibly None) may be assigned, got"
133-
" '%s'" % value
128+
"only True or False (and possibly None) may be assigned, got" " '%s'" % value
134129
)
135130

136131

@@ -248,8 +243,7 @@ def validate(cls, value: Any) -> None:
248243
# must be an RGBColor object ---
249244
if not isinstance(value, RGBColor):
250245
raise ValueError(
251-
"rgb color value must be RGBColor object, got %s %s"
252-
% (type(value), value)
246+
"rgb color value must be RGBColor object, got %s %s" % (type(value), value)
253247
)
254248

255249

@@ -316,7 +310,7 @@ class ST_SignedTwipsMeasure(XsdInt):
316310
def convert_from_xml(cls, str_value: str) -> Length:
317311
if "i" in str_value or "m" in str_value or "p" in str_value:
318312
return ST_UniversalMeasure.convert_from_xml(str_value)
319-
return Twips(int(str_value))
313+
return Twips(int(round(float(str_value))))
320314

321315
@classmethod
322316
def convert_to_xml(cls, value: int | Length) -> str:

src/docx/text/paragraph.py

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

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

7-
from typing_extensions import Self
8-
97
from docx import types as t
108
from docx.enum.style import WD_STYLE_TYPE
119
from docx.oxml.text.run import CT_R
@@ -29,9 +27,7 @@ def __init__(self, p: CT_P, parent: t.ProvidesStoryPart):
2927
super(Paragraph, self).__init__(parent)
3028
self._p = self._element = p
3129

32-
def add_run(
33-
self, text: str | None = None, style: str | CharacterStyle | None = None
34-
) -> Run:
30+
def add_run(self, text: str | None = None, style: str | CharacterStyle | None = None) -> Run:
3531
"""Append run containing `text` and having character-style `style`.
3632
3733
`text` can contain tab (``\\t``) characters, which are converted to the
@@ -82,7 +78,7 @@ def hyperlinks(self) -> List[Hyperlink]:
8278

8379
def insert_paragraph_before(
8480
self, text: str | None = None, style: str | ParagraphStyle | None = None
85-
) -> Self:
81+
) -> Paragraph:
8682
"""Return a newly created paragraph, inserted directly before this paragraph.
8783
8884
If `text` is supplied, the new paragraph contains that text in a single run. If
@@ -123,9 +119,7 @@ def rendered_page_breaks(self) -> List[RenderedPageBreak]:
123119
Most often an empty list, sometimes contains one page-break, but can contain
124120
more than one is rare or contrived cases.
125121
"""
126-
return [
127-
RenderedPageBreak(lrpb, self) for lrpb in self._p.lastRenderedPageBreaks
128-
]
122+
return [RenderedPageBreak(lrpb, self) for lrpb in self._p.lastRenderedPageBreaks]
129123

130124
@property
131125
def runs(self) -> List[Run]:

0 commit comments

Comments
 (0)