Skip to content

Commit

Permalink
Add type hints (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Mar 4, 2024
2 parents c78dae1 + 7d39f3f commit 792fa29
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
4 changes: 2 additions & 2 deletions scripts/despacify.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

import json

from em_keyboard import parse_emojis
from em_keyboard import EmojiDict, parse_emojis # type: ignore[import]

INPUT_EMOJILIB_PATH = "src/em_keyboard/emoji-en-US.json"
OUTPUT_EMOJI_PATH = "src/em_keyboard/emojis.json"


def save_emojis(data: dict[str, list[str]], filename: str) -> None:
def save_emojis(data: EmojiDict, filename: str) -> None:
with open(filename, "w") as outfile:
json.dump(data, outfile, indent=None, separators=(",", ":"))
outfile.write("\n")
Expand Down
2 changes: 1 addition & 1 deletion scripts/run_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import subprocess


def run(command: str, with_console: bool = True, line_limit: int = None) -> None:
def run(command: str, with_console: bool = True, line_limit: int | None = None) -> None:
output = subprocess.run(shlex.split(command), capture_output=True, text=True)
print()
if with_console:
Expand Down
17 changes: 10 additions & 7 deletions src/em_keyboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import sys

try:
import pyperclip as copier
import pyperclip as copier # type: ignore[import]
except ImportError:
try:
import xerox as copier
import xerox as copier # type: ignore[import]
except ImportError:
copier = None

Expand All @@ -46,12 +46,15 @@

CUSTOM_EMOJI_PATH = os.path.join(os.path.expanduser("~/.emojis.json"))

# TODO Remove quotes when dropping Python 3.8
EmojiDict = "dict[str, list[str]]"

def parse_emojis(filename=EMOJI_PATH):

def parse_emojis(filename: str | os.PathLike[str] = EMOJI_PATH) -> EmojiDict:
return json.load(open(filename, encoding="utf-8"))


def translate(lookup: dict, code: str) -> list:
def translate(lookup: EmojiDict, code: str) -> list[str] | list[None]:
output = []
if code[0] == ":" and code[-1] == ":":
code = code[1:-1]
Expand All @@ -61,12 +64,12 @@ def translate(lookup: dict, code: str) -> list:
output.append(emoji)
break
else:
output.append(None)
return [None]

return output


def do_find(lookup: dict, term: str) -> list:
def do_find(lookup: EmojiDict, term: str) -> list:
"""Match term against keywords."""
output = []
seen = set()
Expand All @@ -80,7 +83,7 @@ def do_find(lookup: dict, term: str) -> list:
return output


def clean_name(name):
def clean_name(name: str) -> str:
"""Clean emoji name replacing specials chars by underscore"""
special_chars = "[-. ]" # square brackets are part of the regex
return re.sub(special_chars, "_", name).lower()
Expand Down
24 changes: 13 additions & 11 deletions tests/test_em.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import argparse
import random
from unittest.mock import call, patch
from unittest.mock import MagicMock, call, patch

import pytest

from em_keyboard import cli, copier
from em_keyboard import cli, copier # type: ignore[import-untyped]


@pytest.mark.parametrize(
Expand All @@ -20,7 +20,7 @@
)
@patch("em_keyboard.argparse.ArgumentParser.parse_args")
@patch("builtins.print")
def test_star(mock_print, mock_argparse, test_name):
def test_star(mock_print: MagicMock, mock_argparse: MagicMock, test_name: str) -> None:
# Arrange
mock_argparse.return_value = argparse.Namespace(
name=[test_name], no_copy=None, search=False, random=False
Expand All @@ -41,7 +41,7 @@ def test_star(mock_print, mock_argparse, test_name):

@patch("em_keyboard.argparse.ArgumentParser.parse_args")
@patch("builtins.print")
def test_not_found(mock_print, mock_argparse):
def test_not_found(mock_print: MagicMock, mock_argparse: MagicMock) -> None:
# Arrange
mock_argparse.return_value = argparse.Namespace(
name=["xxx"], no_copy=None, search=False, random=False
Expand All @@ -58,7 +58,7 @@ def test_not_found(mock_print, mock_argparse):

@patch("em_keyboard.argparse.ArgumentParser.parse_args")
@patch("builtins.print")
def test_no_copy(mock_print, mock_argparse):
def test_no_copy(mock_print: MagicMock, mock_argparse: MagicMock) -> None:
# Arrange
mock_argparse.return_value = argparse.Namespace(
name=["star"], no_copy=True, search=False, random=False
Expand All @@ -76,7 +76,7 @@ def test_no_copy(mock_print, mock_argparse):

@patch("em_keyboard.argparse.ArgumentParser.parse_args")
@patch("builtins.print")
def test_search_star(mock_print, mock_argparse):
def test_search_star(mock_print: MagicMock, mock_argparse: MagicMock) -> None:
# Arrange
mock_argparse.return_value = argparse.Namespace(
name=["star"], no_copy=None, search=True, random=False
Expand All @@ -100,7 +100,9 @@ def test_search_star(mock_print, mock_argparse):

@patch("em_keyboard.argparse.ArgumentParser.parse_args")
@patch("builtins.print")
def test_search_single_result_is_copied(mock_print, mock_argparse):
def test_search_single_result_is_copied(
mock_print: MagicMock, mock_argparse: MagicMock
) -> None:
# Arrange
mock_argparse.return_value = argparse.Namespace(
name=["ukraine"], no_copy=None, search=True, random=False
Expand All @@ -121,7 +123,7 @@ def test_search_single_result_is_copied(mock_print, mock_argparse):

@patch("em_keyboard.argparse.ArgumentParser.parse_args")
@patch("builtins.print")
def test_search_not_found(mock_print, mock_argparse):
def test_search_not_found(mock_print: MagicMock, mock_argparse: MagicMock) -> None:
# Arrange
mock_argparse.return_value = argparse.Namespace(
name=["twenty_o_clock"], no_copy=None, search=True, random=False
Expand All @@ -139,7 +141,7 @@ def test_search_not_found(mock_print, mock_argparse):

@patch("em_keyboard.argparse.ArgumentParser.parse_args")
@patch("builtins.print")
def test_random(mock_print, mock_argparse):
def test_random(mock_print: MagicMock, mock_argparse: MagicMock) -> None:
# Arrange
mock_argparse.return_value = argparse.Namespace(
name=None, no_copy=None, search=False, random=True
Expand All @@ -161,7 +163,7 @@ def test_random(mock_print, mock_argparse):

@patch("em_keyboard.argparse.ArgumentParser.parse_args")
@patch("builtins.print")
def test_random_no_copy(mock_print, mock_argparse):
def test_random_no_copy(mock_print: MagicMock, mock_argparse: MagicMock) -> None:
# Arrange
mock_argparse.return_value = argparse.Namespace(
name=None, no_copy=True, search=False, random=True
Expand All @@ -180,7 +182,7 @@ def test_random_no_copy(mock_print, mock_argparse):

@patch("em_keyboard.argparse.ArgumentParser.parse_args")
@patch("builtins.print")
def test_no_name(mock_print, mock_argparse):
def test_no_name(mock_print: MagicMock, mock_argparse: MagicMock) -> None:
# Arrange
mock_argparse.return_value = argparse.Namespace(
name=[], no_copy=None, search=True, random=False
Expand Down

0 comments on commit 792fa29

Please sign in to comment.