Open
Conversation
added 3 commits
October 10, 2022 20:03
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.
SteinRobert
reviewed
Oct 10, 2022
| @@ -1,4 +1,4 @@ | |||
| # simpler import as described in the readme | |||
| from restcountries.base import RestCountryApiV2 | |||
| from restcountries.base import Country, RestCountryApiV2 | |||
Owner
There was a problem hiding this comment.
Usually this class is not needed for imports - why add it here?
Author
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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