Typing Stubs for Emoji
`types-emoji` provides static type checking definitions for the popular `emoji` Python library. It enables type checkers like MyPy to understand the types used in `emoji.emojize()`, `emoji.demojize()`, and other functions, improving code reliability and developer experience. This package is part of the `typeshed` project, which maintains external type annotations for numerous Python libraries and the standard library. `types-emoji` follows the release cadence of `typeshed`, which is often independent of the runtime `emoji` library's release schedule.
Warnings
- gotcha `types-emoji` provides only type hints for static analysis. To execute Python code that uses emoji functionalities, you must also install the actual `emoji` runtime library (e.g., `pip install emoji`).
- breaking As `types-emoji` is part of `typeshed`, any version bump might introduce changes to type annotations that could cause your code to fail type checking, even if the underlying `emoji` runtime library remains unchanged. This can happen due to more precise type definitions or bug fixes in stubs.
- gotcha Emoji shortcodes in `emoji.emojize()` and `emoji.demojize()` default to English. If you intend to use shortcodes from other languages (e.g., Spanish, Portuguese, French) or enable a broader set of aliases, you must explicitly provide the `language` argument (e.g., `language='es'` or `language='alias'`).
- gotcha Some emojis have different Unicode presentation styles (e.g., text vs. emoji presentation). For explicit control over the output, `emoji.emojize()` supports a `variant` argument (e.g., `variant='emoji_type'`).
Install
-
pip install types-emoji -
pip install emoji
Imports
- emoji
import emoji
- emojize
from emoji import emojize
- demojize
from emoji import demojize
Quickstart
import emoji
import os
# Example 1: Convert shortcode to emoji
text_with_emoji = emoji.emojize("Python is fun :red_heart:")
print(f"Emojized: {text_with_emoji}")
# Example 2: Convert emoji to shortcode
text_with_shortcode = emoji.demojize("Python is fun ❤️")
print(f"Demojized: {text_with_shortcode}")
# Example 3: Check if a string contains an emoji
has_emoji = emoji.is_emoji("👍")
print(f"Is '👍' an emoji? {has_emoji}")
# Example 4: Using language-specific shortcodes (e.g., Spanish)
spanish_text_emojized = emoji.emojize("Python es :pulgar_hacia_arriba:", language='es')
print(f"Spanish emojized: {spanish_text_emojized}")
spanish_text_demojized = emoji.demojize("Python es 👍", language='es')
print(f"Spanish demojized: {spanish_text_demojized}")
# Example 5: Using variant for emoji presentation (if supported)
heart_emoji_type = emoji.emojize("Python is fun :red_heart:", variant="emoji_type")
print(f"Heart emoji type: {heart_emoji_type}")