Typing Stubs for python-slugify

8.0.2.20240310 · active · verified Thu Apr 09

This package provides typing stubs for the `python-slugify` library, enabling static type checkers like MyPy or Pyright to validate usage of `python-slugify`. It does not contain any executable code itself. The current version is 8.0.2.20240310. As part of the typeshed project, its releases are typically tied to updates in the upstream `python-slugify` library or typeshed's own update cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `python-slugify` with type checking enabled by `types-python-slugify`. It shows basic slug generation and creating a custom `Slugify` instance. To use these stubs, both `python-slugify` and `types-python-slugify` must be installed. A type checker like MyPy will then automatically leverage these stubs for static analysis.

import os
# Install both packages: pip install python-slugify types-python-slugify

from slugify import slugify, Slugify

def process_title(title: str) -> str:
    """Converts a title to a URL-friendly slug."""
    # Type checkers (like MyPy) will use types-python-slugify here
    return slugify(title)

def create_custom_slugifier() -> Slugify:
    """Creates a custom slugifier instance."""
    # Example of using the Slugify class, also type-checked
    return Slugify(to_lower=True, separator='_')

# --- Example Usage ---

# Basic slugification
original_text = "My Awesome Article Title!"
slugged_text = process_title(original_text)
print(f"Original: '{original_text}'\nSlugged:  '{slugged_text}'")

# Using a custom slugifier
custom_slg = create_custom_slugifier()
custom_slugged = custom_slg("Another-Example String")
print(f"Custom slugged: '{custom_slugged}'")

# To verify type checking, run `mypy your_script_name.py`
# For example, calling process_title(123) would raise a type error.

view raw JSON →