Skip to content

Commit 5eef5d1

Browse files
Merge pull request #2314 from VWS-Python/more-beartype
Do runtime type checking
2 parents 20d95f8 + 4839042 commit 5eef5d1

File tree

10 files changed

+59
-1
lines changed

10 files changed

+59
-1
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ dynamic = [
3333
"version",
3434
]
3535
dependencies = [
36+
"beartype>=0.18.5",
3637
"requests",
3738
"urllib3",
3839
"vws-auth-tools",
3940
]
4041
optional-dependencies.dev = [
4142
"actionlint-py==1.7.1.15",
42-
"beartype==0.18.5",
4343
"check-manifest==0.49",
4444
"deptry==0.20.0",
4545
"doc8==1.1.1",

src/vws/exceptions/base_exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
Cloud Recognition Web API.
44
"""
55

6+
from beartype import beartype
7+
68
from .response import Response
79

810

11+
@beartype
912
class CloudRecoError(Exception):
1013
"""
1114
Base class for Vuforia Cloud Recognition Web API exceptions.
@@ -27,6 +30,7 @@ def response(self) -> Response:
2730
return self._response
2831

2932

33+
@beartype
3034
class VWSError(Exception):
3135
"""
3236
Base class for Vuforia Web Services errors.

src/vws/exceptions/cloud_reco_exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,44 @@
22
Exceptions which match errors raised by the Vuforia Cloud Recognition Web APIs.
33
"""
44

5+
from beartype import beartype
6+
57
from vws.exceptions.base_exceptions import CloudRecoError
68

79

10+
@beartype
811
class MaxNumResultsOutOfRangeError(CloudRecoError):
912
"""
1013
Exception raised when the ``max_num_results`` given to the Cloud
1114
Recognition Web API query endpoint is out of range.
1215
"""
1316

1417

18+
@beartype
1519
class InactiveProjectError(CloudRecoError):
1620
"""
1721
Exception raised when Vuforia returns a response with a result code
1822
'InactiveProject'.
1923
"""
2024

2125

26+
@beartype
2227
class BadImageError(CloudRecoError):
2328
"""
2429
Exception raised when Vuforia returns a response with a result code
2530
'BadImage'.
2631
"""
2732

2833

34+
@beartype
2935
class AuthenticationFailureError(CloudRecoError):
3036
"""
3137
Exception raised when Vuforia returns a response with a result code
3238
'AuthenticationFailure'.
3339
"""
3440

3541

42+
@beartype
3643
class RequestTimeTooSkewedError(CloudRecoError):
3744
"""
3845
Exception raised when Vuforia returns a response with a result code

src/vws/exceptions/custom_exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
or simple errors given by the cloud recognition service.
55
"""
66

7+
from beartype import beartype
8+
79
from .response import Response
810

911

12+
@beartype
1013
class OopsAnErrorOccurredPossiblyBadNameError(Exception):
1114
"""
1215
Exception raised when VWS returns an HTML page which says "Oops, an error
@@ -31,6 +34,7 @@ def response(self) -> Response:
3134
return self._response
3235

3336

37+
@beartype
3438
class RequestEntityTooLargeError(Exception):
3539
"""
3640
Exception raised when the given image is too large.
@@ -52,12 +56,14 @@ def response(self) -> Response:
5256
return self._response
5357

5458

59+
@beartype
5560
class TargetProcessingTimeoutError(Exception):
5661
"""
5762
Exception raised when waiting for a target to be processed times out.
5863
"""
5964

6065

66+
@beartype
6167
class ServerError(Exception): # pragma: no cover
6268
"""
6369
Exception raised when VWS returns a server error.

src/vws/exceptions/response.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
from dataclasses import dataclass
44

5+
from beartype import beartype
6+
57

68
@dataclass
9+
@beartype
710
class Response:
811
"""
912
A response from a request.

src/vws/exceptions/vws_exceptions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
import json
88
from urllib.parse import urlparse
99

10+
from beartype import beartype
11+
1012
from vws.exceptions.base_exceptions import VWSError
1113

1214

15+
@beartype
1316
class UnknownTargetError(VWSError):
1417
"""
1518
Exception raised when Vuforia returns a response with a result code
@@ -27,20 +30,23 @@ def target_id(self) -> str:
2730
return path.split(sep="/", maxsplit=2)[-1]
2831

2932

33+
@beartype
3034
class FailError(VWSError):
3135
"""
3236
Exception raised when Vuforia returns a response with a result code
3337
'Fail'.
3438
"""
3539

3640

41+
@beartype
3742
class BadImageError(VWSError):
3843
"""
3944
Exception raised when Vuforia returns a response with a result code
4045
'BadImage'.
4146
"""
4247

4348

49+
@beartype
4450
class AuthenticationFailureError(VWSError):
4551
"""
4652
Exception raised when Vuforia returns a response with a result code
@@ -49,13 +55,15 @@ class AuthenticationFailureError(VWSError):
4955

5056

5157
# See https://github.com/VWS-Python/vws-python/issues/822.
58+
@beartype
5259
class RequestQuotaReachedError(VWSError): # pragma: no cover
5360
"""
5461
Exception raised when Vuforia returns a response with a result code
5562
'RequestQuotaReached'.
5663
"""
5764

5865

66+
@beartype
5967
class TargetStatusProcessingError(VWSError):
6068
"""
6169
Exception raised when Vuforia returns a response with a result code
@@ -74,6 +82,7 @@ def target_id(self) -> str:
7482

7583

7684
# This is not simulated by the mock.
85+
@beartype
7786
class DateRangeError(VWSError): # pragma: no cover
7887
"""
7988
Exception raised when Vuforia returns a response with a result code
@@ -82,6 +91,7 @@ class DateRangeError(VWSError): # pragma: no cover
8291

8392

8493
# This is not simulated by the mock.
94+
@beartype
8595
class TargetQuotaReachedError(VWSError): # pragma: no cover
8696
"""
8797
Exception raised when Vuforia returns a response with a result code
@@ -90,6 +100,7 @@ class TargetQuotaReachedError(VWSError): # pragma: no cover
90100

91101

92102
# This is not simulated by the mock.
103+
@beartype
93104
class ProjectSuspendedError(VWSError): # pragma: no cover
94105
"""
95106
Exception raised when Vuforia returns a response with a result code
@@ -98,34 +109,39 @@ class ProjectSuspendedError(VWSError): # pragma: no cover
98109

99110

100111
# This is not simulated by the mock.
112+
@beartype
101113
class ProjectHasNoAPIAccessError(VWSError): # pragma: no cover
102114
"""
103115
Exception raised when Vuforia returns a response with a result code
104116
'ProjectHasNoAPIAccess'.
105117
"""
106118

107119

120+
@beartype
108121
class ProjectInactiveError(VWSError):
109122
"""
110123
Exception raised when Vuforia returns a response with a result code
111124
'ProjectInactive'.
112125
"""
113126

114127

128+
@beartype
115129
class MetadataTooLargeError(VWSError):
116130
"""
117131
Exception raised when Vuforia returns a response with a result code
118132
'MetadataTooLarge'.
119133
"""
120134

121135

136+
@beartype
122137
class RequestTimeTooSkewedError(VWSError):
123138
"""
124139
Exception raised when Vuforia returns a response with a result code
125140
'RequestTimeTooSkewed'.
126141
"""
127142

128143

144+
@beartype
129145
class TargetNameExistError(VWSError):
130146
"""
131147
Exception raised when Vuforia returns a response with a result code
@@ -142,13 +158,15 @@ def target_name(self) -> str:
142158
return str(request_json["name"])
143159

144160

161+
@beartype
145162
class ImageTooLargeError(VWSError):
146163
"""
147164
Exception raised when Vuforia returns a response with a result code
148165
'ImageTooLarge'.
149166
"""
150167

151168

169+
@beartype
152170
class TargetStatusNotSuccessError(VWSError):
153171
"""
154172
Exception raised when Vuforia returns a response with a result code
@@ -166,6 +184,7 @@ def target_id(self) -> str:
166184
return path.split(sep="/", maxsplit=2)[-1]
167185

168186

187+
@beartype
169188
class TooManyRequestsError(VWSError): # pragma: no cover
170189
"""
171190
Exception raised when Vuforia returns a response with a result code

src/vws/include_target_data.py

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

55
from enum import StrEnum, auto
66

7+
from beartype import beartype
78

9+
10+
@beartype
811
class CloudRecoIncludeTargetData(StrEnum):
912
"""
1013
Options for the ``include_target_data`` parameter of

src/vws/query.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from urllib.parse import urljoin
1111

1212
import requests
13+
from beartype import beartype
1314
from urllib3.filepost import encode_multipart_formdata
1415
from vws_auth_tools import authorization_header, rfc_1123_date
1516

@@ -31,6 +32,7 @@
3132
_ImageType = io.BytesIO | BinaryIO
3233

3334

35+
@beartype
3436
def _get_image_data(image: _ImageType) -> bytes:
3537
"""Get the data of an image file."""
3638
original_tell = image.tell()
@@ -40,6 +42,7 @@ def _get_image_data(image: _ImageType) -> bytes:
4042
return image_data
4143

4244

45+
@beartype
4346
class CloudRecoService:
4447
"""
4548
An interface to the Vuforia Cloud Recognition Web APIs.

src/vws/reports.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
from dataclasses import dataclass
77
from enum import Enum
88

9+
from beartype import BeartypeConf, beartype
910

11+
12+
@beartype
1013
@dataclass
1114
class DatabaseSummaryReport:
1215
"""
@@ -30,6 +33,7 @@ class DatabaseSummaryReport:
3033
total_recos: int
3134

3235

36+
@beartype
3337
class TargetStatuses(Enum):
3438
"""Constants representing VWS target statuses.
3539
@@ -42,6 +46,7 @@ class TargetStatuses(Enum):
4246
FAILED = "failed"
4347

4448

49+
@beartype
4550
@dataclass
4651
class TargetSummaryReport:
4752
"""
@@ -62,6 +67,7 @@ class TargetSummaryReport:
6267
previous_month_recos: int
6368

6469

70+
@beartype(conf=BeartypeConf(is_pep484_tower=True))
6571
@dataclass
6672
class TargetRecord:
6773
"""
@@ -79,6 +85,7 @@ class TargetRecord:
7985
reco_rating: str
8086

8187

88+
@beartype
8289
@dataclass
8390
class TargetData:
8491
"""
@@ -90,6 +97,7 @@ class TargetData:
9097
target_timestamp: datetime.datetime
9198

9299

100+
@beartype
93101
@dataclass
94102
class QueryResult:
95103
"""
@@ -103,6 +111,7 @@ class QueryResult:
103111
target_data: TargetData | None
104112

105113

114+
@beartype
106115
@dataclass
107116
class TargetStatusAndRecord:
108117
"""

src/vws/vws.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from urllib.parse import urljoin
1313

1414
import requests
15+
from beartype import BeartypeConf, beartype
1516
from vws_auth_tools import authorization_header, rfc_1123_date
1617

1718
from vws.exceptions.custom_exceptions import (
@@ -51,6 +52,7 @@
5152
_ImageType = io.BytesIO | BinaryIO
5253

5354

55+
@beartype
5456
def _get_image_data(image: _ImageType) -> bytes:
5557
"""Get the data of an image file."""
5658
original_tell = image.tell()
@@ -60,6 +62,7 @@ def _get_image_data(image: _ImageType) -> bytes:
6062
return image_data
6163

6264

65+
@beartype
6366
def _target_api_request(
6467
server_access_key: str,
6568
server_secret_key: str,
@@ -125,6 +128,7 @@ def _target_api_request(
125128
)
126129

127130

131+
@beartype(conf=BeartypeConf(is_pep484_tower=True))
128132
class VWS:
129133
"""
130134
An interface to Vuforia Web Services APIs.

0 commit comments

Comments
 (0)