Skip to content

Commit

Permalink
style: update to python 3.8 features
Browse files Browse the repository at this point in the history
modernize type hints
apply isort
  • Loading branch information
matejcik committed Jan 5, 2024
1 parent 0bd9d8d commit 264145f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/mnemonic/mnemonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
from __future__ import annotations

import hashlib
import hmac
import itertools
import os
import secrets
import typing as t
import unicodedata
from typing import AnyStr, List, Optional, TypeVar, Union

_T = TypeVar("_T")
PBKDF2_ROUNDS = 2048


Expand All @@ -53,7 +53,7 @@ def b58encode(v: bytes) -> str:


class Mnemonic(object):
def __init__(self, language: str = "english", wordlist: Optional[List[str]] = None):
def __init__(self, language: str = "english", wordlist: list[str] | None = None):
self.radix = 2048
self.language = language

Expand All @@ -73,15 +73,15 @@ def __init__(self, language: str = "english", wordlist: Optional[List[str]] = No
self.delimiter = "\u3000" if language == "japanese" else " "

@classmethod
def list_languages(cls) -> List[str]:
def list_languages(cls) -> list[str]:
return [
f.split(".")[0]
for f in os.listdir(os.path.join(os.path.dirname(__file__), "wordlist"))
if f.endswith(".txt")
]

@staticmethod
def normalize_string(txt: AnyStr) -> str:
def normalize_string(txt: t.AnyStr) -> str:
if isinstance(txt, bytes):
utxt = txt.decode("utf8")
elif isinstance(txt, str):
Expand Down Expand Up @@ -129,7 +129,7 @@ def generate(self, strength: int = 128) -> str:
return self.to_mnemonic(secrets.token_bytes(strength // 8))

# Adapted from <http://tinyurl.com/oxmn476>
def to_entropy(self, words: Union[List[str], str]) -> bytearray:
def to_entropy(self, words: list[str] | str) -> bytearray:
if not isinstance(words, list):
words = words.split(" ")
if len(words) not in [12, 15, 18, 21, 24]:
Expand Down
1 change: 1 addition & 0 deletions tools/generate_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from random import choice, seed

from bip32utils import BIP32Key

from mnemonic import Mnemonic


Expand Down

0 comments on commit 264145f

Please sign in to comment.