Make library type-checkable - #16
Conversation
This enables proper typehinting when using this library.
…s 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.
There were two __str__ methods defined, the first seemed the less sophisticated one.
Uh oh!
There was an error while loading. Please reload this page.
| @@ -1,4 +1,4 @@ | |||
| # simpler import as described in the readme | |||
| from restcountries.base import RestCountryApiV2 | |||
| from restcountries.base import Country, RestCountryApiV2 | |||
There was a problem hiding this comment.
Usually this class is not needed for imports - why add it here?
There was a problem hiding this comment.
This allows for something like
countries: list[Country] =rapi.get_all()
print(countries[0].alpha_code)in order to pass pyright or a similar type checker, as said checker can now verify that alpha_code is a field that the first object of that list is guaranteed/supposed to have.
CORRECTION: Since I added the type hint for result_list, this is not strictly needed to pass pyright as it can simply infer the type of countries in the example above. Country is however needed to "properly" type hint it, but I guess countries: list will work for most people so if there are good reasons not to make Country importable, this change can be removed.
Minor refactor as I came across some issues while using this library and having pyright enabled. Hope I understood the spirit of the code correctly and thus made acceptable changes. If so, a new pypi release would be great! Thanks in advance