Skip to content

Commit

Permalink
Merge pull request #1197 from PokestarFan/add-types-util
Browse files Browse the repository at this point in the history
(#1164) Add types to util.py
  • Loading branch information
bboe committed Dec 29, 2019
2 parents 54422f7 + f05673e commit 5c29aa3
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions praw/models/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Provide helper classes used by other models."""
import random
import time
from typing import Any, Callable, Generator, List, Optional, Set


class BoundedSet:
Expand All @@ -9,17 +10,17 @@ class BoundedSet:
This class does not implement the complete set interface.
"""

def __init__(self, max_items):
def __init__(self, max_items: int):
"""Construct an instance of the BoundedSet."""
self.max_items = max_items
self._fifo = []
self._set = set()

def __contains__(self, item):
def __contains__(self, item: Any) -> bool:
"""Test if the BoundedSet contains item."""
return item in self._set

def add(self, item):
def add(self, item: Any):
"""Add an item to the set discarding the oldest item if necessary."""
if len(self._set) == self.max_items:
self._set.remove(self._fifo.pop(0))
Expand All @@ -30,7 +31,7 @@ def add(self, item):
class ExponentialCounter:
"""A class to provide an exponential counter with jitter."""

def __init__(self, max_counter):
def __init__(self, max_counter: int):
"""Initialize an instance of ExponentialCounter.
:param max_counter: The maximum base value. Note that the computed
Expand All @@ -39,7 +40,7 @@ def __init__(self, max_counter):
self._base = 1
self._max = max_counter

def counter(self):
def counter(self) -> int:
"""Increment the counter and return the current value with jitter."""
max_jitter = self._base / 16.0
value = self._base + random.random() * max_jitter - max_jitter / 2
Expand All @@ -51,7 +52,9 @@ def reset(self):
self._base = 1


def permissions_string(permissions, known_permissions):
def permissions_string(
permissions: Optional[List[str]], known_permissions: Set[str]
) -> str:
"""Return a comma separated string of permission changes.
:param permissions: A list of strings, or ``None``. These strings can
Expand All @@ -77,12 +80,12 @@ def permissions_string(permissions, known_permissions):


def stream_generator(
function,
pause_after=None,
skip_existing=False,
attribute_name="fullname",
**function_kwargs
):
function: Callable[[Any], Any],
pause_after: Optional[int] = None,
skip_existing: bool = False,
attribute_name: str = "fullname",
**function_kwargs: Any
) -> Generator[Any, None, None]:
"""Yield new items from ListingGenerators and ``None`` when paused.
:param function: A callable that returns a ListingGenerator, e.g.
Expand Down

0 comments on commit 5c29aa3

Please sign in to comment.