A Python utility to calculate check digits on CPF (Brazilian Individual's Taxpayer ID).
Python Support
| Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ |
Features
- ✅ Flexible input: Accepts
strorlist[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 (
TypeErrorvsExceptionsemantics)
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
strorlist[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:9CPF_MAX_LENGTH:11CpfInput: type alias (str | list[str])- Exceptions: see above
Calculation algorithm
The package calculates CPF check digits using the official Brazilian modulo-11 algorithm:
- 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 is0ifremainder > 9, otherwiseremainder. - 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:
- ⭐ Starring the repository
- 🤝 Contributing to the codebase
- 💡 Suggesting new features
- 🐛 Reporting bugs
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
Metadata
Release files for cpf-dv 2.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cpf_dv-2.0.0.tar.gz | 10.8 kB | Without provenance | Details for cpf_dv-2.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cpf_dv-2.0.0-py3-none-any.whl | Python 3 | none | any | Details for cpf_dv-2.0.0-py3-none-any.whl |
Total release size: 19.7 kB
Release files / cpf_dv-2.0.0.tar.gz
| Download URL | cpf_dv-2.0.0.tar.gz |
|---|---|
| Size | 10.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
36c9b4e08cb4bfc0efb9f52d5cf66cd68b278882b9168fae99a52b2af63c5a33
|
|
BLAKE2b-256 checksum How to use checksums |
d9adf2cea83eb8ef00f96736a7b40228c7062a5a401d350e8df1d637e05676a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.6
|
Release files / cpf_dv-2.0.0-py3-none-any.whl
| Download URL | cpf_dv-2.0.0-py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
621f984af3443bc5744d3841dc19cd7ca2ca238e9564e5a9fa35a2afebda9c29
|
|
BLAKE2b-256 checksum How to use checksums |
e2e5dc582e58054a12ac6df5e614c3ff05806fc41aeb155ebae09af396ec5dbe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.6
|
Security for cpf-dv 2.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.