-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor directives fields, drop type_ident - infer type from annotat…
…ions
- Loading branch information
m.kindritskiy
committed
May 25, 2023
1 parent
9104ac3
commit 0ee753e
Showing
17 changed files
with
392 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from dataclasses import dataclass | ||
from typing import Any, Optional, Type | ||
|
||
|
||
@dataclass | ||
class ScalarInfo: | ||
name: str | ||
typ: Any | ||
|
||
|
||
def _process_scalar( | ||
cls: Type, | ||
*, | ||
name: Optional[str] = None, | ||
) -> Type: | ||
cls.__scalar_info__ = ScalarInfo( | ||
name=name or cls.__name__, | ||
typ=cls, | ||
) | ||
|
||
return cls | ||
|
||
|
||
def scalar( | ||
cls: Optional[Type] = None, | ||
*, | ||
name: Optional[str] = None, | ||
) -> Any: | ||
def wrap(cls: Type) -> Type: | ||
return _process_scalar( | ||
cls, | ||
name=name, | ||
) | ||
|
||
if cls is None: | ||
return wrap | ||
|
||
return wrap(cls) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import dataclasses | ||
from enum import EnumMeta | ||
from typing import Any, List, Optional, TypeVar | ||
|
||
EnumType = TypeVar("EnumType", bound=EnumMeta) | ||
|
||
|
||
@dataclasses.dataclass | ||
class EnumValue: | ||
name: str | ||
value: Any | ||
|
||
|
||
@dataclasses.dataclass | ||
class EnumInfo: | ||
wrapped_cls: EnumMeta | ||
name: str | ||
values: List[EnumValue] | ||
|
||
|
||
def _process_enum( | ||
cls: EnumType, | ||
name: Optional[str] = None, | ||
) -> EnumType: | ||
if not isinstance(cls, EnumMeta): | ||
raise TypeError(f"{cls} is not an Enum") | ||
|
||
if not name: | ||
name = cls.__name__ | ||
|
||
values = [] | ||
for item in cls: # type: ignore | ||
value = EnumValue( | ||
item.name, | ||
item.value, | ||
) | ||
values.append(value) | ||
|
||
cls.__enum_info__ = EnumInfo( # type: ignore | ||
wrapped_cls=cls, | ||
name=name, | ||
values=values, | ||
) | ||
|
||
return cls | ||
|
||
|
||
def enum( | ||
_cls: Optional[EnumType] = None, | ||
*, | ||
name: Optional[str] = None, | ||
) -> Any: | ||
def wrap(cls: EnumType) -> EnumType: | ||
return _process_enum(cls, name) | ||
|
||
if not _cls: | ||
return wrap | ||
|
||
return wrap(_cls) |
Oops, something went wrong.