Skip to content

Commit

Permalink
Disallow untyped defs (#632)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrattli committed Mar 12, 2022
1 parent 9bb8393 commit 964fcde
Show file tree
Hide file tree
Showing 45 changed files with 117 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repos:
language: node
pass_filenames: false
types: [python]
additional_dependencies: ["pyright@1.1.228"]
additional_dependencies: ["pyright@1.1.229"]
repo: local
- hooks:
- id: mypy
Expand Down
4 changes: 2 additions & 2 deletions examples/parallel/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
seconds = [5, 1, 2, 4, 3]


def sleep(tm):
def sleep(tm: float) -> float:
time.sleep(tm)
return tm


def output(result):
def output(result: str) -> None:
print("%d seconds" % result)


Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ python_version = "3.9"
follow_imports = "silent"
files = ["reactivex"]
exclude = ["reactivex/operators/_\\w.*\\.py$"] # mypy will eventually catch up
disallow_any_generics = true
disallow_untyped_defs = true

[tool.pytest.ini_options]
testpaths = ["tests"]
Expand Down
7 changes: 5 additions & 2 deletions reactivex/abc/disposable.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from types import TracebackType
from typing import Optional, Type
Expand All @@ -15,15 +17,16 @@ def dispose(self) -> None:
"""
raise NotImplementedError

def __enter__(self):
def __enter__(self) -> DisposableBase:
"""Context management protocol."""
return self

def __exit__(
self,
exctype: Optional[Type[BaseException]],
excinst: Optional[BaseException],
exctb: Optional[TracebackType],
):
) -> None:
"""Context management protocol."""
self.dispose()

Expand Down
2 changes: 1 addition & 1 deletion reactivex/disposable/booleandisposable.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class BooleanDisposable(DisposableBase):
"""Represents a Disposable that can be checked for status."""

def __init__(self):
def __init__(self) -> None:
"""Initializes a new instance of the BooleanDisposable class."""

self.is_disposed = False
Expand Down
2 changes: 1 addition & 1 deletion reactivex/disposable/multipleassignmentdisposable.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class MultipleAssignmentDisposable(DisposableBase):
"""Represents a disposable resource whose underlying disposable
resource can be replaced by another disposable resource."""

def __init__(self):
def __init__(self) -> None:
self.current: Optional[DisposableBase] = None
self.is_disposed = False
self.lock = RLock()
Expand Down
2 changes: 1 addition & 1 deletion reactivex/disposable/scheduleddisposable.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def is_disposed(self) -> bool:
def dispose(self) -> None:
"""Disposes the wrapped disposable on the provided scheduler."""

def action(scheduler: abc.SchedulerBase, state: Any):
def action(scheduler: abc.SchedulerBase, state: Any) -> None:
"""Scheduled dispose action"""

self.disposable.dispose()
Expand Down
2 changes: 1 addition & 1 deletion reactivex/disposable/singleassignmentdisposable.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self) -> None:
def get_disposable(self) -> Optional[DisposableBase]:
return self.current

def set_disposable(self, value: DisposableBase):
def set_disposable(self, value: DisposableBase) -> None:
if self.current:
raise Exception("Disposable has already been assigned")

Expand Down
2 changes: 1 addition & 1 deletion reactivex/internal/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
_T = TypeVar("_T")


def noop(*args: Any, **kw: Any):
def noop(*args: Any, **kw: Any) -> None:
"""No operation. Returns nothing"""


Expand Down
2 changes: 1 addition & 1 deletion reactivex/internal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def add_ref(xs: "Observable[_T]", r: RefCountDisposable) -> "Observable[_T]":

def subscribe(
observer: abc.ObserverBase[Any], scheduler: Optional[abc.SchedulerBase] = None
):
) -> abc.DisposableBase:
return CompositeDisposable(r.disposable, xs.subscribe(observer))

return Observable(subscribe)
Expand Down
16 changes: 8 additions & 8 deletions reactivex/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def subscribe(
observer: abc.ObserverBase[_T],
scheduler: Optional[abc.SchedulerBase] = None,
) -> abc.DisposableBase:
def action(scheduler: abc.SchedulerBase, state: Any):
def action(scheduler: abc.SchedulerBase, state: Any) -> None:
self._accept_observer(observer)
if self.kind == "N":
observer.on_completed()
Expand All @@ -105,7 +105,7 @@ def __eq__(self, other: Any) -> bool:
class OnNext(Notification[_T]):
"""Represents an OnNext notification to an observer."""

def __init__(self, value: _T):
def __init__(self, value: _T) -> None:
"""Constructs a notification of a new value."""

super(OnNext, self).__init__()
Expand Down Expand Up @@ -134,7 +134,7 @@ def __str__(self) -> str:
class OnError(Notification[_T]):
"""Represents an OnError notification to an observer."""

def __init__(self, error: Union[Exception, str]):
def __init__(self, error: Union[Exception, str]) -> None:
"""Constructs a notification of an exception."""

super(OnError, self).__init__()
Expand All @@ -151,7 +151,7 @@ def _accept(
) -> None:
return on_error(self.exception) if on_error else None

def _accept_observer(self, observer: abc.ObserverBase[_T]):
def _accept_observer(self, observer: abc.ObserverBase[_T]) -> None:
return observer.on_error(self.exception)

def __str__(self) -> str:
Expand All @@ -161,7 +161,7 @@ def __str__(self) -> str:
class OnCompleted(Notification[_T]):
"""Represents an OnCompleted notification to an observer."""

def __init__(self):
def __init__(self) -> None:
"""Constructs a notification of the end of a sequence."""

super(OnCompleted, self).__init__()
Expand All @@ -175,7 +175,7 @@ def _accept(
) -> None:
return on_completed() if on_completed else None

def _accept_observer(self, observer: abc.ObserverBase[_T]):
def _accept_observer(self, observer: abc.ObserverBase[_T]) -> None:
return observer.on_completed()

def __str__(self) -> str:
Expand All @@ -196,10 +196,10 @@ def from_notifier(handler: Callable[[Notification[_T]], None]) -> Observer[_T]:
def _on_next(value: _T) -> None:
return handler(OnNext(value))

def _on_error(error: Exception):
def _on_error(error: Exception) -> None:
return handler(OnError(error))

def _on_completed():
def _on_completed() -> None:
return handler(OnCompleted())

return Observer(_on_next, _on_error, _on_completed)
2 changes: 1 addition & 1 deletion reactivex/observable/amb.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def amb_(*sources: Observable[_T]) -> Observable[_T]:

acc: Observable[_T] = never()

def func(previous: Observable[_T], current: Observable[_T]):
def func(previous: Observable[_T], current: Observable[_T]) -> Observable[_T]:
return _.amb(previous)(current)

for source in sources:
Expand Down
6 changes: 3 additions & 3 deletions reactivex/observable/catch.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ def catch_with_iterable_(sources: Iterable[Observable[_T]]) -> Observable[_T]:

def subscribe(
observer: abc.ObserverBase[_T], scheduler_: Optional[abc.SchedulerBase] = None
):
) -> abc.DisposableBase:
_scheduler = scheduler_ or CurrentThreadScheduler.singleton()

subscription = SerialDisposable()
cancelable = SerialDisposable()
last_exception = None
is_disposed = False

def action(scheduler: abc.SchedulerBase, state: Any = None):
def action(scheduler: abc.SchedulerBase, state: Any = None) -> None:
def on_error(exn: Exception) -> None:
nonlocal last_exception
last_exception = exn
Expand Down Expand Up @@ -72,7 +72,7 @@ def on_error(exn: Exception) -> None:

cancelable.disposable = _scheduler.schedule(action)

def dispose():
def dispose() -> None:
nonlocal is_disposed
is_disposed = True

Expand Down
8 changes: 4 additions & 4 deletions reactivex/observable/combinelatest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def subscribe(
is_done = [False] * n
values = [None] * n

def _next(i: Any):
def _next(i: Any) -> None:
has_value[i] = True

if has_value_all[0] or all(has_value):
Expand All @@ -41,22 +41,22 @@ def _next(i: Any):

has_value_all[0] = all(has_value)

def done(i: Any):
def done(i: Any) -> None:
is_done[i] = True
if all(is_done):
observer.on_completed()

subscriptions: List[Optional[SingleAssignmentDisposable]] = [None] * n

def func(i: int):
def func(i: int) -> None:
subscriptions[i] = SingleAssignmentDisposable()

def on_next(x: Any) -> None:
with parent.lock:
values[i] = x
_next(i)

def on_completed():
def on_completed() -> None:
with parent.lock:
done(i)

Expand Down
6 changes: 3 additions & 3 deletions reactivex/observable/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ def subscribe(
cancelable = SerialDisposable()
is_disposed = False

def action(scheduler: abc.SchedulerBase, state: Any = None):
def action(scheduler: abc.SchedulerBase, state: Any = None) -> None:
nonlocal is_disposed
if is_disposed:
return

def on_completed():
def on_completed() -> None:
cancelable.disposable = _scheduler.schedule(action)

try:
Expand All @@ -50,7 +50,7 @@ def on_completed():

cancelable.disposable = _scheduler.schedule(action)

def dispose():
def dispose() -> None:
nonlocal is_disposed
is_disposed = True

Expand Down
8 changes: 5 additions & 3 deletions reactivex/observable/connectableobservable.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ def _subscribe_core(
self,
observer: abc.ObserverBase[_T],
scheduler: Optional[abc.SchedulerBase] = None,
):
) -> abc.DisposableBase:
return self.subject.subscribe(observer, scheduler=scheduler)

def connect(self, scheduler: Optional[abc.SchedulerBase] = None):
def connect(
self, scheduler: Optional[abc.SchedulerBase] = None
) -> Optional[abc.DisposableBase]:
"""Connects the observable."""

if not self.has_subscription:
Expand Down Expand Up @@ -70,7 +72,7 @@ def subscribe(
connectable_subscription[0] = source.connect(scheduler)
is_connected[0] = True

def dispose():
def dispose() -> None:
subscription.dispose()
count[0] -= 1
is_connected[0] = False
Expand Down
2 changes: 1 addition & 1 deletion reactivex/observable/forkjoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def done(i: int) -> None:
cast(SingleAssignmentDisposable, None)
] * n

def _subscribe(i: int):
def _subscribe(i: int) -> None:
subscriptions[i] = SingleAssignmentDisposable()

def on_next(value: Any) -> None:
Expand Down
4 changes: 2 additions & 2 deletions reactivex/observable/fromcallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def from_callback_(
the arguments to the callback as a list.
"""

def function(*args: Any):
def function(*args: Any) -> Observable[Any]:
arguments = list(args)

def subscribe(
observer: abc.ObserverBase[Any],
scheduler: Optional[abc.SchedulerBase] = None,
) -> abc.DisposableBase:
def handler(*args: Any):
def handler(*args: Any) -> None:
results = list(args)
if mapper:
try:
Expand Down
2 changes: 1 addition & 1 deletion reactivex/observable/fromfuture.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def from_future_(future: "Future[_T]") -> Observable[_T]:
def subscribe(
observer: abc.ObserverBase[Any], scheduler: Optional[abc.SchedulerBase] = None
) -> abc.DisposableBase:
def done(future: "Future[_T]"):
def done(future: "Future[_T]") -> None:
try:
value: Any = future.result()
except Exception as ex:
Expand Down
2 changes: 1 addition & 1 deletion reactivex/observable/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def subscribe(
state = initial_state
mad = MultipleAssignmentDisposable()

def action(scheduler: abc.SchedulerBase, state1: Any = None):
def action(scheduler: abc.SchedulerBase, state1: Any = None) -> None:
nonlocal first
nonlocal state

Expand Down
4 changes: 2 additions & 2 deletions reactivex/observable/generatewithrelativetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def generate_with_relative_time_(
def subscribe(
observer: abc.ObserverBase[_TState],
scheduler: Optional[abc.SchedulerBase] = None,
):
) -> abc.DisposableBase:
scheduler = scheduler or TimeoutScheduler.singleton()
mad = MultipleAssignmentDisposable()
state = initial_state
Expand All @@ -48,7 +48,7 @@ def subscribe(
first = True
time: Optional[RelativeTime] = None

def action(scheduler: abc.SchedulerBase, _: Any):
def action(scheduler: abc.SchedulerBase, _: Any) -> None:
nonlocal state
nonlocal has_result
nonlocal result
Expand Down
Loading

0 comments on commit 964fcde

Please sign in to comment.