Skip to content

Commit 08b286a

Browse files
committed
Enums
1 parent b427b21 commit 08b286a

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
minversion = 3.7
33
log_cli=true
44
python_files = test_*.py
5-
addopts = -n auto --dist=loadscope
5+
;addopts = -n auto --dist=loadscope

src/superannotate/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from superannotate.lib.core import PACKAGE_VERSION_UPGRADE # noqa
1919
from superannotate.logger import get_default_logger # noqa
2020
from superannotate.version import __version__ # noqa
21+
import superannotate.lib.core.enums as enums
2122

2223

2324
SESSIONS = {}
@@ -26,6 +27,7 @@
2627
"__version__",
2728
"SAClient",
2829
# Utils
30+
"enums",
2931
"AppException",
3032
# analytics
3133
"class_distribution",

src/superannotate/lib/app/interface/types.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,23 @@
2222
from pydantic import validate_arguments as pydantic_validate_arguments
2323
from pydantic import ValidationError
2424
from pydantic.errors import StrRegexError
25+
from pydantic import errors
26+
from pydantic.errors import PydanticTypeError
2527

2628
NotEmptyStr = constr(strict=True, min_length=1)
2729

2830

31+
class EnumMemberError(PydanticTypeError):
32+
code = 'enum'
33+
34+
def __str__(self) -> str:
35+
permitted = ', '.join(str(v.name) for v in self.enum_values) # type: ignore
36+
return f'Available values are: {permitted}'
37+
38+
39+
errors.EnumMemberError = EnumMemberError
40+
41+
2942
class EmailStr(StrictStr):
3043
@classmethod
3144
def validate(cls, value: Union[str]) -> Union[str]:

src/superannotate/lib/core/enums.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ def __new__(cls, title, value):
88
obj._value_ = value
99
obj.__doc__ = title
1010
obj._type = "titled_enum"
11+
cls._value2member_map_[title] = obj
1112
return obj
1213

14+
@classmethod
15+
def choices(cls):
16+
return tuple(cls._value2member_map_.keys())
17+
1318
@DynamicClassAttribute
1419
def name(self) -> str:
1520
return self.__doc__
@@ -41,6 +46,9 @@ def titles(cls):
4146
def equals(self, other: Enum):
4247
return self.__doc__.lower() == other.__doc__.lower()
4348

49+
def __eq__(self, other):
50+
return super(BaseTitledEnum, self).__eq__(other)
51+
4452

4553
class AnnotationTypes(str, Enum):
4654
BBOX = "bbox"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# from typing import Literal
2+
from pydantic.typing import Literal
3+
4+
from superannotate import enums
5+
from superannotate import SAClient
6+
from superannotate.lib.app.interface.types import validate_arguments
7+
8+
9+
10+
@validate_arguments
11+
def foo(status: enums.ProjectStatus):
12+
return status
13+
14+
15+
def test_enum_arg():
16+
SAClient()
17+
assert foo(1) == 1
18+
assert foo("NotStarted") == 1
19+
assert foo(enums.ProjectStatus.NotStarted.name) == 1
20+
assert foo(enums.ProjectStatus.NotStarted.value) == 1

0 commit comments

Comments
 (0)