Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.10", "3.11", "3.12"]

steps:
- name: Check out repository
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ lengths, or whether an identifier is real.

## Requirements

- Python 3.8+
- Python 3.10+
- No third-party packages

## Quick Start
Expand Down Expand Up @@ -95,8 +95,9 @@ space - . / \ ,

After normalization, the value must contain at least one digit and no other
characters. `checksum`, `calculate_check_digit`, and `append_check_digit` raise
`ValueError` for invalid input. `is_valid` catches that error and returns
`False`, which makes it convenient for validation checks.
`ValueError` for malformed strings and `TypeError` for unsupported input types.
`is_valid` catches both errors and returns `False`, which makes it convenient
for validation checks.

## Examples

Expand Down
8 changes: 3 additions & 5 deletions luhn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

from __future__ import annotations

from typing import Union

NumberLike = Union[str, int]
NumberLike = str | int
REMOVE_CHARS = " -./\\,"
DIGIT_CLEANUP_TABLE = str.maketrans("", "", REMOVE_CHARS)


def _digits(value: NumberLike) -> str:
"""Return only digits, allowing spaces, hyphens, dots, slashes, backslashes, and commas as separators."""
if isinstance(value, bool) or not isinstance(value, (str, int)):
raise ValueError("value must be a string or non-boolean integer")
raise TypeError("value must be a string or non-boolean integer")

cleaned = str(value).strip().translate(DIGIT_CLEANUP_TABLE)

Expand Down Expand Up @@ -51,7 +49,7 @@ def is_valid(value: NumberLike) -> bool:

try:
is_valid_checksum = checksum(value) == 0
except ValueError:
except (TypeError, ValueError):
return False

return is_valid_checksum
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[project]
name = "luhn-algorithm"
version = "0.1.0"
requires-python = ">=3.14"
requires-python = ">=3.10"
dependencies = []
2 changes: 1 addition & 1 deletion test_luhn.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_rejects_unsupported_types(self):
self.assertFalse(is_valid(value))

for function in (checksum, calculate_check_digit, append_check_digit):
with self.assertRaises(ValueError):
with self.assertRaises(TypeError):
function(value)


Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading