diff options
| author | mryouse | 2022-06-16 04:09:26 +0000 |
|---|---|---|
| committer | mryouse | 2022-06-16 04:09:26 +0000 |
| commit | a072842248fe7324574bc7733a8c4255af56c855 (patch) | |
| tree | 86641bc9c500a2463b279a7968fa7e2532179632 /typeclass.py | |
| parent | 3b8834623d780316f81535029f2f8fee40736878 (diff) | |
refactor: take type hints from user in function args
Diffstat (limited to 'typeclass.py')
| -rw-r--r-- | typeclass.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/typeclass.py b/typeclass.py new file mode 100644 index 0000000..eae412c --- /dev/null +++ b/typeclass.py @@ -0,0 +1,34 @@ +from enum import Enum, auto + +class TypeEnum(Enum): + ANY = auto() + STRING = auto() + INT = auto() + FLOAT = auto() + NUMBER = auto() + LIST = auto() + LITERAL = auto() + BOOL = auto() + + def __str__(self): + return f":{self.name.lower()}" + +HIERARCHY = { TypeEnum.ANY: None, + TypeEnum.LITERAL: TypeEnum.ANY, + TypeEnum.LIST: TypeEnum.ANY, + TypeEnum.STRING: TypeEnum.LITERAL, + TypeEnum.BOOL: TypeEnum.LITERAL, + TypeEnum.NUMBER: TypeEnum.LITERAL, + TypeEnum.INT: TypeEnum.NUMBER, + TypeEnum.FLOAT: TypeEnum.NUMBER } + +def is_subtype_of(candidate, expected): + if candidate == expected: + return True + parent = HIERARCHY[candidate] + while parent is not None: + if parent == expected: + return True + parent = HIERARCHY[parent] + return False + |
