From bdbbd68a6b3e5a59b3c7ab1d0510d6c29d67ab5a Mon Sep 17 00:00:00 2001 From: Moreal Date: Sun, 18 Sep 2022 14:16:53 +0900 Subject: [PATCH 1/3] Correct BValue --- bencodex/types.py | 10 +++++++--- tox.ini | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/bencodex/types.py b/bencodex/types.py index 75984b9..8759a77 100644 --- a/bencodex/types.py +++ b/bencodex/types.py @@ -1,8 +1,12 @@ -from typing import Any +from typing import Mapping, Sequence, Union -__all__ = 'BValue', +__all__ = ( + "BKey", + "BValue", +) -BValue = Any +BKey = Union[str, bytes] +BValue = Union[BKey, int, None, Mapping[BKey, "BValue"], Sequence["BValue"]] # Mypy currently does not support recursive types. # https://github.com/python/mypy/issues/731 diff --git a/tox.ini b/tox.ini index f749c0c..7458925 100644 --- a/tox.ini +++ b/tox.ini @@ -17,9 +17,9 @@ commands = [testenv:mypy] basepython = python3 deps = - mypy >= 0.971 + mypy >= 0.981 commands = - mypy --install-types --non-interactive -p bencodex + mypy --install-types --non-interactive -p bencodex --enable-recursive-aliases [flake8] exclude = From 3488585f568a5a1a1dde4766d859ccd835dc9f3b Mon Sep 17 00:00:00 2001 From: Moreal Date: Sun, 18 Sep 2022 17:54:37 +0900 Subject: [PATCH 2/3] Correct type checking --- bencodex/writer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bencodex/writer.py b/bencodex/writer.py index 00255b9..0e2e6ed 100644 --- a/bencodex/writer.py +++ b/bencodex/writer.py @@ -48,7 +48,7 @@ def is_unicode(k: Union[bytes, str]) -> bool: return True raise TypeError('dictionary key must be a bytes or str') items = [ - (u, k.encode('utf-8') if u else k, v) + (u, k.encode('utf-8') if u and isinstance(k, str) else k, v) for k, v in value.items() for u in [is_unicode(k)] ] From 92beac86958851125ecfc457cd83523eb74588df Mon Sep 17 00:00:00 2001 From: Moreal Date: Sun, 18 Sep 2022 17:56:16 +0900 Subject: [PATCH 3/3] Update changelog --- CHANGES.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 059e63c..395ad07 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -9,6 +9,8 @@ To be released. - Python 3.5 and 3.6 are no more supported. Instead, Python 3.8, 3.9, and 3.10 are now officially supported. - Added ``show-bencodex``, a CLI program. +- Added ``BKey`` type. +- Fixed ``BValue`` type. Version 1.0.1