Skip to content

Commit

Permalink
Type hinting.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfjlaros committed Dec 19, 2020
1 parent bd17b75 commit 245acf8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
6 changes: 3 additions & 3 deletions ordered_map/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .ordered_map import read, write


def _get_metadata(name):
def _get_metadata(name: str) -> str:
pkg = get_distribution(__package__)

for line in pkg.get_metadata_lines(pkg.PKG_INFO):
Expand All @@ -19,11 +19,11 @@ def _get_metadata(name):
usage = [_get_metadata('Summary'), _copyright_notice]


def doc_split(func):
def doc_split(func: callable) -> str:
return func.__doc__.split('\n\n')[0]


def version(name):
def version(name: str) -> str:
return '{} version {}\n\n{}\nHomepage: {}'.format(
_get_metadata('Name'), _get_metadata('Version'), _copyright_notice,
_get_metadata('Home-page'))
6 changes: 3 additions & 3 deletions ordered_map/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from . import doc_split, usage, version, read as om_read, write as om_write


def read(input_handle, output_handle):
def read(input_handle: object, output_handle: object) -> None:
"""Convert an ordered map file to a YAML file.
:args stream input_handle: Input file in ordered map format.
Expand All @@ -15,7 +15,7 @@ def read(input_handle, output_handle):
default_flow_style=False)


def write(input_handle, output_handle):
def write(input_handle: object, output_handle: object) -> None:
"""Convert a YAML file to an ordered map file.
:args stream input_handle: Intput file in YAML format.
Expand All @@ -24,7 +24,7 @@ def write(input_handle, output_handle):
output_handle.write(om_write(safe_load(input_handle)))


