Skip to content

Commit 792b407

Browse files
ianardeefharper
authored andcommitted
🐛 💥 fix for customer_company_registration being a list of values
1 parent 812dc0a commit 792b407

File tree

11 files changed

+109
-38
lines changed

11 files changed

+109
-38
lines changed

mindee/documents/financial_document.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from mindee.documents.base import Document
44
from mindee.documents.invoice import Invoice
55
from mindee.documents.receipt import Receipt
6-
from mindee.fields import Field
6+
from mindee.fields.base import Field
77
from mindee.fields.locale import Locale
88
from mindee.fields.orientation import Orientation
99
from mindee.http import Endpoint
@@ -26,7 +26,7 @@ def __init__(
2626
2727
:param api_prediction: Raw prediction from HTTP response
2828
:param input_file: Input object
29-
:param page_n: Page number for multi pages pdf input
29+
:param page_n: Page number for multi-page PDF input
3030
"""
3131
self.total_incl = None
3232
self.total_excl = None
@@ -35,14 +35,14 @@ def __init__(
3535
self.due_date = None
3636
self.taxes = []
3737
self.merchant_name = None
38-
self.payment_details = None
39-
self.company_number = None
38+
self.payment_details = []
39+
self.company_number = []
4040
self.vat_number = None
4141
self.total_tax = None
4242
self.time = None
4343
self.supplier_address = None
4444
self.customer_name = None
45-
self.customer_company_registration = None
45+
self.customer_company_registration = []
4646
self.customer_address = None
4747

4848
# need this for building from prediction
@@ -95,13 +95,8 @@ def build_from_api_prediction(self, api_prediction, page_n=0):
9595
self.time = receipt.time
9696
self.total_tax = receipt.total_tax
9797
self.invoice_number = Field({"value": None, "confidence": 0.0})
98-
self.payment_details = []
99-
self.company_number = []
10098
self.supplier_address = Field({"value": None, "confidence": 0.0})
10199
self.customer_name = Field({"value": None, "confidence": 0.0})
102-
self.customer_company_registration = Field(
103-
{"value": None, "confidence": 0.0}
104-
)
105100
self.customer_address = Field({"value": None, "confidence": 0.0})
106101

107102
def __str__(self) -> str:

mindee/documents/invoice.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from typing import List, Optional
22

33
from mindee.documents.base import Document
4-
from mindee.fields import Field
54
from mindee.fields.amount import Amount
5+
from mindee.fields.base import Field, TypedField
66
from mindee.fields.date import Date
77
from mindee.fields.locale import Locale
88
from mindee.fields.orientation import Orientation
@@ -21,10 +21,10 @@ class Invoice(Document):
2121
supplier: Field
2222
supplier_address: Field
2323
customer_name: Field
24-
customer_company_registration: Field
24+
customer_company_registration: List[TypedField] = []
2525
customer_address: Field
2626
payment_details: List[PaymentDetails] = []
27-
company_number: List[Field] = []
27+
company_number: List[TypedField] = []
2828
total_tax: Amount
2929
# orientation is only present on page-level, not document-level
3030
orientation: Optional[Orientation] = None
@@ -62,8 +62,8 @@ def build_from_api_prediction(self, api_prediction: dict, page_n=0):
6262
self.orientation = Orientation(api_prediction["orientation"], page_n=page_n)
6363

6464
self.company_number = [
65-
Field(company_reg, extra_fields={"type"}, page_n=page_n)
66-
for company_reg in api_prediction["company_registration"]
65+
TypedField(field_dict, page_n=page_n)
66+
for field_dict in api_prediction["company_registration"]
6767
]
6868
self.invoice_date = Date(
6969
api_prediction["date"], value_key="value", page_n=page_n
@@ -78,9 +78,10 @@ def build_from_api_prediction(self, api_prediction: dict, page_n=0):
7878
self.supplier = Field(api_prediction["supplier"], page_n=page_n)
7979
self.supplier_address = Field(api_prediction["supplier_address"], page_n=page_n)
8080
self.customer_name = Field(api_prediction["customer"], page_n=page_n)
81-
self.customer_company_registration = Field(
82-
api_prediction["customer_company_registration"], page_n=page_n
83-
)
81+
self.customer_company_registration = [
82+
TypedField(field_dict, page_n=page_n)
83+
for field_dict in api_prediction["customer_company_registration"]
84+
]
8485
self.customer_address = Field(api_prediction["customer_address"], page_n=page_n)
8586

8687
self.taxes = [
@@ -103,6 +104,9 @@ def build_from_api_prediction(self, api_prediction: dict, page_n=0):
103104

104105
def __str__(self) -> str:
105106
company_numbers = "; ".join([str(n.value) for n in self.company_number])
107+
customer_company_registration = "; ".join(
108+
[str(n.value) for n in self.customer_company_registration]
109+
)
106110
payments = ", ".join([str(p) for p in self.payment_details])
107111
taxes = ", ".join(f"{t}" for t in self.taxes)
108112
return (
@@ -116,7 +120,7 @@ def __str__(self) -> str:
116120
f"Supplier name: {self.supplier}\n"
117121
f"Supplier address: {self.supplier_address}\n"
118122
f"Customer name: {self.customer_name}\n"
119-
f"Customer company registration: {self.customer_company_registration}\n"
123+
f"Customer company registration: {customer_company_registration}\n"
120124
f"Customer address: {self.customer_address}\n"
121125
f"Payment details: {payments}\n"
122126
f"Company numbers: {company_numbers}\n"

mindee/documents/passport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import List
33

44
from mindee.documents.base import Document
5-
from mindee.fields import Field
5+
from mindee.fields.base import Field
66
from mindee.fields.date import Date
77

88

mindee/documents/receipt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from typing import List, Optional
22

33
from mindee.documents.base import Document
4-
from mindee.fields import Field
54
from mindee.fields.amount import Amount
5+
from mindee.fields.base import Field
66
from mindee.fields.date import Date
77
from mindee.fields.locale import Locale
88
from mindee.fields.orientation import Orientation

mindee/fields/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
from mindee.fields.base import Field

mindee/fields/base.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@ def __init__(
1111
abstract_prediction: Dict[str, Any],
1212
value_key: str = "value",
1313
reconstructed=False,
14-
extra_fields=None,
15-
page_n=None,
14+
page_n: Optional[int] = None,
1615
):
1716
"""
1817
Base field object.
1918
2019
:param abstract_prediction: Prediction object from HTTP response
2120
:param value_key: Key to use in the abstract_prediction dict
2221
:param reconstructed: Bool for reconstructed object (not extracted in the API)
23-
:param page_n: Page number for multi pages PDF
24-
:param extra_fields: extra field to get from the abstract_prediction and
25-
to set as attribute of the Field
22+
:param page_n: Page number for multi-page PDF
2623
"""
2724
self.page_n = page_n
2825

@@ -44,10 +41,6 @@ def __init__(
4441
except KeyError:
4542
self.bbox = []
4643

47-
if extra_fields:
48-
for field_name in extra_fields:
49-
setattr(self, field_name, abstract_prediction[field_name])
50-
5144
self.reconstructed = reconstructed
5245

5346
@staticmethod
@@ -109,3 +102,22 @@ def __str__(self) -> str:
109102
if self.value:
110103
return f"{self.value}"
111104
return ""
105+
106+
107+
class TypedField(Field):
108+
type: str
109+
110+
def __init__(
111+
self,
112+
abstract_prediction: Dict[str, Any],
113+
value_key: str = "value",
114+
reconstructed=False,
115+
page_n: Optional[int] = None,
116+
):
117+
super().__init__(abstract_prediction, value_key, reconstructed, page_n)
118+
self.type = abstract_prediction["type"]
119+
120+
def __str__(self) -> str:
121+
if self.value:
122+
return f"{self.type}: {self.value}"
123+
return ""

mindee/fields/tax.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Optional
22

3-
from mindee.fields import Field
3+
from mindee.fields.base import Field
44

55

66
class Tax(Field):

tests/data/invoices/v2/invoice.json renamed to tests/data/invoices/v3/invoice.json

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"type": "VAT NUMBER",
6969
"value": "FR33501124705"
7070
}
71-
],
71+
],
7272
"customer": {
7373
"confidence": 0.0,
7474
"polygon": [],
@@ -559,7 +559,54 @@
559559
],
560560
"value": "1954 Bloon Street West Toronto, ON, M6P 3K9 Canada"
561561
},
562-
"customer_company_registration": [],
562+
"customer_company_registration": [
563+
{
564+
"confidence": 1.0,
565+
"polygon": [
566+
[
567+
0.346,
568+
0.199
569+
],
570+
[
571+
0.484,
572+
0.199
573+
],
574+
[
575+
0.484,
576+
0.217
577+
],
578+
[
579+
0.346,
580+
0.217
581+
]
582+
],
583+
"type": "VAT NUMBER",
584+
"value": "FR00000000000"
585+
},
586+
{
587+
"confidence": 1.0,
588+
"polygon": [
589+
[
590+
0.346,
591+
0.199
592+
],
593+
[
594+
0.484,
595+
0.199
596+
],
597+
[
598+
0.484,
599+
0.217
600+
],
601+
[
602+
0.346,
603+
0.217
604+
]
605+
],
606+
"type": "SIREN",
607+
"value": "111222333"
608+
}
609+
],
563610
"date": {
564611
"confidence": 0.99,
565612
"page_id": 0,

tests/documents/test_invoice.py

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

55
from mindee.documents.invoice import Invoice
66

7-
INVOICE_FILE_PATH = "./tests/data/invoices/v2/invoice.json"
8-
INVOICE_NA_FILE_PATH = "./tests/data/invoices/v2/invoice_all_na.json"
7+
INVOICE_FILE_PATH = "./tests/data/invoices/v3/invoice.json"
8+
INVOICE_NA_FILE_PATH = "./tests/data/invoices/v3/invoice_all_na.json"
99

1010

1111
@pytest.fixture
@@ -48,7 +48,7 @@ def test_constructor(invoice_object):
4848
Supplier name: TURNPIKE DESIGNS CO.
4949
Supplier address: 156 University Ave, Toronto ON, Canada M5H 2H7
5050
Customer name: JIRO DOI
51-
Customer company registration:
51+
Customer company registration: FR00000000000; 111222333
5252
Customer address: 1954 Bloon Street West Toronto, ON, M6P 3K9 Canada
5353
Payment details: FR7640254025476501124705368;
5454
Company numbers: 501124705; FR33501124705

0 commit comments

Comments
 (0)