From af3fff85593c48b2992380dc121c124e14983aa1 Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Thu, 12 Jun 2025 14:40:23 +0200 Subject: [PATCH] fix: lookup error for fuzzy search --- pycountry_wrapper/__main__.py | 2 +- pycountry_wrapper/country.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pycountry_wrapper/__main__.py b/pycountry_wrapper/__main__.py index 4b537e4..de19c1a 100644 --- a/pycountry_wrapper/__main__.py +++ b/pycountry_wrapper/__main__.py @@ -5,4 +5,4 @@ from . import Country, EmptyCountry def cli(): print(f"Running {__name__} version {__version__} from __main__.py") - print(EmptyCountry(country="doesn't exist").name) \ No newline at end of file + print(EmptyCountry(country="zwx").name) \ No newline at end of file diff --git a/pycountry_wrapper/country.py b/pycountry_wrapper/country.py index 6762500..ec40e09 100644 --- a/pycountry_wrapper/country.py +++ b/pycountry_wrapper/country.py @@ -58,9 +58,14 @@ class Country: # fuzzy search if enabled if config.allow_fuzzy_search: - found_countries = pycountry.countries.search_fuzzy(country) - if len(found_countries): - return found_countries[0] + # fuzzy search raises lookup error if nothing was found + try: + found_countries = pycountry.countries.search_fuzzy(country) + if len(found_countries): + return found_countries[0] + except LookupError: + pass + @classmethod def search(cls, country: Optional[str]) -> Optional[Country]: