From 7199df1fbaf913facb75e0d0c0a2dc9253cc34a6 Mon Sep 17 00:00:00 2001 From: Fabien MICHEL Date: Fri, 8 Aug 2025 09:16:56 +0200 Subject: [PATCH 1/3] feat: add stdnum.fr.tva.to_siren to convert french VAT number to SIREN number --- stdnum/fr/tva.py | 17 +++++++++++++++++ tests/test_fr_tva.doctest | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 tests/test_fr_tva.doctest diff --git a/stdnum/fr/tva.py b/stdnum/fr/tva.py index 2638663a..82b5b0f3 100644 --- a/stdnum/fr/tva.py +++ b/stdnum/fr/tva.py @@ -41,6 +41,8 @@ Traceback (most recent call last): ... InvalidFormat: ... +>>> to_siren('Fr 40 303 265 045') +'303 265 045' """ from __future__ import annotations @@ -101,3 +103,18 @@ def is_valid(number: str) -> bool: return bool(validate(number)) except ValidationError: return False + + +def to_siren(number: str) -> str: + """Convert the VAT number to a SIREN number. + + The SIREN number is the 9 last digits of the VAT number. + """ + _siren = [] + digit_count = 0 + for char in reversed(number): + if digit_count < 9: + _siren.insert(0,char) + if isdigits(char): + digit_count += 1 + return ''.join(_siren) diff --git a/tests/test_fr_tva.doctest b/tests/test_fr_tva.doctest new file mode 100644 index 00000000..71885773 --- /dev/null +++ b/tests/test_fr_tva.doctest @@ -0,0 +1,39 @@ +test_fr_tva.doctest - more detailed doctests for the stdnum.fr.tva module + +Copyright (C) 2025 + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA + + +This file contains more detailed doctests for the stdnum.fr.tva module. + +>>> from stdnum.fr import tva +>>> from stdnum.exceptions import * + + +>>> tva.validate('Fr 40 303 265 045') +'40303265045' +>>> tva.validate('23 334 175 221') +'23334175221' +>>> tva.validate('23334175221') +'23334175221' +>>> tva.validate('84 323 140 391') +Traceback (most recent call last): + ... +InvalidChecksum: ... +>>> tva.to_siren('Fr 40 303 265 045') +'303 265 045' + From 3abb1559ef6eefc8a4d1fae879d72e2bb85b3f7a Mon Sep 17 00:00:00 2001 From: Fabien MICHEL Date: Fri, 8 Aug 2025 09:36:26 +0200 Subject: [PATCH 2/3] style: missing space after , --- stdnum/fr/tva.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdnum/fr/tva.py b/stdnum/fr/tva.py index 82b5b0f3..f6137823 100644 --- a/stdnum/fr/tva.py +++ b/stdnum/fr/tva.py @@ -114,7 +114,7 @@ def to_siren(number: str) -> str: digit_count = 0 for char in reversed(number): if digit_count < 9: - _siren.insert(0,char) + _siren.insert(0, char) if isdigits(char): digit_count += 1 return ''.join(_siren) From 456dd323106af4e4198eb886c5022dfb6cbdbc75 Mon Sep 17 00:00:00 2001 From: Fabien MICHEL Date: Fri, 8 Aug 2025 09:38:05 +0200 Subject: [PATCH 3/3] style: add required type hint --- stdnum/fr/tva.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdnum/fr/tva.py b/stdnum/fr/tva.py index f6137823..25c059e8 100644 --- a/stdnum/fr/tva.py +++ b/stdnum/fr/tva.py @@ -110,7 +110,7 @@ def to_siren(number: str) -> str: The SIREN number is the 9 last digits of the VAT number. """ - _siren = [] + _siren: list[str] = [] digit_count = 0 for char in reversed(number): if digit_count < 9: