Skip to main content

cpf-val 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 validate 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

  • ✅ Fixed 11-digit CPF: Validates the standard 11-digit Brazilian CPF via the official modulo-11 algorithm
  • ✅ Flexible input: Accepts str or a sequence of str; 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 TypeError subclass for invalid input type
  • ✅ Minimal dependencies: cpf-dv for check-digit calculation and lacus.utils for 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 (CpfCheckDigits from cpf-dv), the method returns False — no exception is thrown for validation failure.

    If the input is not a str or a sequence of str, CpfValidatorInputTypeError is 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 str or a sequence of str): CpfValidatorInputTypeError — extends CpfValidatorTypeError (extends built-in TypeError).
  • 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); accepts CpfInput in is_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:

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-val 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cpf-val 2.0.0
File Size Uploaded Provenance Actions
cpf_val-2.0.0.tar.gz 9.2 kB Without provenance Details for cpf_val-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cpf-val 2.0.0
File Interpreter ABI Platform Actions
cpf_val-2.0.0-py3-none-any.whl Python 3 none any Details for cpf_val-2.0.0-py3-none-any.whl

Total release size: 17.0 kB

Release files / cpf_val-2.0.0.tar.gz

Download URL cpf_val-2.0.0.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
37e4e104b47e48a4d3692971a4f24ad3133cd2234f22ce0dd1d0fa84d93145c9
BLAKE2b-256 checksum
How to use checksums
363d94b96ee10e3462732fd5d81d77bb4edf41b5ec16fee8dab68447f78bbad1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / cpf_val-2.0.0-py3-none-any.whl

Download URL cpf_val-2.0.0-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f16a0a6d706e3921280f48a689ba86b3e8e72ee4a9d20c4895441a61950ef3df
BLAKE2b-256 checksum
How to use checksums
3f13866ca1fb1f18129318c7050d07657667cf0c234a7fe8161ef4dc40b35c5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Security for cpf-val 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.

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