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-dv 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-dv: 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.1.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.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for cpf_val-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4c1eaaa7b5f981ffe69569010109a9f32b43153f2003f910a2ab125b91044df7
MD5 5a4d20575643a2f1f4b0ccdb5652fff3
BLAKE2b-256 79d51fcde0113f732e9abce8fcef1c60f6ca2d330291c5a619eaa7024b3350be

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for cpf_val-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9b66b3e2b0092db55d73e64784c8254ca3876435500017fbc42b925ed602105c
MD5 d356f8b804402c6b786b8062b105e13c
BLAKE2b-256 d6dc70cdb912a2bf7fc815d373777c53359b3d64496d7edc90f516e80debe052

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