♻ Introduce type hints

For now we'll only use them to have our IDEs scream at us...
This commit is contained in:
Gina Häußge committed 2019-02-22 11:44:45 +01:00
1 parent 45aa26f13e
commit 0c4f4383e3
2 files changed
+36 -22

No files matched your search

+2 -1
View File
@@ -60,7 +60,8 @@ INSTALL_REQUIRES = [
"emoji>=0.5.1,<0.6",
"monotonic>=1.5,<1.6",
"frozendict>=1.2,<1.3",
"sentry-sdk==0.6.6"
"sentry-sdk==0.6.6",
"typing>=3.6.4,<4"
]
if sys.platform == "darwin":
+34 -21
View File
@@ -1,4 +1,4 @@
# coding=utf-8
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
"""
@@ -24,15 +24,45 @@ import collections
import frozendict
import copy
from typing import Union
try:
import queue
except ImportError:
import Queue as queue
from past.builtins import basestring
from past.builtins import basestring, unicode
logger = logging.getLogger(__name__)
def to_bytes(s_or_u, encoding="utf-8", errors="strict"):
# type: (Union[str, bytes], str, str) -> bytes
"""Make sure ``s_or_u`` is a bytestring."""
if isinstance(s_or_u, unicode):
return s_or_u.encode(encoding, errors=errors)
else:
return s_or_u
def to_unicode(s_or_u, encoding="utf-8", errors="strict"):
# type: (Union[str, bytes], str, str) -> str
"""Make sure ``s_or_u`` is a unicode string."""
if isinstance(s_or_u, bytes):
return s_or_u.decode(encoding, errors=errors)
else:
return s_or_u
def to_native_str(s_or_u):
# type: (Union[str, bytes]) -> Union[str, bytes]
"""Make sure ``s_or_u`` is a 'str'."""
if sys.version_info[0] == 2:
return to_bytes(s_or_u)
else:
return to_unicode(s_or_u)
def warning_decorator_factory(warning_type):
def specific_warning(message, stacklevel=1, since=None, includedoc=None, extenddoc=False):
def decorator(func):
@@ -100,6 +130,8 @@ Returns:
function: The wrapped function with the deprecation warnings in place.
"""
to_str = deprecated("to_str has been renamed to to_bytes", since="1.3.11")(to_bytes)
def get_formatted_size(num):
"""
Formats the given byte count as a human readable rounded size expressed in the most pressing unit among B(ytes),
@@ -433,25 +465,6 @@ def filter_non_ascii(line):
return True
def to_bytes(s_or_u, encoding="utf-8", errors="strict"):
"""Make sure ``s_or_u`` is a bytestring."""
if isinstance(s_or_u, unicode):
return s_or_u.encode(encoding, errors=errors)
else:
return s_or_u
to_str = deprecated("to_str has been renamed to to_bytes", since="1.3.11")(to_bytes)
def to_unicode(s_or_u, encoding="utf-8", errors="strict"):
"""Make sure ``s_or_u`` is a unicode string."""
if isinstance(s_or_u, bytes):
return s_or_u.decode(encoding, errors=errors)
else:
return s_or_u
def chunks(l, n):
"""
Yield successive n-sized chunks from l.