Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit ab8e510

Browse files
authored
Merge pull request #39 from climatepolicyradar/feature/generic-types-for-parser-output
Add bound generic typing for parser outputs
2 parents 76c5a2c + 0767748 commit ab8e510

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/cpr_data_access/parser_models.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging.config
33
from datetime import date
44
from enum import Enum
5-
from typing import Optional, Sequence, Tuple, List, Union, Mapping, Any
5+
from typing import Any, List, Mapping, Optional, Sequence, Tuple, TypeVar, Union
66
from collections import Counter
77

88
from deprecation import deprecated
@@ -164,6 +164,9 @@ class PDFData(BaseModel):
164164
text_blocks: Sequence[PDFTextBlock]
165165

166166

167+
_PO = TypeVar("_PO", bound="BaseParserOutput")
168+
169+
167170
class BaseParserOutput(BaseModel):
168171
"""Base class for an output to a parser."""
169172

@@ -247,7 +250,7 @@ def to_string(self) -> str: # type: ignore
247250
[text_block.to_string().strip() for text_block in self.text_blocks]
248251
)
249252

250-
def detect_and_set_languages(self) -> "BaseParserOutput":
253+
def detect_and_set_languages(self: _PO) -> _PO:
251254
"""
252255
Detect language of the text and set the language attribute.
253256
@@ -281,8 +284,8 @@ def detect_and_set_languages(self) -> "BaseParserOutput":
281284
return self
282285

283286
def set_document_languages_from_text_blocks(
284-
self, min_language_proportion: float = 0.4
285-
):
287+
self: _PO, min_language_proportion: float = 0.4
288+
) -> _PO:
286289
"""
287290
Store the document languages attribute as part of the object.
288291
@@ -311,7 +314,7 @@ def set_document_languages_from_text_blocks(
311314

312315
return self
313316

314-
def vertically_flip_text_block_coords(self) -> "BaseParserOutput":
317+
def vertically_flip_text_block_coords(self: _PO) -> _PO:
315318
"""
316319
Flips the coordinates of all PDF text blocks vertically.
317320

0 commit comments

Comments
 (0)