|
1 | 1 | import io |
2 | | -from typing import Any, Callable, Dict, List, Optional, Type |
| 2 | +from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Type |
3 | 3 |
|
4 | 4 | from PIL import Image, ImageDraw |
5 | 5 |
|
6 | | -try: |
7 | | - # NOTE: we always use pydantic v1 but have to do these shenanigans to support both v1 and v2 |
| 6 | +if TYPE_CHECKING: |
| 7 | + # Backwards compatibility is even uglier with mypy |
8 | 8 | from pydantic.v1 import BaseModel, Extra, ValidationError |
9 | | -except ImportError: |
10 | | - from pydantic import BaseModel, Extra, ValidationError |
| 9 | +else: |
| 10 | + try: |
| 11 | + # NOTE: we always use pydantic v1 but have to do these shenanigans to support both v1 and v2 |
| 12 | + from pydantic.v1 import BaseModel, Extra, ValidationError |
| 13 | + except ImportError: |
| 14 | + from pydantic import BaseModel, Extra, ValidationError |
11 | 15 |
|
12 | 16 | # From scaleapi/server/src/lib/select/api/types.ts |
13 | 17 | # These classes specify how user models must pass output to Launch + Nucleus. |
14 | 18 |
|
15 | 19 |
|
16 | | -class PointModel(BaseModel): |
| 20 | +class PointModel(BaseModel): # pylint: disable=used-before-assignment |
17 | 21 | x: float |
18 | 22 | y: float |
19 | 23 |
|
20 | 24 | class Config: |
21 | | - extra = Extra.forbid |
| 25 | + extra = Extra.forbid # pylint: disable=used-before-assignment |
22 | 26 |
|
23 | 27 |
|
24 | 28 | class BoxGeometryModel(BaseModel): |
@@ -106,7 +110,7 @@ def verify_output( |
106 | 110 | for annotation in annotation_list: |
107 | 111 | try: |
108 | 112 | model.parse_obj(annotation) |
109 | | - except ValidationError as e: |
| 113 | + except ValidationError as e: # pylint: disable=used-before-assignment |
110 | 114 | raise ValueError("Failed validation") from e |
111 | 115 | if annotation["type"] != annotation_type: |
112 | 116 | raise ValueError( |
|
0 commit comments