Inflect

raw JSON →
7.5.0 verified Tue May 12 auth: no python install: verified

Inflect.py is a Python library designed to correctly generate plurals, singular nouns, ordinals, indefinite articles (a/an), and word-based representations of numbers. Its functionality is primarily focused on the English language. As of version 7.5.0, it maintains an active development and release cadence, with regular updates addressing features, bug fixes, and compatibility.

pip install inflect
error ModuleNotFoundError: No module named 'inflect'
cause The 'inflect' library is not installed in the current Python environment or the environment is not properly activated.
fix
pip install inflect
error ValueError: `Field` default cannot be set in `Annotated`
cause This error arises from compatibility issues between older versions of 'inflect' (pre-7.2.0) and Pydantic v2.x, as 'inflect' versions 7.2.0 and later replaced Pydantic with Typeguard.
fix
Upgrade 'inflect' to the latest version (7.2.0 or higher) using pip install --upgrade inflect, or downgrade Pydantic to a 1.x version using pip install 'pydantic<2'.
error Incorrect pluralization/singularization output (e.g., p.plural("cats") returns "catses")
cause The 'plural()' methods (like `p.plural()`, `p.plural_noun()`) expect a singular form of the word, and 'singular_noun()' expects a plural form. Passing the incorrect base form results in undefined or incorrect inflections.
fix
Always provide the correct base form (singular for pluralization, plural for singularization) to the respective inflect.engine() methods. For example, use p.plural('cat') for pluralizing 'cat' and p.singular_noun('people') for singularizing 'people'.
error AttributeError: module 'inflect' has no attribute 'plural'
cause Methods like 'plural' are available on an inflect.engine() object, not directly on the inflect module itself.
fix
import inflect p = inflect.engine() word = p.plural("cat")
error TypeError: inflect.engine() takes no arguments
cause The inflect.engine() constructor does not accept any arguments; configuration is done via attributes on the returned engine object.
fix
import inflect p = inflect.engine() # Configuration options are set on the engine instance, e.g.: p.classical(all=False) p.no("and")
breaking Version 7.0.0 removed methods that were renamed in an earlier 0.2.0 release. If your code uses very old aliases, they will cease to function.
fix Refer to the `inflect` documentation for current method names and update your code accordingly. These typically involve methods for singular/plural inflection.
gotcha When using methods like `plural_noun()`, ensure you pass the *singular* form of the word. Similarly, `singular_noun()` expects the *plural* form. Passing the incorrect base form can lead to undefined or incorrect inflections.
fix Always provide the correct base form (singular for pluralization, plural for singularization) to the respective `inflect.engine()` methods.
gotcha Older versions (pre-7.2.0, particularly in the v6.x series) experienced compatibility issues with Pydantic, leading to `ValueError` (e.g., `ValueError: Field default cannot be set in Annotated`). This was often due to Pydantic 1 vs. 2 conflicts. While v7.2.0 replaced Pydantic with Typeguard, users with existing Pydantic installations in their environment might still encounter conflicts if older `inflect` versions are used or if a new conflict arises.
fix Upgrade `inflect` to the latest v7.x version. If issues persist, ensure your environment's Pydantic (if present) and `inflect` versions are compatible, or isolate `inflect` in a separate virtual environment.
gotcha For combining multiple inflections within a single string, the `p.inflect()` string-interpolating method is generally more readable and efficient than concatenating multiple method calls.
fix Consider using `print(p.inflect('num({0}) plural_noun(error) plural_verb(was) detected.'.format(errors)))` instead of `print(p.num(errors), p.plural_noun(' error'), p.plural_verb(' was'), ' detected.')`.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 4.99s 19.1M
3.10 alpine (musl) - - 5.64s 19.1M
3.10 slim (glibc) wheel 1.7s 3.38s 20M
3.10 slim (glibc) - - 3.82s 20M
3.11 alpine (musl) wheel - 5.91s 21.2M
3.11 alpine (musl) - - 6.75s 21.2M
3.11 slim (glibc) wheel 1.8s 5.10s 22M
3.11 slim (glibc) - - 5.01s 22M
3.12 alpine (musl) wheel - 5.63s 13.0M
3.12 alpine (musl) - - 7.00s 13.0M
3.12 slim (glibc) wheel 1.6s 5.40s 14M
3.12 slim (glibc) - - 5.73s 14M
3.13 alpine (musl) wheel - 5.73s 12.8M
3.13 alpine (musl) - - 6.46s 12.7M
3.13 slim (glibc) wheel 1.7s 5.45s 13M
3.13 slim (glibc) - - 5.95s 13M
3.9 alpine (musl) wheel - 4.98s 19.0M
3.9 alpine (musl) - - 5.98s 19.0M
3.9 slim (glibc) wheel 2.2s 4.18s 19M
3.9 slim (glibc) - - 4.74s 19M

This quickstart demonstrates common `inflect` functionalities: pluralizing nouns conditionally, generating ordinal numbers, converting numerals to words, and selecting indefinite articles ('a' or 'an').

import inflect

p = inflect.engine()

# Pluralization
word = 'cat'
count = 5
print(f"The plural of '{word}' is '{p.plural(word)}'")
print(f"I saw {count} {p.plural_noun(word, count)}.")

# Ordinal numbers
number = 21
print(f"The ordinal of {number} is {p.ordinal(number)}")

# Numbers to words
big_number = 1234567
print(f"'{big_number}' in words is '{p.number_to_words(big_number)}'")

# Indefinite articles
print(f"An apple and {p.an('historical object')}")