Fix typing issues and improve typing completeness across the library

Co-authored-by: Danny <Rapptz@users.noreply.github.com>
Co-authored-by: Josh <josh.ja.butt@gmail.com>
This commit is contained in:
Stocker
2022-03-13 23:52:10 -04:00
committed by GitHub
parent 603681940f
commit 5aa696ccfa
66 changed files with 1071 additions and 802 deletions

View File

@ -21,6 +21,11 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
from typing import Optional
from .errors import UnexpectedQuoteError, InvalidEndOfQuotedStringError, ExpectedClosingQuoteError
# map from opening quotes to closing quotes
@ -47,24 +52,24 @@ _all_quotes = set(_quotes.keys()) | set(_quotes.values())
class StringView:
def __init__(self, buffer):
self.index = 0
self.buffer = buffer
self.end = len(buffer)
def __init__(self, buffer: str) -> None:
self.index: int = 0
self.buffer: str = buffer
self.end: int = len(buffer)
self.previous = 0
@property
def current(self):
def current(self) -> Optional[str]:
return None if self.eof else self.buffer[self.index]
@property
def eof(self):
def eof(self) -> bool:
return self.index >= self.end
def undo(self):
def undo(self) -> None:
self.index = self.previous
def skip_ws(self):
def skip_ws(self) -> bool:
pos = 0
while not self.eof:
try:
@ -79,7 +84,7 @@ class StringView:
self.index += pos
return self.previous != self.index
def skip_string(self, string):
def skip_string(self, string: str) -> bool:
strlen = len(string)
if self.buffer[self.index : self.index + strlen] == string:
self.previous = self.index
@ -87,19 +92,19 @@ class StringView:
return True
return False
def read_rest(self):
def read_rest(self) -> str:
result = self.buffer[self.index :]
self.previous = self.index
self.index = self.end
return result
def read(self, n):
def read(self, n: int) -> str:
result = self.buffer[self.index : self.index + n]
self.previous = self.index
self.index += n
return result
def get(self):
def get(self) -> Optional[str]:
try:
result = self.buffer[self.index + 1]
except IndexError:
@ -109,7 +114,7 @@ class StringView:
self.index += 1
return result
def get_word(self):
def get_word(self) -> str:
pos = 0
while not self.eof:
try:
@ -119,12 +124,12 @@ class StringView:
pos += 1
except IndexError:
break
self.previous = self.index
self.previous: int = self.index
result = self.buffer[self.index : self.index + pos]
self.index += pos
return result
def get_quoted_word(self):
def get_quoted_word(self) -> Optional[str]:
current = self.current
if current is None:
return None
@ -187,5 +192,5 @@ class StringView:
result.append(current)
def __repr__(self):
def __repr__(self) -> str:
return f'<StringView pos: {self.index} prev: {self.previous} end: {self.end} eof: {self.eof}>'