Skip to content

feat(Taxonomy): allow fetching taxonomies from other flavors (obf, opff, opf)#466

Merged
raphodn merged 5 commits intodevelopfrom
raphodn/get-taxonomy-oxf
Apr 25, 2026
Merged

feat(Taxonomy): allow fetching taxonomies from other flavors (obf, opff, opf)#466
raphodn merged 5 commits intodevelopfrom
raphodn/get-taxonomy-oxf

Conversation

@raphodn
Copy link
Copy Markdown
Member

@raphodn raphodn commented Mar 29, 2026

Description

Similar to #223, extend the get_taxonomy logic to allow fetching taxonomies for non-off favors.

Solution

  • Update the get_taxonomy() logic
    • new flavor param, defaults to off
  • Update the TAXONOMY_URLS dict
    • add available obf, opff & opf taxonomies
    • fyi obf also as body_parts.full.json but this can be done in a another PR later

Related issue(s)

@raphodn raphodn requested a review from a team as a code owner March 29, 2026 16:33
"""
taxonomy_type = TaxonomyType[taxonomy_type]
filename = f"{taxonomy_type.name}.json"
filename = f"{flavor.name}-{taxonomy_type.name}.json"
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessarily needed, but avoid conflicts if the user has in its cache the same taxonomy from different flavors


def get_taxonomy(
taxonomy_type: TaxonomyType | str,
flavor: Flavor = Flavor.off,
Copy link
Copy Markdown
Member Author

@raphodn raphodn Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kinda frustrating that get_taxonomy() has type THEN flavor
whereas get_dataset() has flavor THEN type ^^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, though we’d want flavor to default to OFF anyway, so it was never going to be the first parameter, since we do not want to set a default taxonomy_type.

Also, when invoking the function, you can always invoke with keyword-only arguments, and then you get to decide the order yourself. ;)

Copy link
Copy Markdown
Member Author

@raphodn raphodn Apr 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but

  • the taxonomy_type does not have a default, so it has to be passed as an argument
  • and in python you can't have defaulted parameters before non-defaulted ones
    so we're stuck ^^
  • though you're saying that when invoking with parameters i can reorder them ? i need to try 👀

but in the end, re-reading this, well dataset_type & taxonomy_type are not really comparable in value, just that their name is similar haha

Copy link
Copy Markdown
Contributor

@Freso Freso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With all the changes here, and esp. with all the repetition, this might be better to make into a function? Maybe something like:

def get_taxonomy_url(taxonomy_type, flavor=off) -> str:
    if flavor = off:
        valid_taxonomies = (categories, …, other_nutritional_substances)
        # these sets of valid taxonomies should maybe be
        # new globals in place of TAXONOMY_URLS, in case
        # other code wants to reference it?
    elif flavor = obf:
        valid_taxonomies = (categories, …, allergens)
    elif …:
        …
    else:
        # Unrecognised flavor
        raise UnrecognisedFlavorError
        # or return None?
    if taxonomy_type in valid_taxonomies:
        return URLBuilder.static(flavor, Environment.org)
            + f"/data/taxonomies/{taxonomy_type}.full.json",
    else:
        raise UnknownTaxonomyTypeError
        # or return None?

Or subclass URLBuilder into a TaxonomyURLBuilder class, overriding the static() method to take flavor, taxonomy_type?

Comment thread src/openfoodfacts/taxonomy.py Outdated
Comment thread src/openfoodfacts/taxonomy.py Outdated
Comment thread src/openfoodfacts/taxonomy.py Outdated

def get_taxonomy(
taxonomy_type: TaxonomyType | str,
flavor: Flavor = Flavor.off,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, though we’d want flavor to default to OFF anyway, so it was never going to be the first parameter, since we do not want to set a default taxonomy_type.

Also, when invoking the function, you can always invoke with keyword-only arguments, and then you get to decide the order yourself. ;)

Comment thread src/openfoodfacts/taxonomy.py Outdated
@sonarqubecloud
Copy link
Copy Markdown

)
+ "/data/taxonomies/other_nutritional_substances.full.json",
Flavor.off: {
TaxonomyType.category: URLBuilder.static(Flavor.off, Environment.org)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @Freso was suggesting, I also think it would be worth creating a get_taxonomy_url function that would take as argument the flavor and the filename, to shorten a bit this dict.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do this in a followup PR 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in #475

@github-project-automation github-project-automation Bot moved this from In progress to Reviewer approved in 🐍 Python SDK - Keep on par with the API Apr 24, 2026
@raphodn raphodn merged commit e5474de into develop Apr 25, 2026
9 checks passed
@raphodn raphodn deleted the raphodn/get-taxonomy-oxf branch April 25, 2026 09:23
@github-project-automation github-project-automation Bot moved this from Reviewer approved to Done in 🐍 Python SDK - Keep on par with the API Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Extend get_taxonomy to other flavors (obf, opf, opff)

3 participants