diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 662ff47..58ef46a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,22 +1,31 @@ repos: +- repo: https://github.com/compilerla/conventional-pre-commit + rev: v1.0.0 + hooks: + - id: conventional-pre-commit + stages: [commit-msg] - repo: https://github.com/ambv/black rev: 19.10b0 hooks: - id: black - args: [--line-length=120] - repo: https://github.com/asottile/reorder_python_imports - rev: v1.7.0 + rev: v2.3.0 hooks: - id: reorder-python-imports - repo: https://gitlab.com/pycqa/flake8 - rev: 3.7.9 + rev: '3.8.3' + hooks: + - id: flake8 + args: ["--config=setup.cfg"] + language_version: python3 +- repo: https://github.com/pycqa/pydocstyle + rev: 6.1.1 hooks: - - id: flake8 -- repo: git://github.com/skorokithakis/pre-commit-mypy - rev: v0.701 + - id: pydocstyle +- repo: https://github.com/pre-commit/mirrors-mypy + rev: v0.782 hooks: - id: mypy - args: [-s] - repo: local hooks: - id: gitchangelog diff --git a/CHANGELOG.md b/CHANGELOG.md index bceb07e..ec52f7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,17 @@ # Changelog +## v1.0.2 (2021-11-08) + +### Features + +* Added basic input type validation to encode and decode (#49) [Ivan Savov] + +### Fixes + +* Use sys.version_info since sys.version returns string that interprets 3.10 as 3.1 in comparison. (#54) [Karthikeyan Singaravelan] + + ## v1.0.1 (2020-03-06) ### Fixes diff --git a/setup.cfg b/setup.cfg index df66f4b..bc3eb4b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,13 +1,12 @@ [flake8] -exclude= -ignore=F403,E128,E126,E123,E121,E203,E265,E501,W503 -import-order-style = smarkets - -[isort] -include_trailing_comma = true -line_length = 120 -force_grid_wrap = 0 -multi_line_output = 3 +exclude = docs/*,setup.py,.* +ignore = F403,E128,E231,E126,E123,E121,E265,E501,N802,N803,N806,C901,D100,D101,D102,D105,W503 [pep8] max-line-length = 120 + +[pydocstyle] +inherit = false +add-ignore = D100,D101,D102,D103,D104,D105,D106,D107,D401 +add-select = D205,D213 +match = .*\.py diff --git a/shortuuid/__init__.py b/shortuuid/__init__.py index 4df494a..bd48d9f 100644 --- a/shortuuid/__init__.py +++ b/shortuuid/__init__.py @@ -1,5 +1,6 @@ # flake8: noqa import pkg_resources + from shortuuid.main import decode from shortuuid.main import encode from shortuuid.main import get_alphabet diff --git a/shortuuid/main.py b/shortuuid/main.py index 8bea173..ba7964b 100644 --- a/shortuuid/main.py +++ b/shortuuid/main.py @@ -1,4 +1,4 @@ -""" Concise UUID generation. """ +"""Concise UUID generation.""" import binascii import math import os @@ -8,6 +8,7 @@ def int_to_string(number, alphabet, padding=None): """ Convert a number to a string, using the given alphabet. + The output has the most significant digit first. """ output = "" @@ -24,6 +25,7 @@ def int_to_string(number, alphabet, padding=None): def string_to_int(string, alphabet): """ Convert a string to a number, using the given alphabet. + The input is assumed to have the most significant digit first. """ number = 0 @@ -36,21 +38,20 @@ def string_to_int(string, alphabet): class ShortUUID(object): def __init__(self, alphabet=None): if alphabet is None: - alphabet = list("23456789ABCDEFGHJKLMNPQRSTUVWXYZ" "abcdefghijkmnopqrstuvwxyz") + alphabet = list( + "23456789ABCDEFGHJKLMNPQRSTUVWXYZ" "abcdefghijkmnopqrstuvwxyz" + ) self.set_alphabet(alphabet) @property def _length(self): - """ - Return the necessary length to fit the entire UUID given - the current alphabet. - """ + """Return the necessary length to fit the entire UUID given the current alphabet.""" return int(math.ceil(math.log(2 ** 128, self._alpha_len))) def encode(self, uuid, pad_length=None): """ - Encode a UUID into a string (LSB first) according to the alphabet + Encode a UUID into a string (LSB first) according to the alphabet. If leftmost (MSB) bits are 0, the string might be shorter. """ @@ -62,14 +63,14 @@ def encode(self, uuid, pad_length=None): def decode(self, string, legacy=False): """ - Decode a string according to the current alphabet into a UUID - Raises ValueError when encountering illegal characters - or a too-long string. + Decode a string according to the current alphabet into a UUID. + + Raises ValueError when encountering illegal characters or a too-long string. If string too short, fills leftmost (MSB) bits with 0. - Pass `legacy=True` if your UUID was encoded with a ShortUUID version - prior to 1.0.0. + Pass `legacy=True` if your UUID was encoded with a ShortUUID version prior to + 1.0.0. """ if not isinstance(string, str): raise ValueError("Input `string` must be a str.") @@ -97,10 +98,7 @@ def uuid(self, name=None, pad_length=None): return self.encode(u, pad_length) def random(self, length=None): - """ - Generate and return a cryptographically-secure short random string - of the specified length. - """ + """Generate and return a cryptographically secure short random string of `length`.""" if length is None: length = self._length @@ -113,7 +111,6 @@ def get_alphabet(self): def set_alphabet(self, alphabet): """Set the alphabet to be used for new UUIDs.""" - # Turn the alphabet into a set and sort it to prevent duplicates # and ensure reproducibility. new_alphabet = list(sorted(set(alphabet))) @@ -124,9 +121,7 @@ def set_alphabet(self, alphabet): raise ValueError("Alphabet with more than " "one unique symbols required.") def encoded_length(self, num_bytes=16): - """ - Returns the string length of the shortened UUID. - """ + """Return the string length of the shortened UUID.""" factor = math.log(256) / math.log(self._alpha_len) return int(math.ceil(factor * num_bytes)) diff --git a/shortuuid/tests.py b/shortuuid/tests.py index c1c4c88..c466bf1 100644 --- a/shortuuid/tests.py +++ b/shortuuid/tests.py @@ -117,7 +117,9 @@ def test_encoded_length(self): su1 = ShortUUID() self.assertEqual(su1.encoded_length(), 22) - base64_alphabet = string.ascii_uppercase + string.ascii_lowercase + string.digits + "+/" + base64_alphabet = ( + string.ascii_uppercase + string.ascii_lowercase + string.digits + "+/" + ) su2 = ShortUUID(base64_alphabet) self.assertEqual(su2.encoded_length(), 22)