From 0c4f4383e3c3a0106fd4175206570a0551b9bf26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 21 Feb 2019 15:29:20 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20Introduce=20type=20hints?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For now we'll only use them to have our IDEs scream at us... --- setup.py | 3 +- src/octoprint/util/__init__.py | 55 +++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/setup.py b/setup.py index 625a9f515..3bbc8570c 100644 --- a/setup.py +++ b/setup.py @@ -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": diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index 1f384acc2..89aca7342 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -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.