diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index f09c11d0..ae77c586 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -23,3 +23,9 @@ jobs: - name: Analysing the code with Black run: | black --check . + - name: Analysing the code with mypy + run: | + mypy --install-types --non-interactive ./mindee + - name: Analysing the code with pylint + run: | + pylint ./mindee diff --git a/mindee/__init__.py b/mindee/__init__.py index 32b98aef..5f718e30 100644 --- a/mindee/__init__.py +++ b/mindee/__init__.py @@ -19,7 +19,7 @@ } -class Client(object): +class Client: def __init__( self, expense_receipt_token=None, @@ -231,7 +231,7 @@ def parse_financial_document( return self._wrap_response(input_file, response, "financial_document") -class Response(object): +class Response: def __init__( self, http_response=None, pages=None, document=None, document_type=None ): @@ -311,7 +311,7 @@ def format_response(json_response, document_type, input_file): raise Exception("Document type not supported.") # Create page level objects - for page_n, page_prediction in enumerate( + for _, page_prediction in enumerate( json_response["document"]["inference"]["pages"] ): pages.append( diff --git a/mindee/documents/__init__.py b/mindee/documents/__init__.py index 4655b9d5..1ee36e56 100644 --- a/mindee/documents/__init__.py +++ b/mindee/documents/__init__.py @@ -1,4 +1,4 @@ -class Document(object): +class Document: def __init__(self, input_file=None): self.filepath = None self.filename = None @@ -11,6 +11,7 @@ def __init__(self, input_file=None): self.checklist = {} def request(self, *args): + """Make request to the product endpoint""" raise NotImplementedError() def _checklist(self, *args): @@ -20,4 +21,5 @@ def _reconstruct(self, *args): pass def all_checks(self): + """Return all checks""" return all(self.checklist) diff --git a/mindee/documents/car_plate.py b/mindee/documents/car_plate.py index cc2c4927..f23f946e 100644 --- a/mindee/documents/car_plate.py +++ b/mindee/documents/car_plate.py @@ -1,7 +1,6 @@ from mindee.documents import Document from mindee.fields import Field from mindee.http import make_api_request, make_predict_url -import os class CarPlate(Document): @@ -46,7 +45,7 @@ def build_from_api_prediction(self, api_prediction, page_n=0): for license_plate in api_prediction["license_plates"] ] - def __str__(self): + def __str__(self) -> str: return ( "-----Car plate data-----\n" "Filename: %s\n" diff --git a/mindee/documents/financial_document.py b/mindee/documents/financial_document.py index 6fb46bb8..6ee2a73e 100644 --- a/mindee/documents/financial_document.py +++ b/mindee/documents/financial_document.py @@ -8,7 +8,6 @@ from mindee.http import make_api_request, make_predict_url from mindee.documents.invoice import Invoice from mindee.documents.receipt import Receipt -import os class FinancialDocument(Document): @@ -159,7 +158,7 @@ def build_from_api_prediction(self, api_prediction, input_file, page_n=0): self.payment_details = [] self.company_number = [] - def __str__(self): + def __str__(self) -> str: return ( "-----Financial Document data-----\n" "Filename: %s \n" diff --git a/mindee/documents/invoice.py b/mindee/documents/invoice.py index 1351350f..b34702bc 100644 --- a/mindee/documents/invoice.py +++ b/mindee/documents/invoice.py @@ -7,7 +7,6 @@ from mindee.fields.payment_details import PaymentDetails from mindee.fields.tax import Tax from mindee.http import make_api_request, make_predict_url -import os class Invoice(Document): @@ -153,7 +152,7 @@ def build_from_api_prediction(self, api_prediction, page_n=0): {"value": None, "probability": 0.0}, value_key="value", page_n=page_n ) - def __str__(self): + def __str__(self) -> str: return ( "-----Invoice data-----\n" "Filename: %s \n" diff --git a/mindee/documents/passport.py b/mindee/documents/passport.py index 01a04c3b..02f36ede 100644 --- a/mindee/documents/passport.py +++ b/mindee/documents/passport.py @@ -1,9 +1,8 @@ +from datetime import datetime from mindee.documents import Document from mindee.fields import Field from mindee.fields.date import Date -from datetime import datetime from mindee.http import make_api_request, make_predict_url -import os class Passport(Document): @@ -127,7 +126,7 @@ def build_from_api_prediction(self, api_prediction, page_n=0): self.mrz = Field({"value": None, "probability": 0.0}, page_n=page_n) self.full_name = Field({"value": None, "probability": 0.0}, page_n=page_n) - def __str__(self): + def __str__(self) -> str: return ( "-----Passport data-----\n" "Filename: %s \n" diff --git a/mindee/documents/receipt.py b/mindee/documents/receipt.py index c3e0be62..cb99878c 100644 --- a/mindee/documents/receipt.py +++ b/mindee/documents/receipt.py @@ -6,7 +6,6 @@ from mindee.fields.orientation import Orientation from mindee.fields.tax import Tax from mindee.http import make_api_request, make_predict_url -import os class Receipt(Document): @@ -95,7 +94,7 @@ def __init__( # Reconstruct extra fields self._reconstruct() - def __str__(self): + def __str__(self) -> str: return ( "-----Receipt data-----\n" "Filename: %s\n" diff --git a/mindee/fields/__init__.py b/mindee/fields/__init__.py index f16785e2..9ae9cac4 100644 --- a/mindee/fields/__init__.py +++ b/mindee/fields/__init__.py @@ -1,4 +1,4 @@ -class Field(object): +class Field: def __init__( self, abstract_prediction, diff --git a/mindee/http.py b/mindee/http.py index 0ca8dfad..f22eb31a 100644 --- a/mindee/http.py +++ b/mindee/http.py @@ -3,7 +3,7 @@ MINDEE_API_URL = "https://api.mindee.net/v1" -platform = get_platform() +PLATFORM = get_platform() def make_predict_url(product: str, version: str, owner: str = "mindee") -> str: @@ -30,7 +30,7 @@ def make_api_request(url: str, input_file, token: str, include_words: bool = Fal files = {"document": (input_file.filename, input_file.file_object.read())} headers = { "Authorization": f"Token {token}", - "User-Agent": f"mindee-api-python@v{__version__} python-v{python_version} {platform}", + "User-Agent": f"mindee-api-python@v{__version__} python-v{python_version} {PLATFORM}", } params = {} diff --git a/mindee/inputs.py b/mindee/inputs.py index b5f8d79d..45961997 100644 --- a/mindee/inputs.py +++ b/mindee/inputs.py @@ -1,11 +1,11 @@ -import fitz import io import os from base64 import decodebytes from mimetypes import guess_type +import fitz -class Inputs(object): +class Inputs: def __init__( self, file, input_type="path", filename=None, cut_pdf=True, n_pdf_pages=3 ): @@ -119,7 +119,7 @@ def merge_pdf_pages(self, pages_number): width = spage.MediaBoxSize[0] height = spage.MediaBoxSize[1] r = fitz.Rect(0, 0, width, height) - page = doc.newPage(-1, width=width, height=height) + page = doc.new_page(-1, width=width, height=height) try: page.showPDFpage(r, src, spage.number) except: diff --git a/mindee/versions.py b/mindee/versions.py index 43f45f82..6996b60b 100644 --- a/mindee/versions.py +++ b/mindee/versions.py @@ -10,6 +10,7 @@ def get_platform() -> str: + """Get the current OS platform.""" platforms = { "linux": "linux", "win32": "windows", diff --git a/pyproject.toml b/pyproject.toml index e2dd2233..5e44d396 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,5 +3,34 @@ line-length = 88 target-version = ['py35', 'py36', 'py37'] include = '\.pyi?$' -[tool.mypy] +[[tool.mypy.overrides]] +module = ['fitz',] ignore_missing_imports = true + +[tool.pylint.'MESSAGES CONTROL'] +disable=[ + 'line-too-long', + 'bare-except', + 'consider-using-set-comprehension', + 'missing-class-docstring', + 'missing-module-docstring', + 'super-with-arguments', + 'no-else-return', + 'too-many-instance-attributes', + 'too-many-locals', + 'too-many-arguments', + 'duplicate-code', + 'unidiomatic-typecheck', + 'arguments-differ', + 'inconsistent-return-statements', + 'invalid-name', + 'super-init-not-called', + 'no-else-raise', + 'raise-missing-from', + 'consider-iterating-dictionary', + 'unspecified-encoding', + 'use-implicit-booleaness-not-len', + 'unnecessary-pass', + 'consider-using-f-string', + 'consider-using-with', +] diff --git a/setup.py b/setup.py index 3671290d..84156d82 100644 --- a/setup.py +++ b/setup.py @@ -16,9 +16,9 @@ requirements = [ - "requests==2.25.1", "pytz==2021.3", "PyMuPDF==1.18.17", + "requests==2.25.1", ] test_requirements = [ @@ -28,8 +28,10 @@ dev_requirements = [ "black==21.12b0", - "setuptools==49.2.0", + "mypy==0.931", "pip-tools==6.4.0", + "pylint==2.12.2", + "setuptools==49.2.0", ] setup(