Skip to content

Commit

Permalink
Add mypy type declarations, fixes ClearcodeHQ#12
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaslowskicc committed Sep 14, 2016
1 parent 0dbede7 commit 8fad4a1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ install:
- "[ ! -d ~/bin ] && mkdir ~/bin || true"
- wget -q -O - https://github.com/yihui/crandalf/raw/master/inst/scripts/install-pandoc
| bash
- if [[ "$TRAVIS_PYTHON_VERSION" == 3* ]]; then pip install mypy-lang; fi
script:
- py.test --verbose --capture=no --showlocals --cov querystringsafe_base64
- pylama
- if [[ "$TRAVIS_PYTHON_VERSION" == 3* ]]; then
mypy querystringsafe_base64.py tests;
mypy --py2 querystringsafe_base64.py tests;
fi
after_success:
- coveralls
matrix:
Expand Down
19 changes: 12 additions & 7 deletions querystringsafe_base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,38 @@
from base64 import urlsafe_b64encode, urlsafe_b64decode

__version__ = '0.2.0'
PY2 = sys.version_info < (3, 0)

if not sys.version_info < (3, 0):
unicode = str


def encode(to_encode):
# type: (bytes) -> unicode
"""
Encode an arbitrary string as a base64 that is safe to put as a URL query value.
Encode an arbitrary bytestring as a base64 that is safe to put as a URL query value.
urllib.quote and urllib.quote_plus do not have any effect on the
result of querystringsafe_base64.encode.
:param (str, bytes) to_encode:
:param bytes to_encode:
:rtype: str
:return: a string that is safe to put as a value in an URL query
string - like base64, except characters ['+', '/', '='] are
replaced with ['-', '_', '.'] consequently
"""
encoded = urlsafe_b64encode(to_encode).replace(b'=', b'.')
if PY2:
if sys.version_info < (3, 0):
return encoded
return encoded.decode()


def decode(encoded):
# type: (unicode) -> bytes
"""
Decode the result of querystringsafe_base64_encode or a regular base64.
.. note ::
As a regular base64 string does not contain dots, replcing dots with
As a regular base64 string does not contain dots, replacing dots with
equal signs does basically noting to it. Also,
base64.urlsafe_b64decode allows to decode both safe and unsafe base64.
Therefore this function may also be used to decode the regular base64.
Expand All @@ -59,6 +63,7 @@ def decode(encoded):
:rtype: str, bytes
:return: decoded string
"""
if PY2:
if sys.version_info < (3, 0):
return urlsafe_b64decode(str(encoded).replace('.', '='))
return urlsafe_b64decode(bytes(encoded, 'ascii').replace(b'.', b'='))
else:
return urlsafe_b64decode(bytes(encoded, 'ascii').replace(b'.', b'='))
7 changes: 4 additions & 3 deletions tests/test_encode_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@

from base64 import b64encode, b64decode, urlsafe_b64decode
import string
try:
import sys
if sys.version_info < (3, 0):
from urllib import quote_plus, unquote_plus
except ImportError:
else:
from urllib.parse import quote_plus, unquote_plus

import pytest
import pytest # type: ignore
import querystringsafe_base64

# We want to test querystringsafe_base64.encode with a string that normally
Expand Down

0 comments on commit 8fad4a1

Please sign in to comment.