def main():
def main() -> None:
"""Main entry point."""
files_parser = ArgumentParser(add_help=False)
files_parser.add_argument(
Expand Down
46 changes: 23 additions & 23 deletions ordered_map/ordered_map.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
def _deserialise(string):
def _deserialise(string: str) -> dict:
"""Deserialise an ordered map string.
:arg str string: An ordered map string.
:arg string: An ordered map string.
:returns dict: An ordered map.
:returns: An ordered map.
"""
if '.' not in string.split('=')[0]:
key, value = string.split('=', maxsplit=1)
Expand All @@ -13,13 +13,13 @@ def _deserialise(string):
return {head: _deserialise(tail)}


def _serialise(data, prefix=''):
def _serialise(data: any, prefix: str='') -> str:
"""Serialise an ordered map.
:arg dict data: An ordered map.
:arg str prefix: Partially serialised result.
:arg data: An ordered map.
:arg prefix: Partially serialised result.
:returns str: An ordered map string.
:returns: An ordered map string.
"""
if not isinstance(data, dict):
return '{}={}\n'.format(prefix, data)
Expand All @@ -36,13 +36,13 @@ def _serialise(data, prefix=''):
return result


def _merge(d1, d2):
def _merge(d1: any, d2: any) -> dict:
"""Merge two dictionaries.
:arg dict d1: Dictionary 1.
:arg dict d2: Dictionary 2.
:arg d1: Dictionary 1.
:arg d2: Dictionary 2.
:returns dict: Merged dictionary.
:returns: Merged dictionary.
"""
if not isinstance(d1, dict) and not isinstance(d2, dict):
return {d1, d2}
Expand All @@ -65,12 +65,12 @@ def _merge(d1, d2):
return result


def _to_list(data):
def _to_list(data: any) -> dict:
"""Convert indexed dictionaries to lists.
:arg dict data: An ordered map.
:arg data: An ordered map.
:returns dict: An ordered map.
:returns: An ordered map.
"""
if not isinstance(data, dict):
return data
Expand All @@ -82,12 +82,12 @@ def _to_list(data):
return dict([(key, _to_list(data[key])) for key in data])


def _from_list(data):
def _from_list(data: any) -> dict:
"""Convert lists to indexed dictionaries.
:arg dict data: An ordered map.
:arg data: An ordered map.
:returns dict: An ordered map.
:returns: An ordered map.
"""
if isinstance(data, list):
return dict([(str(i), _from_list(v)) for i, v in enumerate(data)])
Expand All @@ -96,12 +96,12 @@ def _from_list(data):
return data


def read(string):
def read(string: str) -> dict:
"""Parse an ordered map file.
:arg str string: Content of an ordered map file.
:arg string: Content of an ordered map file.
:returns dict: An ordered map.
:returns: An ordered map.
"""
data = {}
for line in string.split('\n'):
Expand All @@ -110,12 +110,12 @@ def read(string):
return _to_list(data)


def write(data):
def write(data: dict) -> str:
"""Write an odered map to a file.
:arg dict data: An ordered map.
:arg data: An ordered map.
:returns str: Content of an ordered map file.
:returns: Content of an ordered map file.
"""
_data = _from_list(data)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from ordered_map.cli import read, write


def test_read():
def test_read() -> None:
s = StringIO()
read(StringIO('a=b'), s)
assert s.getvalue() == 'a: b\n'


def test_write():
def test_write() -> None:
s = StringIO()
write(StringIO('a: b'), s)
assert s.getvalue() == '{}\na=b\n'.format(62 * '#')
22 changes: 11 additions & 11 deletions tests/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
from ordered_map import read, write


def test_deserialise():
def test_deserialise() -> None:
assert _deserialise('a.b=c\n') == {'a': {'b': 'c'}}
assert _deserialise('a.b=c') == {'a': {'b': 'c'}}
assert _deserialise('a.b = c') == {'a': {'b': 'c'}}
assert _deserialise('a.b.c=d') == {'a': {'b': {'c': 'd'}}}


def test_serialise():
def test_serialise() -> None:
assert _serialise({'a': {'b': 'c'}}) == 'a.b=c\n'
assert _serialise({'a': {'b': {'c': 'd'}}}) == 'a.b.c=d\n'


def test_merge():
def test_merge() -> None:
assert _merge({'a': 'b'}, {'x': 'y'}) == {'a': 'b', 'x': 'y'}
assert _merge({'a': 'b'}, {'a': 'y'}) == {'a': {'b', 'y'}}
assert _merge({'a': {'b': 'c'}}, {'a': {'y': 'z'}}) == {
Expand All @@ -25,7 +25,7 @@ def test_merge():
'a': {'b': {'c', 'z'}}}


def test_to_list():
def test_to_list() -> None:
assert _to_list({'0': 'a'}) == ['a']
assert _to_list({'1': 'a'}) == {'1': 'a'}
assert _to_list({'0': 'a', '1': 'b'}) == ['a', 'b']
Expand All @@ -37,7 +37,7 @@ def test_to_list():
{'a': 'b'}, {'x': 'y'}]


def test_from_list():
def test_from_list() -> None:
assert _from_list(['a']) == {'0': 'a'}
assert _from_list({'1': 'a'}) == {'1': 'a'}
assert _from_list(['a', 'b']) == {'0': 'a', '1': 'b'}
Expand All @@ -49,7 +49,7 @@ def test_from_list():
'0': {'a': 'b'}, '1': {'x': 'y'}}


def test_read_skip():
def test_read_skip() -> None:
assert read('') == []
assert read('\n') == []
assert read('\r') == []
Expand All @@ -58,23 +58,23 @@ def test_read_skip():
assert read('\ta=b') == []


def test_read_single():
def test_read_single() -> None:
assert read('a=b') == {'a': 'b'}


def test_read_multi():
def test_read_multi() -> None:
assert read('a=b\nx=y') == {'a': 'b', 'x': 'y'}


def test_write_skip():
def test_write_skip() -> None:
assert write({}) == ''
assert write([]) == ''


def test_write_single():
def test_write_single() -> None:
assert write({'a': 'b'}) == '{}\na=b\n'.format(62 * '#')


def test_write_multi():
def test_write_multi() -> None:
assert write({'a': 'b', 'x': 'y'}) == '{}\na=b\n{}\nx=y\n'.format(
62 * '#', 62 * '#')

0 comments on commit 245acf8

Please sign in to comment.