Skip to content

Commit

Permalink
MAINT Add type annotation (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
rth authored Oct 23, 2020
1 parent a2d0d6f commit e4aeceb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
python -m pip install --upgrade pip
if [ ${{ matrix.python-version }} == 3.6 ]
then
# for other python versions the latest pandas and numpy versions are tested
pip install numpy==1.13.3 pandas==0.25
elif [ ${{ matrix.python-version }} == 'pypy3' ]
then
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 19.10b0
rev: 20.8b1
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
Expand All @@ -15,7 +15,7 @@ repos:
- id: flake8
types: [file, python]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.730
rev: v0.790
hooks:
- id: mypy
args:
Expand Down
24 changes: 12 additions & 12 deletions pgeocode.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import urllib.request
import warnings
from io import BytesIO
from typing import Any, Tuple

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -120,11 +121,10 @@
]


def _get_url(url):
def _open_url(url: str) -> Tuple[BytesIO, Any]:
"""Download contents for a URL"""
res = urllib.request.urlopen(url)
reader = BytesIO(res.read())
res.close()
with urllib.request.urlopen(url) as res:
reader = BytesIO(res.read())
return reader, res.headers


Expand All @@ -140,7 +140,7 @@ class Nominatim:
into a single entry
"""

def __init__(self, country="fr", unique=True):
def __init__(self, country: str = "fr", unique: bool = True):

country = country.upper()
if country not in COUNTRIES_VALID:
Expand All @@ -166,7 +166,7 @@ def __init__(self, country="fr", unique=True):
self.unique = unique

@staticmethod
def _get_data(country):
def _get_data(country: str) -> Tuple[str, pd.DataFrame]:
"""Load the data from disk; otherwise download and save it"""
from zipfile import ZipFile

Expand All @@ -175,7 +175,7 @@ def _get_data(country):
data = pd.read_csv(data_path, dtype={"postal_code": str})
else:
url = DOWNLOAD_URL.format(country=country)
reader, headers = _get_url(url)
reader, headers = _open_url(url)
with ZipFile(reader) as fh_zip:
with fh_zip.open(country.upper() + ".txt") as fh:
data = pd.read_csv(
Expand All @@ -191,7 +191,7 @@ def _get_data(country):

return data_path, data

def _index_postal_codes(self):
def _index_postal_codes(self) -> pd.DataFrame:
""" Create a dataframe with unique postal codes """
data_path_unique = self._data_path.replace(".txt", "-index.txt")

Expand All @@ -216,7 +216,7 @@ def _index_postal_codes(self):
data_unique.to_csv(data_path_unique, index=None)
return data_unique

def _normalize_postal_code(self, codes):
def _normalize_postal_code(self, codes: pd.DataFrame) -> pd.DataFrame:
"""Normalize postal codes to the values contained in the database
For instance, take into account only first letters when applicable.
Expand Down Expand Up @@ -270,7 +270,7 @@ def query_location(self, name):


class GeoDistance(Nominatim):
""" Distance calculation from a city name or a postal code
"""Distance calculation from a city name or a postal code
Parameters
----------
Expand All @@ -282,11 +282,11 @@ class GeoDistance(Nominatim):
'nearest' (find from nearest valid points)
"""

def __init__(self, country="fr", errors="ignore"):
def __init__(self, country: str = "fr", errors: str = "ignore"):
super().__init__(country)

def query_postal_code(self, x, y):
""" Get distance (in km) between postal codes
"""Get distance (in km) between postal codes
Parameters
----------
Expand Down

0 comments on commit e4aeceb

Please sign in to comment.