Skip to content

Commit e935827

Browse files
committed
Fix problem with municipalities that have the same slugified name: e.g., Paternò (CT) vs Paterno (PZ). #246
1 parent f73775e commit e935827

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

src/codicefiscale/codicefiscale.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,18 @@ def _get_birthplace(
145145
birthplace: str,
146146
birthdate: datetime | str | None = None,
147147
) -> dict[str, dict[str, Any]] | None:
148+
birthplace_unicode_slug = slugify(birthplace, allow_unicode=True)
148149
birthplace_slug = slugify(birthplace)
149150
birthplace_code = birthplace_slug.upper()
150151
birthplaces_options = _DATA["municipalities"].get(
151-
birthplace_slug,
152-
_DATA["countries"].get(
152+
birthplace_unicode_slug,
153+
_DATA["municipalities"].get(
153154
birthplace_slug,
154-
_DATA["codes"].get(
155-
birthplace_code,
155+
_DATA["countries"].get(
156+
birthplace_slug,
157+
_DATA["codes"].get(
158+
birthplace_code,
159+
),
156160
),
157161
),
158162
)

src/codicefiscale/data.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Any
77

88
import fsutil
9+
from slugify import slugify
910

1011

1112
def get_data_basedir() -> str:
@@ -46,7 +47,8 @@ def get_indexed_data() -> dict[
4647
for municipality in municipalities:
4748
code = municipality["code"]
4849
province = municipality["province"].lower()
49-
names = municipality["name_slugs"]
50+
municipality_unicode_slug = slugify(municipality["name"], allow_unicode=True)
51+
names = [municipality_unicode_slug] + municipality["name_slugs"]
5052
for name in names:
5153
name_and_province = f"{name}-{province}"
5254
data["municipalities"].setdefault(name, [])

tests/issues/test_issue_0246.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import datetime
2+
3+
from codicefiscale import codicefiscale
4+
5+
6+
def test_issue_0246():
7+
"""
8+
Test problem with municipalities that have the same or similar name
9+
that once slugified return the same string: e.g., Paternò (CT) vs Paterno (PZ)
10+
https://github.com/fabiocaccamo/python-codicefiscale/issues/246
11+
"""
12+
13+
code = codicefiscale.encode(
14+
lastname="DE GREGORI",
15+
firstname="FRANCESCO",
16+
gender="M",
17+
birthdate="31/12/1984",
18+
birthplace="PATERNÒ",
19+
)
20+
assert code == "DGRFNC84T31G371E"
21+
22+
code_data = codicefiscale.decode(code)
23+
code_data.pop("omocodes", None)
24+
expected_code_data = {
25+
"code": "DGRFNC84T31G371E",
26+
"gender": "M",
27+
"birthdate": datetime.datetime(1984, 12, 31, 0, 0),
28+
"birthplace": {
29+
"active": False,
30+
"code": "G371",
31+
"date_created": "1861-03-17T00:00:00",
32+
"date_deleted": "1985-05-16T00:00:00",
33+
"name": "Paternò",
34+
"name_alt": "",
35+
"name_alt_trans": "",
36+
"name_slugs": ["paterno"],
37+
"name_trans": "Paterno'",
38+
"province": "CT",
39+
},
40+
"raw": {
41+
"code": "DGRFNC84T31G371E",
42+
"lastname": "DGR",
43+
"firstname": "FNC",
44+
"birthdate": "84T31",
45+
"birthdate_year": "84",
46+
"birthdate_month": "T",
47+
"birthdate_day": "31",
48+
"birthplace": "G371",
49+
"cin": "E",
50+
},
51+
}
52+
assert code_data == expected_code_data

0 commit comments

Comments
 (0)