This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
Type Hints for aioredis #584
Merged
popravich
merged 3 commits into
aio-libs-abandoned:master
from
butuzov:typings/parser.py
Apr 20, 2019
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
from .errors import ProtocolError, ReplyError | ||
from typing import Optional, Generator, Callable, Iterator | ||
|
||
__all__ = [ | ||
'Reader', 'PyReader', | ||
] | ||
|
||
|
||
class PyReader: | ||
"""Pure-Python Redis protocol parser that follows hiredis.Reader | ||
interface (except setmaxbuf/getmaxbuf). | ||
""" | ||
def __init__(self, protocolError=ProtocolError, replyError=ReplyError, | ||
encoding=None): | ||
def __init__(self, protocolError: Callable = ProtocolError, | ||
replyError: Callable = ReplyError, | ||
encoding: Optional[str] = None): | ||
if not callable(protocolError): | ||
raise TypeError("Expected a callable") | ||
if not callable(replyError): | ||
raise TypeError("Expected a callable") | ||
self._parser = Parser(protocolError, replyError, encoding) | ||
|
||
def feed(self, data, o=0, l=-1): | ||
def feed(self, data, o: int = 0, l: int = -1): | ||
"""Feed data to parser.""" | ||
if l == -1: | ||
l = len(data) - o | ||
|
@@ -35,31 +36,33 @@ def gets(self): | |
""" | ||
return self._parser.parse_one() | ||
|
||
def setmaxbuf(self, size): | ||
def setmaxbuf(self, size: Optional[int] ) -> None: | ||
"""No-op.""" | ||
pass | ||
|
||
def getmaxbuf(self): | ||
def getmaxbuf(self) -> int: | ||
"""No-op.""" | ||
return 0 | ||
|
||
|
||
class Parser: | ||
def __init__(self, protocolError, replyError, encoding): | ||
self.buf = bytearray() | ||
self.pos = 0 | ||
self.protocolError = protocolError | ||
self.replyError = replyError | ||
self.encoding = encoding | ||
def __init__(self, protocolError: Callable, | ||
replyError: Callable, encoding: Optional[str] ): | ||
|
||
self.buf: bytearray = bytearray() | ||
self.pos: int = 0 | ||
self.protocolError: Callable = protocolError | ||
self.replyError: Callable = replyError | ||
self.encoding: Optional[str] = encoding | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wont work in Python 3.5 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean variable annotations |
||
self._err = None | ||
self._gen = None | ||
self._gen: Optional[Generator] = None | ||
|
||
def waitsome(self, size): | ||
def waitsome(self, size: int) -> Iterator[bool]: | ||
# keep yielding false until at least `size` bytes added to buf. | ||
while len(self.buf) < self.pos+size: | ||
yield False | ||
|
||
def waitany(self): | ||
def waitany(self) -> Iterator[bool]: | ||
yield from self.waitsome(len(self.buf) + 1) | ||
|
||
def readone(self): | ||
|
@@ -69,7 +72,7 @@ def readone(self): | |
self.pos += 1 | ||
return val | ||
|
||
def readline(self, size=None): | ||
def readline(self, size: Optional[int] = None): | ||
if size is not None: | ||
if len(self.buf) < size + 2 + self.pos: | ||
yield from self.waitsome(size + 2) | ||
|
@@ -96,7 +99,7 @@ def error(self, msg): | |
self._err = self.protocolError(msg) | ||
return self._err | ||
|
||
def parse(self, is_bulk=False): | ||
def parse(self, is_bulk: bool = False): | ||
if self._err is not None: | ||
raise self._err | ||
ctl = yield from self.readone() | ||
|
@@ -156,7 +159,6 @@ def parse_one(self): | |
else: | ||
return False | ||
|
||
|
||
try: | ||
import hiredis | ||
Reader = hiredis.Reader | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ pytest==4.1.1 | |
pytest-cov==2.6.1 | ||
pytest-xdist==1.26.0 | ||
async-timeout==3.0.1 | ||
mypy |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes total sense. Added 3.7 version for Travis-CI job.