Skip to content

feat: add stdnum.fr.tva.to_siren to convert french VAT number to SIREN #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
17 changes: 17 additions & 0 deletions stdnum/fr/tva.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
Traceback (most recent call last):
...
InvalidFormat: ...
>>> to_siren('Fr 40 303 265 045')
'303 265 045'
"""

from __future__ import annotations
Expand Down Expand Up @@ -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: list[str] = []
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)
39 changes: 39 additions & 0 deletions tests/test_fr_tva.doctest
Original file line number Diff line number Diff line change
@@ -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'

Loading