Skip to content

Commit e9af704

Browse files
authored
refactor(Taxonomy): for dataset filepath, use mapping instead of overriding enum (#479)
1 parent 0f83128 commit e9af704

2 files changed

Lines changed: 81 additions & 50 deletions

File tree

src/openfoodfacts/types.py

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -891,47 +891,59 @@ class DatasetType(str, enum.Enum):
891891

892892

893893
class TaxonomyType(str, enum.Enum):
894-
dataset_filename: str
895-
896-
category = ("category", "categories.full.json")
897-
ingredient = ("ingredient", "ingredients.full.json")
898-
label = ("label", "labels.full.json")
899-
brand = ("brand", "brands.full.json")
900-
packaging_shape = ("packaging_shape", "packaging_shapes.full.json")
901-
packaging_material = ("packaging_material", "packaging_materials.full.json")
902-
packaging_recycling = ("packaging_recycling", "packaging_recycling.full.json")
903-
country = ("country", "countries.full.json")
904-
store = ("store", "stores.full.json")
905-
nova_group = ("nova_group", "nova_groups.full.json")
906-
packaging = ("packaging", "packaging.full.json")
907-
additive = ("additive", "additives.full.json")
908-
vitamin = ("vitamin", "vitamins.full.json")
909-
mineral = ("mineral", "minerals.full.json")
910-
amino_acid = ("amino_acid", "amino_acids.full.json")
911-
nucleotide = ("nucleotide", "nucleotides.full.json")
912-
allergen = ("allergen", "allergens.full.json")
913-
state = ("state", "states.full.json")
914-
data_quality = ("data_quality", "data_quality.full.json")
915-
origin = ("origin", "origins.full.json")
916-
language = ("language", "languages.full.json")
917-
other_nutritional_substance = (
918-
"other_nutritional_substance",
919-
"other_nutritional_substances.full.json",
920-
)
894+
category = "category"
895+
ingredient = "ingredient"
896+
label = "label"
897+
brand = "brand"
898+
packaging_shape = "packaging_shape"
899+
packaging_material = "packaging_material"
900+
packaging_recycling = "packaging_recycling"
901+
country = "country"
902+
store = "store"
903+
nova_group = "nova_group"
904+
packaging = "packaging"
905+
additive = "additive"
906+
vitamin = "vitamin"
907+
mineral = "mineral"
908+
amino_acid = "amino_acid"
909+
nucleotide = "nucleotide"
910+
allergen = "allergen"
911+
state = "state"
912+
data_quality = "data_quality"
913+
origin = "origin"
914+
language = "language"
915+
other_nutritional_substance = "other_nutritional_substance"
921916

922-
def __new__(cls, value: str, dataset_filename: str):
923-
"""
924-
Override __new__ to allow storing the dataset filename
925-
associated with each taxonomy type.
926-
"""
927-
obj = str.__new__(cls, value)
928-
obj._value_ = value
929-
obj.dataset_filename = dataset_filename
930-
return obj
917+
def __str__(self) -> str:
918+
return self.name
931919

932920
@property
933921
def dataset_path(self) -> str:
934-
return f"data/taxonomies/{self.dataset_filename}"
922+
dataset_filename_mapping = {
923+
self.category: "categories.full.json",
924+
self.ingredient: "ingredients.full.json",
925+
self.label: "labels.full.json",
926+
self.brand: "brands.full.json",
927+
self.packaging_shape: "packaging_shapes.full.json",
928+
self.packaging_material: "packaging_materials.full.json",
929+
self.packaging_recycling: "packaging_recycling.full.json",
930+
self.country: "countries.full.json",
931+
self.store: "stores.full.json",
932+
self.nova_group: "nova_groups.full.json",
933+
self.packaging: "packaging.full.json",
934+
self.additive: "additives.full.json",
935+
self.vitamin: "vitamins.full.json",
936+
self.mineral: "minerals.full.json",
937+
self.amino_acid: "amino_acids.full.json",
938+
self.nucleotide: "nucleotides.full.json",
939+
self.allergen: "allergens.full.json",
940+
self.state: "states.full.json",
941+
self.data_quality: "data_quality.full.json",
942+
self.origin: "origins.full.json",
943+
self.language: "languages.full.json",
944+
self.other_nutritional_substance: "other_nutritional_substances.full.json",
945+
}
946+
return f"data/taxonomies/{dataset_filename_mapping[self]}"
935947

936948

937949
class NutritionV3NutrientAggregated(BaseModel):

tests/unit/test_types.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,49 @@
66
NutritionV3,
77
NutritionV3InputSet,
88
NutritionV3NutrientInput,
9+
TaxonomyType,
910
)
1011

1112

12-
def test_from_product_type_food():
13-
assert Flavor.from_product_type("food") == Flavor.off
13+
class TestFlavor:
14+
def test_str(self):
15+
assert str(Flavor.off) == "off"
1416

17+
def test_from_product_type_food(self):
18+
assert Flavor.from_product_type("food") == Flavor.off
1519

16-
def test_from_product_type_beauty():
17-
assert Flavor.from_product_type("beauty") == Flavor.obf
20+
def test_from_product_type_beauty(self):
21+
assert Flavor.from_product_type("beauty") == Flavor.obf
1822

23+
def test_from_product_type_petfood(self):
24+
assert Flavor.from_product_type("petfood") == Flavor.opff
1925

20-
def test_from_product_type_petfood():
21-
assert Flavor.from_product_type("petfood") == Flavor.opff
26+
def test_from_product_type_product(self):
27+
assert Flavor.from_product_type("product") == Flavor.opf
2228

29+
def test_from_product_type_invalid(self):
30+
with pytest.raises(
31+
ValueError, match="no Flavor matched with product_type 'invalid'"
32+
):
33+
Flavor.from_product_type("invalid")
2334

24-
def test_from_product_type_product():
25-
assert Flavor.from_product_type("product") == Flavor.opf
2635

36+
class TestTaxonomyType:
37+
def test_str(self):
38+
assert str(TaxonomyType.category) == "category"
2739

28-
def test_from_product_type_invalid():
29-
with pytest.raises(
30-
ValueError, match="no Flavor matched with product_type 'invalid'"
31-
):
32-
Flavor.from_product_type("invalid")
40+
def test_type_unknown(self):
41+
with pytest.raises(AttributeError):
42+
_ = TaxonomyType.unknown
43+
44+
def test_dataset_path_known(self):
45+
assert (
46+
TaxonomyType.category.dataset_path == "data/taxonomies/categories.full.json"
47+
)
48+
49+
def test_dataset_path_unknown(self):
50+
with pytest.raises(AttributeError):
51+
_ = TaxonomyType.unknown.dataset_path
3352

3453

3554
NUTRITION_1 = {

0 commit comments

Comments
 (0)