Skip to content

Commit c015d35

Browse files
authored
🏷️Add py.typed and some more type hints (#514)
1 parent ddf79f6 commit c015d35

5 files changed

Lines changed: 29 additions & 26 deletions

File tree

bibtexparser/middlewares/names.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import List
1010
from typing import Literal
1111
from typing import Tuple
12+
from typing import Union
1213

1314
from bibtexparser.model import Block
1415
from bibtexparser.model import Entry
@@ -73,7 +74,7 @@ def metadata_key(cls) -> str:
7374
return "separate_coauthors"
7475

7576
# docstr-coverage: inherited
76-
def _transform_field_value(self, name) -> List[str]:
77+
def _transform_field_value(self, name: str) -> List[str]:
7778
return split_multiple_persons_names(name)
7879

7980

@@ -86,7 +87,7 @@ def metadata_key(cls) -> str:
8687
return "merge_coauthors"
8788

8889
# docstr-coverage: inherited
89-
def _transform_field_value(self, name):
90+
def _transform_field_value(self, name: Union[List[str], str]) -> str:
9091
if isinstance(name, list):
9192
return " and ".join(name)
9293
return name
@@ -160,7 +161,7 @@ class SplitNameParts(_NameTransformerMiddleware):
160161
def metadata_key(cls) -> str:
161162
return "split_name_parts"
162163

163-
def _transform_field_value(self, name) -> List[NameParts]:
164+
def _transform_field_value(self, name: List[str]) -> List[NameParts]:
164165
if not isinstance(name, list):
165166
raise ValueError(
166167
"Expected a list of strings, got {}. "
@@ -195,7 +196,7 @@ def __init__(
195196
def metadata_key(cls) -> str:
196197
return "merge_name_parts"
197198

198-
def _transform_field_value(self, name) -> List[str]:
199+
def _transform_field_value(self, name: List[NameParts]) -> List[str]:
199200
if not (isinstance(name, list) and all(isinstance(n, NameParts) for n in name)):
200201
raise ValueError(f"Expected a list of NameParts, got {name}. ")
201202

@@ -207,7 +208,7 @@ def _transform_field_value(self, name) -> List[str]:
207208
raise ValueError(f"""Expected "first" or "last" style, got {self.style}. """)
208209

209210

210-
def parse_single_name_into_parts(name, strict=True):
211+
def parse_single_name_into_parts(name: str, strict: bool = True) -> NameParts:
211212
"""
212213
Parse a name into its constituent parts: First, von, Last, and Jr.
213214
@@ -502,7 +503,7 @@ def rindex(k, x, default):
502503
return parts
503504

504505

505-
def split_multiple_persons_names(names):
506+
def split_multiple_persons_names(names: str) -> List[str]:
506507
"""
507508
Splits a string of multiple names.
508509

bibtexparser/model.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import List
55
from typing import Optional
66
from typing import Set
7+
from typing import Tuple
78

89

910
class Block(abc.ABC):
@@ -67,7 +68,7 @@ def set_parser_metadata(self, key: str, value: Any):
6768
See attribute ``parser_metadata`` for more information."""
6869
self._parser_metadata[key] = value
6970

70-
def __eq__(self, other):
71+
def __eq__(self, other: object) -> bool:
7172
# make sure they have the same type and same content
7273
return (
7374
isinstance(other, self.__class__)
@@ -108,10 +109,10 @@ def value(self) -> str:
108109
def value(self, value: str):
109110
self._value = value
110111

111-
def __str__(self):
112+
def __str__(self) -> str:
112113
return f"String (line: {self.start_line}, key: `{self.key}`): `{self.value}`"
113114

114-
def __repr__(self):
115+
def __repr__(self) -> str:
115116
return (
116117
f"String(key=`{self.key}`, value=`{self.value}`, "
117118
f"start_line={self.start_line}, raw=`{self.raw}`)"
@@ -134,10 +135,10 @@ def value(self) -> str:
134135
def value(self, value: str):
135136
self._value = value
136137

137-
def __str__(self):
138+
def __str__(self) -> str:
138139
return f"Preamble (line: {self.start_line}): `{self.value}`"
139140

140-
def __repr__(self):
141+
def __repr__(self) -> str:
141142
return f"Preamble(value=`{self.value}`, " f"start_line={self.start_line}, raw=`{self.raw}`)"
142143

143144

@@ -157,10 +158,10 @@ def comment(self) -> str:
157158
def comment(self, value: str):
158159
self._comment = value
159160

160-
def __str__(self):
161+
def __str__(self) -> str:
161162
return f"ExplicitComment (line: {self.start_line}): `{self.comment}`"
162163

163-
def __repr__(self):
164+
def __repr__(self) -> str:
164165
return (
165166
f"ExplicitComment(comment=`{self.comment}`, "
166167
f"start_line={self.start_line}, raw=`{self.raw}`)"
@@ -183,10 +184,10 @@ def comment(self) -> str:
183184
def comment(self, value: str):
184185
self._comment = value
185186

186-
def __str__(self):
187+
def __str__(self) -> str:
187188
return f"ImplicitComment (line: {self.start_line}): `{self.comment}`"
188189

189-
def __repr__(self):
190+
def __repr__(self) -> str:
190191
return (
191192
f"ImplicitComment(comment=`{self.comment}`, "
192193
f"start_line={self.start_line}, raw=`{self.raw}`)"
@@ -224,18 +225,18 @@ def start_line(self) -> int:
224225
"""The line number of the first line of this field in the originally parsed string."""
225226
return self._start_line
226227

227-
def __eq__(self, other):
228+
def __eq__(self, other: object) -> bool:
228229
# make sure they have the same type and same content
229230
return (
230231
isinstance(other, self.__class__)
231232
and isinstance(self, other.__class__)
232233
and self.__dict__ == other.__dict__
233234
)
234235

235-
def __str__(self):
236+
def __str__(self) -> str:
236237
return f"Field (line: {self.start_line}, key: `{self.key}`): `{self.value}`"
237238

238-
def __repr__(self):
239+
def __repr__(self) -> str:
239240
return f"Field(key=`{self.key}`, value=`{self.value}`, " f"start_line={self.start_line})"
240241

241242

@@ -256,7 +257,7 @@ def __init__(
256257
self._fields = fields
257258

258259
@property
259-
def entry_type(self):
260+
def entry_type(self) -> str:
260261
"""The type of the entry, e.g. ``article`` in ``@article{Cesar2013, ...}``."""
261262
return self._entry_type
262263

@@ -265,7 +266,7 @@ def entry_type(self, value: str):
265266
self._entry_type = value
266267

267268
@property
268-
def key(self):
269+
def key(self) -> str:
269270
"""The key of the entry, e.g. ``Cesar2013`` in ``@article{Cesar2013, ...}``."""
270271
return self._key
271272

@@ -343,15 +344,15 @@ def __setitem__(self, key: str, value: Any):
343344
"""
344345
self.set_field(Field(key, value))
345346

346-
def __delitem__(self, key):
347+
def __delitem__(self, key: str) -> None:
347348
"""Dict-mimicking index.
348349
349350
This serves for partial v1.x backwards compatibility,
350351
as well as for a shorthand for `pop`.
351352
"""
352353
self.pop(key)
353354

354-
def items(self):
355+
def items(self) -> List[Tuple[str, Any]]:
355356
"""Dict-mimicking, for partial v1.x backwards compatibility.
356357
357358
For newly written code, it's recommended to use `entry.entry_type`,
@@ -361,12 +362,12 @@ def items(self):
361362
("ID", self.key),
362363
] + [(f.key, f.value) for f in self.fields]
363364

364-
def __str__(self):
365+
def __str__(self) -> str:
365366
lines = [f"Entry (line: {self.start_line}, type: `{self.entry_type}`, key: `{self.key}`):"]
366367
lines.extend([f"\t`{f.key}` = `{f.value}`" for f in self.fields])
367368
return "\n".join(lines)
368369

369-
def __repr__(self):
370+
def __repr__(self) -> str:
370371
return (
371372
f"Entry(entry_type=`{self.entry_type}`, key=`{self.key}`, "
372373
f"fields=`{self.fields.__repr__()}`, start_line={self.start_line})"

bibtexparser/py.typed

Whitespace-only changes.

bibtexparser/splitter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(self, bibstr: str):
4646

4747
self._reset_block_status(current_char_index=0)
4848

49-
def _reset_block_status(self, current_char_index):
49+
def _reset_block_status(self, current_char_index: int) -> None:
5050
self._open_brackets = 0
5151
self._is_quote_open = False
5252
self._expected_next: Optional[List[str]] = None
@@ -131,7 +131,7 @@ def _move_to_closed_bracket(self) -> int:
131131
)
132132

133133
def _move_to_comma_or_closing_curly_bracket(
134-
self, currently_quote_escaped=False, num_open_curls=0
134+
self, currently_quote_escaped: bool = False, num_open_curls: int = 0
135135
) -> int:
136136
"""Index of the end of the field, taking quote-escape into account."""
137137

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def load_readme():
3131
long_description=load_readme(),
3232
python_requires=">=3.9",
3333
packages=setuptools.find_packages(include=["bibtexparser", "bibtexparser.*"]),
34+
package_data={"bibtexparser": ["py.typed"]},
3435
classifiers=[
3536
"Development Status :: 4 - Beta",
3637
"Programming Language :: Python :: 3",

0 commit comments

Comments
 (0)