Skip to content

Commit

Permalink
Update type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed Aug 25, 2022
1 parent 510b631 commit 9180120
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 177 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ckanapi==4.7
defopt==6.4.0
email_validator==1.2.1
hdx-python-country==3.3.3
hdx-python-country==3.3.5
ndg-httpsclient==0.5.1
pyasn1==0.4.8
pyOpenSSL==22.0.0
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ install_requires =
ckanapi >= 4.7
defopt >= 6.4.0
email_validator
hdx-python-country>=3.3.3
hdx-python-country>=3.3.5
ndg-httpsclient
pyasn1
pyOpenSSL
Expand Down
10 changes: 8 additions & 2 deletions src/hdx/api/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,14 @@ def create_session_user_agent(
"""
if not session:
whitelist = frozenset(
["HEAD", "TRACE", "GET", "POST", "PUT", "OPTIONS", "DELETE"]
whitelist = (
"HEAD",
"TRACE",
"GET",
"POST",
"PUT",
"OPTIONS",
"DELETE",
)
session = get_session(
user_agent,
Expand Down
23 changes: 13 additions & 10 deletions src/hdx/api/locations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Locations in HDX"""
from typing import Dict, List, Optional, Tuple

from hdx.utilities.typehint import ListTuple

from hdx.api.configuration import Configuration


Expand Down Expand Up @@ -29,12 +31,12 @@ def validlocations(cls, configuration=None) -> List[Dict]:
return cls._validlocations

@classmethod
def set_validlocations(cls, locations: List[Dict]) -> None:
def set_validlocations(cls, locations: ListTuple[Dict]) -> None:
"""
Set valid locations using list of dictionaries of form {'name': 'zmb', 'title', 'Zambia'}
Args:
locations (List[Dict]): List of dictionaries of form {'name': 'zmb', 'title', 'Zambia'}
locations (ListTuple[Dict]): List of dictionaries of form {'name': 'zmb', 'title', 'Zambia'}
Returns:
None
Expand All @@ -45,38 +47,39 @@ def set_validlocations(cls, locations: List[Dict]) -> None:
def get_location_from_HDX_code(
cls,
code: str,
locations: Optional[List[Dict]] = None,
locations: Optional[ListTuple[Dict]] = None,
configuration: Optional[Configuration] = None,
) -> Optional[str]:
"""Get location from HDX location code
Args:
code (str): code for which to get location name
locations (Optional[List[Dict]]): Valid locations list. Defaults to list downloaded from HDX.
locations (Optional[ListTuple[Dict]]): Valid locations list. Defaults to list downloaded from HDX.
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
Optional[str]: location name
Optional[str]: location name or None
"""
if locations is None:
locations = cls.validlocations(configuration)
code = code.upper()
for locdict in locations:
if code.upper() == locdict["name"].upper():
if code == locdict["name"].upper():
return locdict["title"]
return None

@classmethod
def get_HDX_code_from_location(
cls,
location: str,
locations: Optional[List[Dict]] = None,
locations: Optional[ListTuple[Dict]] = None,
configuration: Optional[Configuration] = None,
) -> Optional[str]:
"""Get HDX code for location
Args:
location (str): Location for which to get HDX code
locations (Optional[List[Dict]]): Valid locations list. Defaults to list downloaded from HDX.
locations (Optional[ListTuple[Dict]]): Valid locations list. Defaults to list downloaded from HDX.
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
Expand All @@ -99,14 +102,14 @@ def get_HDX_code_from_location(
def get_HDX_code_from_location_partial(
cls,
location: str,
locations: Optional[List[Dict]] = None,
locations: Optional[ListTuple[Dict]] = None,
configuration: Optional[Configuration] = None,
) -> Tuple[Optional[str], bool]:
"""Get HDX code for location
Args:
location (str): Location for which to get HDX code
locations (Optional[List[Dict]]): Valid locations list. Defaults to list downloaded from HDX.
locations (Optional[ListTuple[Dict]]): Valid locations list. Defaults to list downloaded from HDX.
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
Expand Down
Loading

0 comments on commit 9180120

Please sign in to comment.