Skip to content

Commit e2a77c5

Browse files
refactor: type hints public interface (#387)
* add type hints * add null checks/disambiguation for typing * add type checking to workflows
1 parent 6b590eb commit e2a77c5

13 files changed

Lines changed: 479 additions & 187 deletions

File tree

‎.github/workflows/python.yml‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ jobs:
2424
gem install awesome_bot
2525
- name: Run tests
2626
run: find . -type f -name '*.md' -exec awesome_bot {} \;
27-
27+
2828
linting:
2929
runs-on: ubuntu-latest
3030
steps:
31-
- uses: actions/checkout@v3
31+
- uses: actions/checkout@v3
3232
- name: Set up Python 3.9
3333
uses: actions/setup-python@v3
3434
with:
@@ -44,27 +44,27 @@ jobs:
4444
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
4545
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
4646
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
47-
47+
4848
integration_tests:
4949
uses: optimizely/python-sdk/.github/workflows/integration_test.yml@master
5050
secrets:
5151
CI_USER_TOKEN: ${{ secrets.CI_USER_TOKEN }}
5252
TRAVIS_COM_TOKEN: ${{ secrets.TRAVIS_COM_TOKEN }}
53-
53+
5454
fullstack_production_suite:
5555
uses: optimizely/python-sdk/.github/workflows/integration_test.yml@master
5656
with:
5757
FULLSTACK_TEST_REPO: ProdTesting
5858
secrets:
5959
CI_USER_TOKEN: ${{ secrets.CI_USER_TOKEN }}
6060
TRAVIS_COM_TOKEN: ${{ secrets.TRAVIS_COM_TOKEN }}
61-
61+
6262
test:
6363
runs-on: ubuntu-latest
6464
strategy:
6565
fail-fast: false
6666
matrix:
67-
python-version: ["pypy-3.7-v7.3.5", "3.7", "3.8", "3.9", "3.10.0"]
67+
python-version: ["pypy-3.7-v7.3.5", "3.7", "3.8", "3.9", "3.10"]
6868
steps:
6969
- uses: actions/checkout@v3
7070
- name: Set up Python ${{ matrix.python-version }}
@@ -78,3 +78,25 @@ jobs:
7878
- name: Test with pytest
7979
run: |
8080
pytest --cov=optimizely
81+
82+
type-check:
83+
runs-on: ubuntu-latest
84+
strategy:
85+
fail-fast: false
86+
matrix:
87+
python-version: ["3.7", "3.8", "3.9", "3.10"]
88+
steps:
89+
- uses: actions/checkout@v3
90+
- name: Set up Python ${{ matrix.python-version }}
91+
uses: actions/setup-python@v3
92+
with:
93+
python-version: ${{ matrix.python-version }}
94+
- name: Install dependencies
95+
run: |
96+
python -m pip install --upgrade pip
97+
pip install -r requirements/typing.txt
98+
- name: Type check with mypy
99+
run: |
100+
mypy .
101+
# disabled until entire sdk is type hinted
102+
# mypy . --exclude "tests/" --strict

‎mypy.ini‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[mypy]
2+
# regex to exclude:
3+
# - docs folder
4+
# - setup.py
5+
# https://mypy.readthedocs.io/en/stable/config_file.html#confval-exclude
6+
exclude = (?x)(
7+
^docs/
8+
| ^setup\.py$
9+
)
10+
show_error_codes = True
11+
pretty = True
12+
13+
# suppress error on conditional import of typing_extensions module
14+
[mypy-optimizely.entities]
15+
no_warn_unused_ignores = True
16+
17+
# suppress error on conditional import of typing_extensions module
18+
[mypy-event_dispatcher]
19+
no_warn_unused_ignores = True
20+
21+
# suppress error on conditional import of typing_extensions module
22+
[mypy-optimizely.condition]
23+
no_warn_unused_ignores = True

‎optimizely/entities.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016-2021, Optimizely
1+
# Copyright 2016-2022, Optimizely
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -11,7 +11,6 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313

14-
1514
class BaseEntity:
1615
def __eq__(self, other):
1716
return self.__dict__ == other.__dict__

‎optimizely/event/event_processor.py‎

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2021 Optimizely
1+
# Copyright 2019-2022, Optimizely
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
@@ -11,17 +11,19 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313

14+
from __future__ import annotations
1415
from abc import ABC, abstractmethod
1516
import numbers
1617
import threading
1718
import time
1819

20+
from typing import Optional
1921
from datetime import timedelta
2022
import queue
2123

2224
from optimizely import logger as _logging
2325
from optimizely import notification_center as _notification_center
24-
from optimizely.event_dispatcher import EventDispatcher as default_event_dispatcher
26+
from optimizely.event_dispatcher import EventDispatcher, CustomEventDispatcher
2527
from optimizely.helpers import enums
2628
from optimizely.helpers import validator
2729
from .event_factory import EventFactory
@@ -32,7 +34,7 @@ class BaseEventProcessor(ABC):
3234
""" Class encapsulating event processing. Override with your own implementation. """
3335

3436
@abstractmethod
35-
def process(self, user_event):
37+
def process(self, user_event: UserEvent) -> None:
3638
""" Method to provide intermediary processing stage within event production.
3739
Args:
3840
user_event: UserEvent instance that needs to be processed and dispatched.
@@ -49,24 +51,28 @@ class BatchEventProcessor(BaseEventProcessor):
4951
maximum duration before the resulting LogEvent is sent to the EventDispatcher.
5052
"""
5153

54+
class Signal:
55+
'''Used to create unique objects for sending signals to event queue.'''
56+
pass
57+
5258
_DEFAULT_QUEUE_CAPACITY = 1000
5359
_DEFAULT_BATCH_SIZE = 10
5460
_DEFAULT_FLUSH_INTERVAL = 30
5561
_DEFAULT_TIMEOUT_INTERVAL = 5
56-
_SHUTDOWN_SIGNAL = object()
57-
_FLUSH_SIGNAL = object()
62+
_SHUTDOWN_SIGNAL = Signal()
63+
_FLUSH_SIGNAL = Signal()
5864
LOCK = threading.Lock()
5965

6066
def __init__(
6167
self,
62-
event_dispatcher,
63-
logger=None,
64-
start_on_init=False,
65-
event_queue=None,
66-
batch_size=None,
67-
flush_interval=None,
68-
timeout_interval=None,
69-
notification_center=None,
68+
event_dispatcher: Optional[type[EventDispatcher] | CustomEventDispatcher] = None,
69+
logger: Optional[_logging.Logger] = None,
70+
start_on_init: bool = False,
71+
event_queue: Optional[queue.Queue[UserEvent | Signal]] = None,
72+
batch_size: Optional[int] = None,
73+
flush_interval: Optional[float] = None,
74+
timeout_interval: Optional[float] = None,
75+
notification_center: Optional[_notification_center.NotificationCenter] = None,
7076
):
7177
""" BatchEventProcessor init method to configure event batching.
7278
@@ -84,43 +90,48 @@ def __init__(
8490
thread.
8591
notification_center: Optional instance of notification_center.NotificationCenter.
8692
"""
87-
self.event_dispatcher = event_dispatcher or default_event_dispatcher
93+
self.event_dispatcher = event_dispatcher or EventDispatcher
8894
self.logger = _logging.adapt_logger(logger or _logging.NoOpLogger())
8995
self.event_queue = event_queue or queue.Queue(maxsize=self._DEFAULT_QUEUE_CAPACITY)
90-
self.batch_size = (
91-
batch_size
96+
self.batch_size: int = (
97+
batch_size # type: ignore
9298
if self._validate_instantiation_props(batch_size, 'batch_size', self._DEFAULT_BATCH_SIZE)
9399
else self._DEFAULT_BATCH_SIZE
94100
)
95-
self.flush_interval = (
96-
timedelta(seconds=flush_interval)
101+
self.flush_interval: timedelta = (
102+
timedelta(seconds=flush_interval) # type: ignore
97103
if self._validate_instantiation_props(flush_interval, 'flush_interval', self._DEFAULT_FLUSH_INTERVAL)
98104
else timedelta(seconds=self._DEFAULT_FLUSH_INTERVAL)
99105
)
100-
self.timeout_interval = (
101-
timedelta(seconds=timeout_interval)
106+
self.timeout_interval: timedelta = (
107+
timedelta(seconds=timeout_interval) # type: ignore
102108
if self._validate_instantiation_props(timeout_interval, 'timeout_interval', self._DEFAULT_TIMEOUT_INTERVAL)
103109
else timedelta(seconds=self._DEFAULT_TIMEOUT_INTERVAL)
104110
)
105111

106112
self.notification_center = notification_center or _notification_center.NotificationCenter(self.logger)
107-
self._current_batch = list()
113+
self._current_batch: list[UserEvent] = []
108114

109115
if not validator.is_notification_center_valid(self.notification_center):
110116
self.logger.error(enums.Errors.INVALID_INPUT.format('notification_center'))
111117
self.logger.debug('Creating notification center for use.')
112118
self.notification_center = _notification_center.NotificationCenter(self.logger)
113119

114-
self.executor = None
120+
self.executor: Optional[threading.Thread] = None
115121
if start_on_init is True:
116122
self.start()
117123

118124
@property
119-
def is_running(self):
125+
def is_running(self) -> bool:
120126
""" Property to check if consumer thread is alive or not. """
121127
return self.executor.is_alive() if self.executor else False
122128

123-
def _validate_instantiation_props(self, prop, prop_name, default_value):
129+
def _validate_instantiation_props(
130+
self,
131+
prop: Optional[numbers.Integral | int | float],
132+
prop_name: str,
133+
default_value: numbers.Integral | int | float
134+
) -> bool:
124135
""" Method to determine if instantiation properties like batch_size, flush_interval
125136
and timeout_interval are valid.
126137
@@ -147,7 +158,7 @@ def _validate_instantiation_props(self, prop, prop_name, default_value):
147158

148159
return is_valid
149160

150-
def _get_time(self, _time=None):
161+
def _get_time(self, _time: Optional[float] = None) -> float:
151162
""" Method to return time as float in seconds. If _time is None, uses current time.
152163
153164
Args:
@@ -161,7 +172,7 @@ def _get_time(self, _time=None):
161172

162173
return _time
163174

164-
def start(self):
175+
def start(self) -> None:
165176
""" Starts the batch processing thread to batch events. """
166177
if hasattr(self, 'executor') and self.is_running:
167178
self.logger.warning('BatchEventProcessor already started.')
@@ -172,7 +183,7 @@ def start(self):
172183
self.executor.daemon = True
173184
self.executor.start()
174185

175-
def _run(self):
186+
def _run(self) -> None:
176187
""" Triggered as part of the thread which batches events or flushes event_queue and hangs on get
177188
for flush interval if queue is empty.
178189
"""
@@ -215,12 +226,12 @@ def _run(self):
215226
self.logger.info('Exiting processing loop. Attempting to flush pending events.')
216227
self._flush_batch()
217228

218-
def flush(self):
229+
def flush(self) -> None:
219230
""" Adds flush signal to event_queue. """
220231

221232
self.event_queue.put(self._FLUSH_SIGNAL)
222233

223-
def _flush_batch(self):
234+
def _flush_batch(self) -> None:
224235
""" Flushes current batch by dispatching event. """
225236
batch_len = len(self._current_batch)
226237
if batch_len == 0:
@@ -237,12 +248,16 @@ def _flush_batch(self):
237248

238249
self.notification_center.send_notifications(enums.NotificationTypes.LOG_EVENT, log_event)
239250

251+
if log_event is None:
252+
self.logger.exception('Error dispatching event: Cannot dispatch None event.')
253+
return
254+
240255
try:
241256
self.event_dispatcher.dispatch_event(log_event)
242257
except Exception as e:
243258
self.logger.error(f'Error dispatching event: {log_event} {e}')
244259

245-
def process(self, user_event):
260+
def process(self, user_event: UserEvent) -> None:
246261
""" Method to process the user_event by putting it in event_queue.
247262
248263
Args:
@@ -263,7 +278,7 @@ def process(self, user_event):
263278
f'Payload not accepted by the queue. Current size: {self.event_queue.qsize()}'
264279
)
265280

266-
def _add_to_batch(self, user_event):
281+
def _add_to_batch(self, user_event: UserEvent) -> None:
267282
""" Method to append received user event to current batch.
268283
269284
Args:
@@ -283,7 +298,7 @@ def _add_to_batch(self, user_event):
283298
self.logger.debug('Flushing on batch size.')
284299
self._flush_batch()
285300

286-
def _should_split(self, user_event):
301+
def _should_split(self, user_event: UserEvent) -> bool:
287302
""" Method to check if current event batch should split into two.
288303
289304
Args:
@@ -308,7 +323,7 @@ def _should_split(self, user_event):
308323

309324
return False
310325

311-
def stop(self):
326+
def stop(self) -> None:
312327
""" Stops and disposes batch event processor. """
313328
self.event_queue.put(self._SHUTDOWN_SIGNAL)
314329
self.logger.warning('Stopping Scheduler.')
@@ -327,23 +342,28 @@ class ForwardingEventProcessor(BaseEventProcessor):
327342
The ForwardingEventProcessor sends the LogEvent to EventDispatcher as soon as it is received.
328343
"""
329344

330-
def __init__(self, event_dispatcher, logger=None, notification_center=None):
345+
def __init__(
346+
self,
347+
event_dispatcher: type[EventDispatcher] | CustomEventDispatcher,
348+
logger: Optional[_logging.Logger] = None,
349+
notification_center: Optional[_notification_center.NotificationCenter] = None
350+
):
331351
""" ForwardingEventProcessor init method to configure event dispatching.
332352
333353
Args:
334354
event_dispatcher: Provides a dispatch_event method which if given a URL and params sends a request to it.
335355
logger: Optional component which provides a log method to log messages. By default nothing would be logged.
336356
notification_center: Optional instance of notification_center.NotificationCenter.
337357
"""
338-
self.event_dispatcher = event_dispatcher or default_event_dispatcher
358+
self.event_dispatcher = event_dispatcher or EventDispatcher
339359
self.logger = _logging.adapt_logger(logger or _logging.NoOpLogger())
340360
self.notification_center = notification_center or _notification_center.NotificationCenter(self.logger)
341361

342362
if not validator.is_notification_center_valid(self.notification_center):
343363
self.logger.error(enums.Errors.INVALID_INPUT.format('notification_center'))
344364
self.notification_center = _notification_center.NotificationCenter()
345365

346-
def process(self, user_event):
366+
def process(self, user_event: UserEvent) -> None:
347367
""" Method to process the user_event by dispatching it.
348368
349369
Args:
@@ -361,6 +381,10 @@ def process(self, user_event):
361381

362382
self.notification_center.send_notifications(enums.NotificationTypes.LOG_EVENT, log_event)
363383

384+
if log_event is None:
385+
self.logger.exception('Error dispatching event: Cannot dispatch None event.')
386+
return
387+
364388
try:
365389
self.event_dispatcher.dispatch_event(log_event)
366390
except Exception as e:

0 commit comments

Comments
 (0)