A Python utility to validate CPF (Brazilian Individual's Taxpayer ID).
Python Support
| Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ | Passing ✔ |
Features
- ✅ Fixed 11-digit CPF: Validates the standard 11-digit Brazilian CPF via the official modulo-11 algorithm
- ✅ Flexible input: Accepts
stror a sequence ofstr; sequence elements are concatenated in order - ✅ Format agnostic: Strips every non-digit character before validation
- ✅ Repeated-digit rejection: All-identical-digit CPFs (e.g.
111.111.111-11,00000000000) are rejected - ✅ Typed input validation: Dedicated
TypeErrorsubclass for invalid input type - ✅ Minimal dependencies:
cpf-dvfor check-digit calculation andlacus.utilsfor type descriptions in error messages - ✅ Dual API style: Object-oriented (
CpfValidator) and functional (cpf_val())
Installation
$ pip install cpf-val
Import
from cpf_val import CpfValidator, cpf_val
Quick start
from cpf_val import CpfValidator
validator = CpfValidator()
validator.is_valid('12345678909') # True
validator.is_valid('123.456.789-09') # True
validator.is_valid('12345678910') # False (invalid check digits)
validator.is_valid('00000000000') # False (repeated digits)
Functional helper:
from cpf_val import cpf_val
cpf_val('12345678909') # True
cpf_val('123.456.789-09') # True
cpf_val('12345678910') # False
Usage
The main entry points are the class CpfValidator and the helper cpf_val().
CpfValidator
-
__init__: Takes no arguments. CPF validation has no configuration options. -
is_valid(cpf_input): Validates a CPF value.Input is normalized to a string (sequences of strings are concatenated). Every non-digit character is then stripped. If the sanitized length is not exactly 11, its base is an all-identical-digit sequence, or the check digits do not match (
CpfCheckDigitsfromcpf-dv), the method returnsFalse— no exception is thrown for validation failure.If the input is not a
stror a sequence ofstr,CpfValidatorInputTypeErroris raised.
from cpf_val import CpfValidator
validator = CpfValidator()
validator.is_valid('123.456.789-09') # True
validator.is_valid('12345678909') # True
validator.is_valid(['123', '456', '789', '09']) # True
validator.is_valid('12345678910') # False (invalid check digits)
validator.is_valid('11111111111') # False (repeated digits)
Functional helper
cpf_val() builds a new CpfValidator and calls is_valid(cpf_input) once. It takes only the input value:
from cpf_val import cpf_val
cpf_val('11144477735') # True
cpf_val('111.444.777-35') # True
cpf_val('11144477736') # False
Input formats
String: Plain digits or a formatted CPF (e.g. 123.456.789-09, 499.784.420-90, 011_258_960_00). Non-digit characters are stripped before validation; the result must be exactly 11 digits.
Sequence of strings: Each element must be a str; values are concatenated (e.g. per digit, grouped segments, or mixed with punctuation). Non-string elements raise CpfValidatorInputTypeError.
from cpf_val import cpf_val
cpf_val(['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '9']) # True
cpf_val(['123.456', '789-09']) # True
Errors & exceptions
This package uses TypeError for invalid input types. Validation failures (wrong length, ineligible base such as repeated digits, invalid check digits) return False and do not throw.
- Wrong input type (not
stror a sequence ofstr):CpfValidatorInputTypeError— extendsCpfValidatorTypeError(extends built-inTypeError). CpfValidatorException: base for non-type (business) errors; currently has no concrete subclass in this package.
from cpf_val import (
CpfValidatorInputTypeError,
CpfValidatorTypeError,
cpf_val,
)
# Input type (e.g. integer not allowed)
try:
cpf_val(12345678909)
except CpfValidatorInputTypeError as e:
print(e) # CPF input must be of type string or string[]. Got integer number.
# Any type error from the package
try:
cpf_val(None)
except CpfValidatorTypeError as e:
pass # handle
API
Exports
All public symbols are available from the cpf_val package:
cpf_val:(cpf_input: CpfInput) -> bool— convenience helper.CpfValidator: Class to validate CPF (no options); acceptsCpfInputinis_valid().CPF_LENGTH:11(constant).CpfInput: Type alias —str | Sequence[str].- Exceptions:
CpfValidatorTypeError,CpfValidatorInputTypeError,CpfValidatorException.
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
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cpf_val-2.0.0.tar.gz.
File metadata
- Download URL: cpf_val-2.0.0.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37e4e104b47e48a4d3692971a4f24ad3133cd2234f22ce0dd1d0fa84d93145c9
|
|
| MD5 |
676da71a1e8f7dbdbbe90167e777f6f2
|
|
| BLAKE2b-256 |
363d94b96ee10e3462732fd5d81d77bb4edf41b5ec16fee8dab68447f78bbad1
|
File details
Details for the file cpf_val-2.0.0-py3-none-any.whl.
File metadata
- Download URL: cpf_val-2.0.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f16a0a6d706e3921280f48a689ba86b3e8e72ee4a9d20c4895441a61950ef3df
|
|
| MD5 |
b5b06cd9f0a444579ea9e1bcb723f911
|
|
| BLAKE2b-256 |
3f13866ca1fb1f18129318c7050d07657667cf0c234a7fe8161ef4dc40b35c5e
|