From 65b0c2b8e682f568b976362f1e11ab7ac9b5a507 Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Thu, 12 Jun 2025 14:37:38 +0200 Subject: [PATCH] feat: playing with new --- pycountry_wrapper/__main__.py | 4 ++-- pycountry_wrapper/country.py | 12 +++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pycountry_wrapper/__main__.py b/pycountry_wrapper/__main__.py index 6ed4f72..4b537e4 100644 --- a/pycountry_wrapper/__main__.py +++ b/pycountry_wrapper/__main__.py @@ -1,8 +1,8 @@ from .__about__ import __name__, __version__ -from . import Country +from . import Country, EmptyCountry def cli(): print(f"Running {__name__} version {__version__} from __main__.py") - print(Country(country="DE").name) \ No newline at end of file + print(EmptyCountry(country="doesn't exist").name) \ No newline at end of file diff --git a/pycountry_wrapper/country.py b/pycountry_wrapper/country.py index b4474c6..6762500 100644 --- a/pycountry_wrapper/country.py +++ b/pycountry_wrapper/country.py @@ -1,6 +1,7 @@ from __future__ import annotations from typing import Optional, Union from functools import wraps +from typing_extensions import Self import pycountry import pycountry.db @@ -22,7 +23,6 @@ class Country: If the country couldn't be found, it raises a ValueError, or creates an empty object. Empty objects return for every attribute None """ - def __init__(self, country: Optional[str] = None, pycountry_object: Optional[pycountry.db.Country] = None) -> None: if pycountry_object is None: # search for the country string instead if the pycountry_object isn't given @@ -54,13 +54,13 @@ class Country: elif len(country) == 3: pycountry_object = pycountry.countries.get(alpha_3=country.upper()) if pycountry_object is not None: - return cls(pycountry_object=pycountry_object) # type: ignore + return pycountry_object # fuzzy search if enabled if config.allow_fuzzy_search: found_countries = pycountry.countries.search_fuzzy(country) if len(found_countries): - return cls(pycountry_object=found_countries[0]) # type: ignore + return found_countries[0] @classmethod def search(cls, country: Optional[str]) -> Optional[Country]: @@ -110,8 +110,10 @@ class EmptyCountry(Country): This will be used if you don't want to use a fallback country but you still want to be able to not have None You can access the same attributes but they will just return None """ - def __init__(self) -> None: - pass + def __new__(cls, country: Optional[str] = None, pycountry_object: Optional[pycountry.db.Country] = None): + print("new", country, pycountry_object) + return super().__new__(cls) + @classmethod def search(cls, country: Optional[str]) -> Union[Country, EmptyCountry]: