Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make library type-checkable #16

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

iron3oxide
Copy link

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

jh 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.
restcountries/base.py Show resolved Hide resolved
@@ -1,4 +1,4 @@
# simpler import as described in the readme
from restcountries.base import RestCountryApiV2
from restcountries.base import Country, RestCountryApiV2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually this class is not needed for imports - why add it here?

Copy link
Author

@iron3oxide iron3oxide Oct 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants