From 101c362a69c8f4a44e87cde51ebfa2e5e99ad6b8 Mon Sep 17 00:00:00 2001 From: jh Date: Mon, 10 Oct 2022 20:03:32 +0200 Subject: [PATCH 1/3] refactor: Add "Coutry" to the importable classes This enables proper typehinting when using this library. --- restcountries/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/restcountries/__init__.py b/restcountries/__init__.py index e67ef4c..6329e13 100644 --- a/restcountries/__init__.py +++ b/restcountries/__init__.py @@ -1,4 +1,4 @@ # simpler import as described in the readme -from restcountries.base import RestCountryApiV2 +from restcountries.base import Country, RestCountryApiV2 __version__ = "2.0.0" From a70b529e14da158a080de0b40aaf3ea2ef3260fe Mon Sep 17 00:00:00 2001 From: jh Date: Mon, 10 Oct 2022 20:16:39 +0200 Subject: [PATCH 2/3] refactor: Make _get_country_list return a list of countries regardless of number of found countries This is what the naming of the method suggests it is supposed to do and without it, typechecking fails or requires painful workarounds. . Code that uses a "get_countries_" method and expects a single Country object as reurn type will break. Only the get_country_by_country_code has been adapted to return the first element of the list of countries that _get_country_list returns. --- restcountries/base.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/restcountries/base.py b/restcountries/base.py index 84aba06..a673f30 100644 --- a/restcountries/base.py +++ b/restcountries/base.py @@ -1,6 +1,7 @@ -import requests import json +import requests + class RestCountryApiV2: BASE_URI = "https://restcountries.com/v2" @@ -36,7 +37,7 @@ def _get_country_list(cls, resource, term="", filters=None): response = requests.get(uri) if response.status_code == 200: - result_list = [] + result_list: list[Country] = [] data = json.loads(response.text) # parse json to dict if type(data) == list: for ( @@ -47,7 +48,7 @@ def _get_country_list(cls, resource, term="", filters=None): country = Country(country_data) result_list.append(country) else: - return Country(data) + result_list.append(Country(data)) return result_list elif response.status_code == 404: raise requests.exceptions.InvalidURL @@ -58,7 +59,7 @@ def _get_country_list(cls, resource, term="", filters=None): def get_all(cls, filters=None): """Returns all countries provided by restcountries.eu. - :param filters - a list of fields to filter the output of the request to include only the specified fields. + :param filters - a list of fields to filter the output of the request to include only the specified fields. """ resource = "/all" return cls._get_country_list(resource, filters=filters) @@ -106,7 +107,7 @@ def get_country_by_country_code(cls, alpha, filters=None): You can look those up at wikipedia: https://en.wikipedia.org/wiki/ISO_3166-1 """ resource = "/alpha" - return cls._get_country_list(resource, alpha, filters=filters) + return cls._get_country_list(resource, alpha, filters=filters)[0] @classmethod def get_countries_by_country_codes(cls, codes, filters=None): From 8cd2d174b3a102a8b40ec21b15b2bcabd54e8b80 Mon Sep 17 00:00:00 2001 From: jh Date: Mon, 10 Oct 2022 20:18:30 +0200 Subject: [PATCH 3/3] fix: Remove redundant __str__ method There were two __str__ methods defined, the first seemed the less sophisticated one. --- restcountries/base.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/restcountries/base.py b/restcountries/base.py index a673f30..e150959 100644 --- a/restcountries/base.py +++ b/restcountries/base.py @@ -168,9 +168,6 @@ def get_countries_by_capital(cls, capital, filters=None): class Country: - def __str__(self): - return "{}".format(self.name) - def __init__(self, country_data): self.top_level_domain = country_data.get("topLevelDomain") self.alpha2_code = country_data.get("alpha2Code")