Skip to content

Commit a49da14

Browse files
added type hints
1 parent f779617 commit a49da14

3 files changed

Lines changed: 16 additions & 9 deletions

File tree

sifter/extensions/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import (
22
TYPE_CHECKING,
33
Dict,
4+
List,
45
Text,
56
Optional,
67
Union,
@@ -16,7 +17,7 @@
1617
class ExtensionRegistry():
1718

1819
_HANDLERS_MAP: Dict[Text, Dict[Text, Union[bool, Type['Comparator'], Type['Rule']]]] = {}
19-
DEFAULT_EXTENSION = [
20+
DEFAULT_EXTENSION: List[Text] = [
2021
'regex',
2122
'comparator-i;ascii-casemap',
2223
'comparator-i;octet',
@@ -31,18 +32,18 @@ def __init__(self) -> None:
3132
self.register_handler(entry_point.load())
3233

3334
@classmethod
34-
def register_extension(cls, extension_name):
35+
def register_extension(cls, extension_name: Text) -> None:
3536
cls.register('extension', extension_name, True)
3637

3738
@classmethod
38-
def register_handler(cls, ext_cls):
39+
def register_handler(cls, ext_cls: Union[Type['Comparator'], Type['Rule']]) -> None:
3940
cls.register(ext_cls.handler_type(), ext_cls.handler_id(), ext_cls)
4041

4142
@classmethod
4243
def register(
4344
cls,
44-
handler_type: Optional[Text],
45-
handler_id: Optional[Text],
45+
handler_type: Text,
46+
handler_id: Text,
4647
value: Union[bool, Type['Comparator'], Type['Rule']]
4748
) -> None:
4849
cls._HANDLERS_MAP.setdefault(handler_type, {})[handler_id] = value

sifter/grammar/comparator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ class Comparator(object):
1515
COMPARATOR_ID: Optional[Text] = None
1616

1717
@classmethod
18-
def handler_type(cls):
18+
def handler_type(cls) -> Text:
1919
return 'comparator'
2020

2121
@classmethod
22-
def handler_id(cls):
22+
def handler_id(cls) -> Text:
23+
if cls.COMPARATOR_ID is None:
24+
raise NotImplementedError('Rule must be implemented as subclass as COMPARATOR_ID must be set')
2325
return cls.COMPARATOR_ID
2426

2527
@classmethod

sifter/grammar/rule.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ class Rule(object):
3636
TESTS_MAX: Optional[int] = None
3737

3838
@classmethod
39-
def handler_type(cls):
39+
def handler_type(cls) -> Text:
40+
if cls.RULE_TYPE is None:
41+
raise NotImplementedError('Rule must be implemented as subclass as RULE_TYPE must be set')
4042
return cls.RULE_TYPE
4143

4244
@classmethod
43-
def handler_id(cls):
45+
def handler_id(cls) -> Text:
46+
if cls.RULE_IDENTIFIER is None:
47+
raise NotImplementedError('Rule must be implemented as subclass as RULE_IDENTIFIER must be set')
4448
return cls.RULE_IDENTIFIER
4549

4650
def __init__(

0 commit comments

Comments
 (0)