From e7066fb671177a99eea48c618ee3b32c96bb8c34 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 17 Apr 2021 19:08:52 +0300 Subject: [PATCH 1/3] Type hints --- emoji/__init__.pyi | 7 ++++++ emoji/core.py | 6 +++--- emoji/core.pyi | 37 ++++++++++++++++++++++++++++++++ emoji/py.typed | 0 emoji/unicode_codes/__init__.pyi | 13 +++++++++++ emoji/unicode_codes/en.pyi | 11 ++++++++++ emoji/unicode_codes/es.pyi | 7 ++++++ emoji/unicode_codes/it.pyi | 7 ++++++ emoji/unicode_codes/pt.pyi | 7 ++++++ emoji/unicode_codes/py.typed | 0 emoji/unicode_codes/types.pyi | 6 ++++++ example/example.py | 4 +++- setup.py | 14 +++++++++--- 13 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 emoji/__init__.pyi create mode 100644 emoji/core.pyi create mode 100644 emoji/py.typed create mode 100644 emoji/unicode_codes/__init__.pyi create mode 100644 emoji/unicode_codes/en.pyi create mode 100644 emoji/unicode_codes/es.pyi create mode 100644 emoji/unicode_codes/it.pyi create mode 100644 emoji/unicode_codes/pt.pyi create mode 100644 emoji/unicode_codes/py.typed create mode 100644 emoji/unicode_codes/types.pyi diff --git a/emoji/__init__.pyi b/emoji/__init__.pyi new file mode 100644 index 00000000..cfdade4d --- /dev/null +++ b/emoji/__init__.pyi @@ -0,0 +1,7 @@ +from .core import * + +__version__: str = ... +__author__: str = ... +__email__: str = ... +__source__: str = ... +__license__: str = ... diff --git a/emoji/core.py b/emoji/core.py index 66e59ff9..00b921d5 100644 --- a/emoji/core.py +++ b/emoji/core.py @@ -33,7 +33,7 @@ def emojize( use_aliases=False, delimiters=(_DEFAULT_DELIMITER, _DEFAULT_DELIMITER), variant=None, - language='en', + language='en' ): """Replace emoji names in a string with unicode codes. @@ -78,7 +78,7 @@ def demojize( string, use_aliases=False, delimiters=(_DEFAULT_DELIMITER, _DEFAULT_DELIMITER), - language='en', + language='en' ): """Replace unicode emoji in a string with emoji shortcodes. Useful for storage. :param string: String contains unicode characters. MUST BE UNICODE. @@ -103,7 +103,7 @@ def replace(match): return re.sub(u'\ufe0f', '', (get_emoji_regexp(language).sub(replace, string))) -def replace_emoji(string, replace='', language='en', ): +def replace_emoji(string, replace='', language='en'): """Replace unicode emoji in a customizable string. """ return re.sub(u'\ufe0f', '', (get_emoji_regexp(language).sub(replace, string))) diff --git a/emoji/core.pyi b/emoji/core.pyi new file mode 100644 index 00000000..b5c4aa13 --- /dev/null +++ b/emoji/core.pyi @@ -0,0 +1,37 @@ +# -*- coding: UTF-8 -*- + +import re +from typing import Tuple, Optional, List, Dict, Union + + + +def emojize( + string: str, + use_aliases: bool = ..., + delimiters: Tuple[str, str] = ..., + variant: Optional[str] = ..., + language: str = ..., +) -> str: ... + + +def demojize( + string: str, + use_aliases: bool = ..., + delimiters: Tuple[str, str] = ..., + language: str = ..., +) -> str: ... + + +def replace_emoji(string: str, replace: str = ..., language: str = ...) -> str: ... + + +def get_emoji_regexp(language: str = ...) -> re.Pattern: ... + + +def emoji_lis(string: str, language: str = ...) -> List[Dict[str, Union[str, int]]]: ... + + +def distinct_emoji_lis(string: str, language: str = ...) -> List[str]: ... + + +def emoji_count(string: str) -> int: ... diff --git a/emoji/py.typed b/emoji/py.typed new file mode 100644 index 00000000..e69de29b diff --git a/emoji/unicode_codes/__init__.pyi b/emoji/unicode_codes/__init__.pyi new file mode 100644 index 00000000..4e398423 --- /dev/null +++ b/emoji/unicode_codes/__init__.pyi @@ -0,0 +1,13 @@ +# -*- coding: utf-8 -*- +from typing import Dict + +from .en import * +from .es import * +from .pt import * +from .it import * + +from .types import _EMOJI_UNICODE_T, _UNICODE_EMOJI_T + +EMOJI_UNICODE: Dict[str, _EMOJI_UNICODE_T] = ... + +UNICODE_EMOJI: Dict[str, _UNICODE_EMOJI_T] = ... diff --git a/emoji/unicode_codes/en.pyi b/emoji/unicode_codes/en.pyi new file mode 100644 index 00000000..d166b9f9 --- /dev/null +++ b/emoji/unicode_codes/en.pyi @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- + +from .types import _EMOJI_UNICODE_T, _EMOJI_ALIAS_UNICODE_T, _UNICODE_EMOJI_T, _UNICODE_EMOJI_ALIAS_T + +EMOJI_UNICODE_ENGLISH: _EMOJI_UNICODE_T = ... + +EMOJI_ALIAS_UNICODE_ENGLISH: _EMOJI_ALIAS_UNICODE_T = ... + +UNICODE_EMOJI_ENGLISH: _UNICODE_EMOJI_T = ... + +UNICODE_EMOJI_ALIAS_ENGLISH: _UNICODE_EMOJI_ALIAS_T = ... diff --git a/emoji/unicode_codes/es.pyi b/emoji/unicode_codes/es.pyi new file mode 100644 index 00000000..8cbaf2a5 --- /dev/null +++ b/emoji/unicode_codes/es.pyi @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- + +from .types import _EMOJI_UNICODE_T, _UNICODE_EMOJI_T + +EMOJI_UNICODE_SPANISH: _EMOJI_UNICODE_T = ... + +UNICODE_EMOJI_SPANISH: _UNICODE_EMOJI_T = ... diff --git a/emoji/unicode_codes/it.pyi b/emoji/unicode_codes/it.pyi new file mode 100644 index 00000000..7d22812b --- /dev/null +++ b/emoji/unicode_codes/it.pyi @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- + +from .types import _EMOJI_UNICODE_T, _UNICODE_EMOJI_T + +EMOJI_UNICODE_ITALIAN: _EMOJI_UNICODE_T = ... + +UNICODE_EMOJI_ITALIAN: _UNICODE_EMOJI_T = ... diff --git a/emoji/unicode_codes/pt.pyi b/emoji/unicode_codes/pt.pyi new file mode 100644 index 00000000..08ad4e2a --- /dev/null +++ b/emoji/unicode_codes/pt.pyi @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- + +from .types import _EMOJI_UNICODE_T, _UNICODE_EMOJI_T + +EMOJI_UNICODE_PORTUGUESE: _EMOJI_UNICODE_T = ... + +UNICODE_EMOJI_PORTUGUESE: _UNICODE_EMOJI_T = ... diff --git a/emoji/unicode_codes/py.typed b/emoji/unicode_codes/py.typed new file mode 100644 index 00000000..e69de29b diff --git a/emoji/unicode_codes/types.pyi b/emoji/unicode_codes/types.pyi new file mode 100644 index 00000000..c8d04288 --- /dev/null +++ b/emoji/unicode_codes/types.pyi @@ -0,0 +1,6 @@ +from typing import Dict + +_EMOJI_UNICODE_T = Dict[str, str] +_EMOJI_ALIAS_UNICODE_T = Dict[str, str] +_UNICODE_EMOJI_T = Dict[str, str] +_UNICODE_EMOJI_ALIAS_T = Dict[str, str] \ No newline at end of file diff --git a/example/example.py b/example/example.py index 5fd08e58..65772802 100644 --- a/example/example.py +++ b/example/example.py @@ -2,5 +2,7 @@ import emoji print(emoji.emojize('Water! :water_wave:')) -print(emoji.demojize(u' at runtime expect NOT to see a picture here, but regular text instead --> 🌊')) # for Python 2.x +print(emoji.demojize( + u' at runtime expect NOT to see a picture here, but regular text instead --> 🌊' +)) # for Python 2.x # print(emoji.demojize('🌊')) # for Python 3.x ## also NO pic to be seen here. it is "emo" and "demo" function i.e. un-emoize. diff --git a/setup.py b/setup.py index 61d05785..66528bc9 100644 --- a/setup.py +++ b/setup.py @@ -4,8 +4,8 @@ """Setup script for emoji.""" - import os +import sys from codecs import open from setuptools import setup @@ -13,7 +13,6 @@ with open('README.rst', encoding='utf-8') as f: readme_content = f.read().strip() - author = email = source = version = None with open(os.path.join('emoji', '__init__.py'), encoding='utf-8') as f: for line in f: @@ -28,7 +27,7 @@ elif None not in (version, author, email, source): break -setup( +setup_args = dict( name='emoji', author=author, author_email=email, @@ -53,6 +52,7 @@ 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', 'Topic :: Multimedia :: Graphics :: Presentation', 'Topic :: Software Development :: Libraries :: Python Modules', + 'Typing :: Typed' ], description="Emoji for Python", keywords=['emoji'], @@ -71,3 +71,11 @@ version=version, zip_safe=True, ) + +if sys.version_info[0] >= 3 and sys.version_info[1] >= 5: + setup_args["package_data"] = { + "emoji": ["py.typed", "*.pyi"], + "emoji.unicode_codes": ["py.typed", "*.pyi"] + } + +setup(**setup_args) From 91530820272615033e47bfe4c4318f4cc9f07c18 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Fri, 5 Aug 2022 01:28:30 +0300 Subject: [PATCH 2/3] Sync with python/typeshed stubs (https://github.com/python/typeshed/tree/a03e8b49491a858bcfcfd8df32c714ca87da8f1f/stubs/emoji) --- emoji/__init__.pyi | 35 +++++++++++++++---- emoji/core.pyi | 56 +++++++++++++++---------------- emoji/unicode_codes/__init__.pyi | 15 +++------ emoji/unicode_codes/data_dict.pyi | 7 ++++ 4 files changed, 67 insertions(+), 46 deletions(-) create mode 100644 emoji/unicode_codes/data_dict.pyi diff --git a/emoji/__init__.pyi b/emoji/__init__.pyi index cfdade4d..b1fbfd52 100644 --- a/emoji/__init__.pyi +++ b/emoji/__init__.pyi @@ -1,7 +1,30 @@ -from .core import * +from .core import ( + demojize as demojize, + distinct_emoji_list as distinct_emoji_list, + emoji_count as emoji_count, + emoji_list as emoji_list, + emojize as emojize, + is_emoji as is_emoji, + replace_emoji as replace_emoji, + version as version, +) +from .unicode_codes import EMOJI_DATA, LANGUAGES, STATUS -__version__: str = ... -__author__: str = ... -__email__: str = ... -__source__: str = ... -__license__: str = ... +__all__ = [ + "emojize", + "demojize", + "emoji_count", + "emoji_list", + "distinct_emoji_list", + "replace_emoji", + "version", + "is_emoji", + "EMOJI_DATA", + "STATUS", + "LANGUAGES", +] +__version__: str +__author__: str +__email__: str +__source__: str +__license__: str \ No newline at end of file diff --git a/emoji/core.pyi b/emoji/core.pyi index b5c4aa13..0f815b0d 100644 --- a/emoji/core.pyi +++ b/emoji/core.pyi @@ -1,37 +1,35 @@ -# -*- coding: UTF-8 -*- +from collections.abc import Callable +from typing_extensions import Literal, TypedDict -import re -from typing import Tuple, Optional, List, Dict, Union +_DEFAULT_DELIMITER: str +class _EmojiLisReturn(TypedDict): + emoji: str + location: int +class _EmojiListReturn(TypedDict): + emoji: str + match_start: int + match_end: int def emojize( - string: str, - use_aliases: bool = ..., - delimiters: Tuple[str, str] = ..., - variant: Optional[str] = ..., - language: str = ..., + string: str, + delimiters: tuple[str, str] = ..., + variant: Literal["text_type", "emoji_type", None] = ..., + language: str = ..., + version: float | None = ..., + handle_version: str | Callable[[str, dict[str, str]], str] | None = ..., ) -> str: ... - - def demojize( - string: str, - use_aliases: bool = ..., - delimiters: Tuple[str, str] = ..., - language: str = ..., + string: str, + delimiters: tuple[str, str] = ..., + language: str = ..., + version: float | None = ..., + handle_version: str | Callable[[str, dict[str, str]], str] | None = ..., ) -> str: ... - - -def replace_emoji(string: str, replace: str = ..., language: str = ...) -> str: ... - - -def get_emoji_regexp(language: str = ...) -> re.Pattern: ... - - -def emoji_lis(string: str, language: str = ...) -> List[Dict[str, Union[str, int]]]: ... - - -def distinct_emoji_lis(string: str, language: str = ...) -> List[str]: ... - - -def emoji_count(string: str) -> int: ... +def replace_emoji(string: str, replace: str | Callable[[str, dict[str, str]], str] = ..., version: float = ...) -> str: ... +def emoji_list(string: str) -> list[_EmojiListReturn]: ... +def distinct_emoji_list(string: str) -> list[str]: ... +def emoji_count(string: str, unique: bool = ...) -> int: ... +def version(string: str) -> float: ... +def is_emoji(string: str) -> bool: ... \ No newline at end of file diff --git a/emoji/unicode_codes/__init__.pyi b/emoji/unicode_codes/__init__.pyi index 4e398423..ef7db186 100644 --- a/emoji/unicode_codes/__init__.pyi +++ b/emoji/unicode_codes/__init__.pyi @@ -1,13 +1,6 @@ -# -*- coding: utf-8 -*- -from typing import Dict +from .data_dict import * -from .en import * -from .es import * -from .pt import * -from .it import * +__all__ = ["get_emoji_unicode_dict", "get_aliases_unicode_dict", "EMOJI_DATA", "STATUS", "LANGUAGES"] -from .types import _EMOJI_UNICODE_T, _UNICODE_EMOJI_T - -EMOJI_UNICODE: Dict[str, _EMOJI_UNICODE_T] = ... - -UNICODE_EMOJI: Dict[str, _UNICODE_EMOJI_T] = ... +def get_emoji_unicode_dict(lang: str) -> dict[str, str]: ... +def get_aliases_unicode_dict() -> dict[str, str]: ... \ No newline at end of file diff --git a/emoji/unicode_codes/data_dict.pyi b/emoji/unicode_codes/data_dict.pyi new file mode 100644 index 00000000..31383338 --- /dev/null +++ b/emoji/unicode_codes/data_dict.pyi @@ -0,0 +1,7 @@ +from typing import Any + +__all__ = ["EMOJI_DATA", "STATUS", "LANGUAGES"] + +STATUS: dict[str, int] +LANGUAGES: list[str] +EMOJI_DATA: dict[str, dict[str, Any]] From 741318520a36120fc82215809d613c5a4a2288ac Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Fri, 5 Aug 2022 01:39:00 +0300 Subject: [PATCH 3/3] Lost 'Typing :: Typed' classifier --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 61d05785..7ba901af 100644 --- a/setup.py +++ b/setup.py @@ -53,6 +53,7 @@ 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', 'Topic :: Multimedia :: Graphics :: Presentation', 'Topic :: Software Development :: Libraries :: Python Modules', + 'Typing :: Typed' ], description="Emoji for Python", keywords=['emoji'],