add typing information for postcode formatter

This commit is contained in:
Sarah Hoffmann
2022-07-08 11:52:45 +02:00
parent d0c44431d0
commit 8adab2c6ca
2 changed files with 19 additions and 11 deletions

View File

@@ -7,7 +7,7 @@
""" """
Functions for importing and managing static country information. Functions for importing and managing static country information.
""" """
from typing import Dict, Any, Iterable, Tuple, Optional, Container from typing import Dict, Any, Iterable, Tuple, Optional, Container, overload
from pathlib import Path from pathlib import Path
import psycopg2.extras import psycopg2.extras
@@ -87,6 +87,13 @@ def setup_country_config(config: Configuration) -> None:
""" """
_COUNTRY_INFO.load(config) _COUNTRY_INFO.load(config)
@overload
def iterate() -> Iterable[Tuple[str, Dict[str, Any]]]:
...
@overload
def iterate(prop: str) -> Iterable[Tuple[str, Any]]:
...
def iterate(prop: Optional[str] = None) -> Iterable[Tuple[str, Dict[str, Any]]]: def iterate(prop: Optional[str] = None) -> Iterable[Tuple[str, Dict[str, Any]]]:
""" Iterate over country code and properties. """ Iterate over country code and properties.

View File

@@ -8,6 +8,7 @@
Functions for formatting postcodes according to their country-specific Functions for formatting postcodes according to their country-specific
format. format.
""" """
from typing import Any, Mapping, Optional, Set, Match
import re import re
from nominatim.errors import UsageError from nominatim.errors import UsageError
@@ -17,7 +18,7 @@ class CountryPostcodeMatcher:
""" Matches and formats a postcode according to a format definition """ Matches and formats a postcode according to a format definition
of the given country. of the given country.
""" """
def __init__(self, country_code, config): def __init__(self, country_code: str, config: Mapping[str, Any]) -> None:
if 'pattern' not in config: if 'pattern' not in config:
raise UsageError("Field 'pattern' required for 'postcode' " raise UsageError("Field 'pattern' required for 'postcode' "
f"for country '{country_code}'") f"for country '{country_code}'")
@@ -30,7 +31,7 @@ class CountryPostcodeMatcher:
self.output = config.get('output', r'\g<0>') self.output = config.get('output', r'\g<0>')
def match(self, postcode): def match(self, postcode: str) -> Optional[Match[str]]:
""" Match the given postcode against the postcode pattern for this """ Match the given postcode against the postcode pattern for this
matcher. Returns a `re.Match` object if the match was successful matcher. Returns a `re.Match` object if the match was successful
and None otherwise. and None otherwise.
@@ -44,7 +45,7 @@ class CountryPostcodeMatcher:
return None return None
def normalize(self, match): def normalize(self, match: Match[str]) -> str:
""" Return the default format of the postcode for the given match. """ Return the default format of the postcode for the given match.
`match` must be a `re.Match` object previously returned by `match` must be a `re.Match` object previously returned by
`match()` `match()`
@@ -56,9 +57,9 @@ class PostcodeFormatter:
""" Container for different postcode formats of the world and """ Container for different postcode formats of the world and
access functions. access functions.
""" """
def __init__(self): def __init__(self) -> None:
# Objects without a country code can't have a postcode per definition. # Objects without a country code can't have a postcode per definition.
self.country_without_postcode = {None} self.country_without_postcode: Set[Optional[str]] = {None}
self.country_matcher = {} self.country_matcher = {}
self.default_matcher = CountryPostcodeMatcher('', {'pattern': '.*'}) self.default_matcher = CountryPostcodeMatcher('', {'pattern': '.*'})
@@ -71,14 +72,14 @@ class PostcodeFormatter:
raise UsageError(f"Invalid entry 'postcode' for country '{ccode}'") raise UsageError(f"Invalid entry 'postcode' for country '{ccode}'")
def set_default_pattern(self, pattern): def set_default_pattern(self, pattern: str) -> None:
""" Set the postcode match pattern to use, when a country does not """ Set the postcode match pattern to use, when a country does not
have a specific pattern or is marked as country without postcode. have a specific pattern.
""" """
self.default_matcher = CountryPostcodeMatcher('', {'pattern': pattern}) self.default_matcher = CountryPostcodeMatcher('', {'pattern': pattern})
def get_matcher(self, country_code): def get_matcher(self, country_code: str) -> Optional[CountryPostcodeMatcher]:
""" Return the CountryPostcodeMatcher for the given country. """ Return the CountryPostcodeMatcher for the given country.
Returns None if the country doesn't have a postcode and the Returns None if the country doesn't have a postcode and the
default matcher if there is no specific matcher configured for default matcher if there is no specific matcher configured for
@@ -90,7 +91,7 @@ class PostcodeFormatter:
return self.country_matcher.get(country_code, self.default_matcher) return self.country_matcher.get(country_code, self.default_matcher)
def match(self, country_code, postcode): def match(self, country_code: str, postcode: str) -> Optional[Match[str]]:
""" Match the given postcode against the postcode pattern for this """ Match the given postcode against the postcode pattern for this
matcher. Returns a `re.Match` object if the country has a pattern matcher. Returns a `re.Match` object if the country has a pattern
and the match was successful or None if the match failed. and the match was successful or None if the match failed.
@@ -101,7 +102,7 @@ class PostcodeFormatter:
return self.country_matcher.get(country_code, self.default_matcher).match(postcode) return self.country_matcher.get(country_code, self.default_matcher).match(postcode)
def normalize(self, country_code, match): def normalize(self, country_code: str, match: Match[str]) -> str:
""" Return the default format of the postcode for the given match. """ Return the default format of the postcode for the given match.
`match` must be a `re.Match` object previously returned by `match` must be a `re.Match` object previously returned by
`match()` `match()`