Skip to main content

Utility function to validate CPF (Brazilian personal ID)

Project description

cpf-val for Python

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

Utility function/class to validate CPF (Brazilian personal ID).

CPF validation follows the official rules established by Receita Federal:

  • The CPF must contain exactly 11 digits.
  • All digits cannot be identical (e.g., 111.111.111-11 is considered invalid).
  • The last two digits (10th and 11th) are check digits, calculated from the preceding nine digits.

Python Support

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

Installation

$ pip install cpf-val

Import

# Using class-based resource
from cpf_val import CpfValidator

# Or using function-based one
from cpf_val import cpf_val

Usage

Object-Oriented Usage

validator = CpfValidator()
cpf = '11144477735'

print('Valid' if validator.is_valid(cpf) else 'Invalid')  # returns 'Valid'

cpf = '111.444.777-35'
print('Valid' if validator.is_valid(cpf) else 'Invalid')  # returns 'Valid'

cpf = '11144477736'
print('Valid' if validator.is_valid(cpf) else 'Invalid')  # returns 'Invalid'

Functional programming

The helper function cpf_val() is just a functional abstraction. Internally it creates an instance of CpfValidator and calls the is_valid() method right away.

cpf = '11144477735'

print('Valid' if cpf_val(cpf) else 'Invalid')      # returns 'Valid'

print('Valid' if cpf_val('111.444.777-35') else 'Invalid')  # returns 'Valid'

print('Valid' if cpf_val('11144477736') else 'Invalid')      # returns 'Invalid'

Validation Examples

# Valid CPF numbers
cpf_val('11144477735')      # returns True
cpf_val('111.444.777-35')   # returns True
cpf_val('12345678909')      # returns True

# Invalid CPF numbers
cpf_val('11144477736')      # returns False
cpf_val('12345678901')      # returns False
cpf_val('00000000000')      # returns False
cpf_val('11111111111')      # returns False
cpf_val('123')              # returns False (too short)
cpf_val('')                 # returns False (empty)

Features

  • Format Agnostic: Accepts CPF with or without formatting (dots, dashes)
  • Strict Validation: Validates both check digits according to Brazilian CPF algorithm
  • Type Safety: Built with Python 3.10+ type hints
  • Lightweight: Minimal dependencies, only requires cpf-cd for check digit calculation
  • Dual API: Both object-oriented and functional programming styles supported

API Reference

CpfValidator Class

is_valid(cpf_string: str) -> bool

Validates a CPF string and returns True if valid, False otherwise.

Parameters:

  • cpf_string (str): The CPF to validate (with or without formatting)

Returns:

  • bool: True if the CPF is valid, False otherwise

cpf_val() Function

cpf_val(cpf_string: str) -> bool

Functional wrapper around CpfValidator.is_valid().

Parameters:

  • cpf_string (str): The CPF to validate (with or without formatting)

Returns:

  • bool: True if the CPF is valid, False otherwise

Validation Algorithm

The package validates CPF using the official Brazilian algorithm:

  1. Length Check: Ensures the CPF has exactly 11 digits
  2. Repeated Digits Check: Rejects CPFs with all digits being the same (e.g., 11111111111, 00000000000)
  3. First Check Digit: Calculates and validates the 10th digit
  4. Second Check Digit: Calculates and validates the 11th digit
  5. Format Tolerance: Automatically strips non-numeric characters before validation

Error Handling

The validator is designed to be forgiving with input format but strict with validation:

  • Invalid formats (too short, too long) return False
  • Invalid check digits return False
  • Empty strings return False
  • Non-numeric strings (after stripping formatting) return False
  • CPFs with all digits the same return False

Dependencies

  • Python: >= 3.10
  • cpf-cd: for check digit calculation

Contribution & Support

We welcome contributions! Please see our Contributing Guidelines for details. But 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

Project details


Download files

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

Source Distribution

cpf_val-1.0.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

cpf_val-1.0.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file cpf_val-1.0.0.tar.gz.

File metadata

  • Download URL: cpf_val-1.0.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for cpf_val-1.0.0.tar.gz
Algorithm Hash digest
SHA256 be4fe70f5f080d100c48b4fe1e85e3a7f6e5e17928d600bb28c5f065f3f093d4
MD5 aa1ebc128dff39d5cbcd32be2d485fe7
BLAKE2b-256 2b505eea47d192e12e04dcde9ab08a4de49b5d9db3bfaea283f50890eba8173c

See more details on using hashes here.

File details

Details for the file cpf_val-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: cpf_val-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for cpf_val-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d5e53aa7e562729430910141a5f2928bf7ff5f28aedcb847ae07282b9d8b4826
MD5 034ae50b24975b27b9886e73806b53c2
BLAKE2b-256 1a188634044d1b13ce8c5d3be9dd710b11baea2852ff117ce5623afadd3fa4e7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page