Skip to main content

cpf-dv for Python

PyPI Version PyPI Downloads Python Version Test Status Last Update Date Project License

🌎 Acessar documentação em português

A Python utility to calculate check digits on CPF (Brazilian Individual's Taxpayer ID).

Python Support

Python 3.10 Python 3.11 Python 3.12 Python 3.13 Python 3.14
Passing ✔ Passing ✔ Passing ✔ Passing ✔ Passing ✔

Features

  • Flexible input: Accepts str or list[str]
  • Format agnostic: Strips non-numeric characters from string input
  • Auto-expansion: Multi-character strings in lists are joined and parsed like a single string
  • Input validation: Rejects ineligible CPFs (9 identical digits in the base — repeated-digit pattern)
  • Lazy evaluation: Check digits are calculated only when accessed (via properties)
  • Caching: Calculated values are cached for subsequent access
  • Type hints: Built with Python 3.10+ type annotations
  • Minimal dependencies: Only lacus.utils
  • Error handling: Specific types for type, length, and invalid CPF scenarios (TypeError vs Exception semantics)

Installation

$ pip install cpf-dv

Quick Start

from cpf_dv import CpfCheckDigits

Basic usage:

check_digits = CpfCheckDigits("054496519")

check_digits.first   # '1'
check_digits.second  # '0'
check_digits.both    # '10'
check_digits.cpf     # '05449651910'

Usage

The main resource of this package is the class CpfCheckDigits. Through an instance, you access CPF check-digit information:

  • __init__: CpfCheckDigits(str | list[str]) — 9–11 digits after sanitization (formatting stripped from strings). Only the first 9 digits are used as the base; if you pass 10 or 11 digits (e.g. a full CPF including prior check digits), digits 10–11 are ignored and the check digits are recalculated.
  • first: First check digit (10th digit of the full CPF). Lazy, cached.
  • second: Second check digit (11th digit of the full CPF). Lazy, cached.
  • both: Both check digits concatenated as a string.
  • cpf: The complete CPF as a string of 11 digits (9 base digits + 2 check digits).

Input formats

The CpfCheckDigits class accepts multiple input formats:

String input: plain digits or formatted CPF (e.g. 054.496.519-10, 123.456.789). Non-numeric characters are removed. Leading zeros are preserved.

List of strings: each element must be a string; values are concatenated and then parsed like a single string (e.g. ["0","5","4",…], ["054","496","519"], ["054496519"]). Non-string elements are not allowed.

# String — plain, formatted, or with existing check digits (only first 9 digits used)
CpfCheckDigits("054496519")
CpfCheckDigits("054.496.519-10")
CpfCheckDigits("05449651910")

# List of strings — single- or multi-character elements
CpfCheckDigits(["0", "5", "4", "4", "9", "6", "5", "1", "9"])
CpfCheckDigits(["054", "496", "519"])
CpfCheckDigits(["054496519"])

Errors & exceptions handling

This package uses TypeError vs Exception semantics: type errors indicate incorrect API use (e.g. wrong type); exceptions indicate invalid or ineligible data (e.g. invalid length or business rules). You can catch specific classes or use the base classes.

  • CpfCheckDigitsTypeError — base class for type errors; extends Python's TypeError
  • CpfCheckDigitsInputTypeError — input is not str or list[str] (or list contains a non-string element)
  • CpfCheckDigitsException — base class for data/flow exceptions; extends Exception
  • CpfCheckDigitsInputLengthException — sanitized length is not 9–11
  • CpfCheckDigitsInputInvalidException — first 9 digits are all identical (repeated-digit pattern)
from cpf_dv import (
    CpfCheckDigits,
    CpfCheckDigitsException,
    CpfCheckDigitsInputInvalidException,
    CpfCheckDigitsInputLengthException,
    CpfCheckDigitsInputTypeError,
)

# Input type (e.g. integer not allowed)
try:
    CpfCheckDigits(12345678901)
except CpfCheckDigitsInputTypeError as e:
    print(e)  # CPF input must be of type string or string[]. Got integer number.

# Length (must be 9–11 digits after sanitization)
try:
    CpfCheckDigits("12345678")
except CpfCheckDigitsInputLengthException as e:
    print(e)  # CPF input "12345678" does not contain 9 to 11 digits. Got 8.

# Invalid (e.g. repeated digits)
try:
    CpfCheckDigits(["999", "999", "999"])
except CpfCheckDigitsInputInvalidException as e:
    print(e)  # CPF input ["999","999","999"] is invalid. Repeated digits are not considered valid.

# Any data exception from the package
try:
    CpfCheckDigits(["999", "999", "999"])
except CpfCheckDigitsException as e:
    print(e)

Other available resources

Import from cpf_dv:

  • CPF_MIN_LENGTH: 9
  • CPF_MAX_LENGTH: 11
  • CpfInput: type alias (str | list[str])
  • Exceptions: see above

Calculation algorithm

The package calculates CPF check digits using the official Brazilian modulo-11 algorithm:

  1. First check digit (10th position): apply to the first 9 base digits; weights 10, 9, 8, 7, 6, 5, 4, 3, 2 (left to right); let remainder = 11 - (sum(digit × weight) % 11). The digit is 0 if remainder > 9, otherwise remainder.
  2. Second check digit (11th position): apply to the first 9 base digits plus the first check digit; weights 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 (left to right); same formula for remainder.

Contribution & Support

We welcome contributions! Please see our Contributing Guidelines for details. If you find this project helpful, please consider:

License

This project is licensed under the MIT License — see the LICENSE file for details.

Changelog

See CHANGELOG for a list of changes and version history.


Made with ❤️ by Lacus Solutions

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cpf_dv-2.0.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cpf_dv-2.0.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file cpf_dv-2.0.0.tar.gz.

File metadata

  • Download URL: cpf_dv-2.0.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for cpf_dv-2.0.0.tar.gz
Algorithm Hash digest
SHA256 36c9b4e08cb4bfc0efb9f52d5cf66cd68b278882b9168fae99a52b2af63c5a33
MD5 e97bcfd317135141308c96b9b95da04b
BLAKE2b-256 d9adf2cea83eb8ef00f96736a7b40228c7062a5a401d350e8df1d637e05676a9

See more details on using hashes here.

File details

Details for the file cpf_dv-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: cpf_dv-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for cpf_dv-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 621f984af3443bc5744d3841dc19cd7ca2ca238e9564e5a9fa35a2afebda9c29
MD5 51595aad450ec6f94c490769023b5bdc
BLAKE2b-256 e2e5dc582e58054a12ac6df5e614c3ff05806fc41aeb155ebae09af396ec5dbe

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page