Skip to content

Commit bc97480

Browse files
committed
fix: ISSN checksum returns 11 instead of 0 when weighted sum is divisible by 11
The `_calculate_checksum` method in `InternationalStandardSerialNumber` was missing a final `% 11`, causing it to return 11 (an invalid two-digit value) instead of 0 when the weighted digit sum happened to be exactly divisible by 11. Fixes #238.
1 parent 1d872d3 commit bc97480

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

barcode/isxn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def _calculate_checksum(self):
113113
11
114114
- sum(x * int(y) for x, y in enumerate(reversed(self.issn[:7]), start=2))
115115
% 11
116-
)
116+
) % 11
117117
if tmp == 10:
118118
return "X"
119119

tests/test_checksums.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,16 @@ def test_isbn13_checksum() -> None:
4646
def test_gs1_128_checksum() -> None:
4747
gs1_128 = get_barcode("gs1_128", "00376401856400470087")
4848
assert gs1_128.get_fullcode() == "00376401856400470087"
49+
50+
51+
def test_issn_checksum_zero() -> None:
52+
# When the weighted sum is divisible by 11, the checksum should be 0,
53+
# not 11. Regression test for issue #238.
54+
issn = get_barcode("issn", "6727893")
55+
assert issn.issn == "67278930" # type: ignore[attr-defined]
56+
57+
58+
def test_issn_checksum_x() -> None:
59+
# When (11 - weighted_sum % 11) % 11 == 10, checksum should be "X".
60+
issn = get_barcode("issn", "0000006")
61+
assert issn.issn == "0000006X" # type: ignore[attr-defined]

0 commit comments

Comments
 (0)