Skip to content

Commit 2f5a343

Browse files
authored
Merge pull request #211 from lonvia/type-annotations
Introduce type annotations
2 parents bd0c2d1 + 4ac14a6 commit 2f5a343

File tree

15 files changed

+555
-84
lines changed

15 files changed

+555
-84
lines changed

lib/osm.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,17 @@ PYBIND11_MODULE(_osm, m) {
102102
py::return_value_policy::reference_internal,
103103
"Extend the box to include the given location. If the location "
104104
"is invalid the box remains unchanged. If the box is invalid, it "
105-
"will contain only the location after the operation.")
105+
"will contain only the location after the operation. "
106+
"Returns a reference to itself.")
106107
.def("extend",
107108
(osmium::Box& (osmium::Box::*)(osmium::Box const &))
108109
&osmium::Box::extend,
109110
py::arg("box"),
110111
py::return_value_policy::reference_internal,
111112
"Extend the box to include the given box. If the box to be added "
112113
"is invalid the input box remains unchanged. If the input box is invalid, it "
113-
"will become equal to the box that was added.")
114+
"will become equal to the box that was added. "
115+
"Returns a reference to itself.")
114116
.def("valid", &osmium::Box::valid,
115117
"Check if the box coordinates are defined and with the usual bounds.")
116118
.def("size", &osmium::Box::size,

lib/osmium.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ PYBIND11_MODULE(_osmium, m) {
3737
"Apply a chain of handlers.");
3838
m.def("apply", [](osmium::io::Reader &rd, NodeLocationHandler &h)
3939
{ py::gil_scoped_release release; osmium::apply(rd, h); },
40-
py::arg("reader"), py::arg("handler"),
40+
py::arg("reader"), py::arg("node_handler"),
4141
"Apply a chain of handlers.");
4242
m.def("apply", [](osmium::io::Reader &rd, NodeLocationHandler &l,
4343
BaseHandler &h)

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ def build_extension(self, ext):
166166
ext_modules=[CMakeExtension('cmake_example')],
167167
packages = ['osmium', 'osmium/osm', 'osmium/replication'],
168168
package_dir = {'' : 'src'},
169+
package_data = { 'osmium': ['py.typed', '*.pyi',
170+
'replication/_replication.pyi',
171+
'osm/_osm.pyi']},
169172
python_requires = ">=3.6",
170173
install_requires = ['requests'],
171174
cmdclass=dict(build_ext=CMakeBuild, sdist=Pyosmium_sdist),

src/osmium/_osmium.pyi

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from typing import overload, ByteString, Union
2+
import os
3+
4+
import osmium.index
5+
import osmium.io
6+
7+
StrPath = Union[str, 'os.PathLike[str]']
8+
9+
class InvalidLocationError(Exception): ...
10+
11+
class NodeLocationsForWays:
12+
def __init__(self, locations: osmium.index.LocationTable) -> None: ...
13+
def ignore_errors(self) -> None: ...
14+
15+
class BaseHandler: ...
16+
17+
class SimpleHandler(BaseHandler):
18+
def __init__(self) -> None: ...
19+
def apply_buffer(self, buffer: Union[ByteString, str], format: str, locations: bool = ..., idx: str = ...) -> None: ...
20+
def apply_file(self, filename: StrPath, locations: bool = ..., idx: str = ...) -> None: ...
21+
22+
class MergeInputReader:
23+
def __init__(self) -> None: ...
24+
def add_buffer(self, buffer: Union[ByteString, str], format: str) -> int: ...
25+
def add_file(self, file: str) -> int: ...
26+
def apply(self, handler: BaseHandler, idx: str = ..., simplify: bool = ...) -> None: ...
27+
def apply_to_reader(self, reader: osmium.io.Reader, writer: osmium.io.Writer, with_history: bool = ...) -> None: ...
28+
29+
class WriteHandler(BaseHandler):
30+
@overload
31+
def __init__(self, filename: str, bufsz: int, filetype: str) -> None: ...
32+
@overload
33+
def __init__(self, filename: str, bufsz: int) -> None: ...
34+
@overload
35+
def __init__(self, filename: str) -> None: ...
36+
def close(self) -> None: ...
37+
38+
class SimpleWriter:
39+
@overload
40+
def __init__(self, filename: str, bufsz: int, header: osmium.io.Header) -> None: ...
41+
@overload
42+
def __init__(self, filename: str, bufsz: int) -> None: ...
43+
@overload
44+
def __init__(self, filename: str) -> None: ...
45+
def add_node(self, node: object) -> None: ...
46+
def add_relation(self, relation: object) -> None: ...
47+
def add_way(self, way: object) -> None: ...
48+
def close(self) -> None: ...
49+
50+
@overload
51+
def apply(reader: osmium.io.Reader, handler: BaseHandler) -> None: ...
52+
@overload
53+
def apply(reader: osmium.io.Reader, node_handler: NodeLocationsForWays) -> None: ...
54+
@overload
55+
def apply(reader: osmium.io.Reader, node_handler: NodeLocationsForWays, handler: BaseHandler) -> None: ...

src/osmium/geom.pyi

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
from typing import ClassVar
2+
3+
from typing import overload
4+
import osmium.osm
5+
ALL: use_nodes
6+
BACKWARD: direction
7+
FORWARD: direction
8+
UNIQUE: use_nodes
9+
10+
11+
class use_nodes:
12+
ALL: ClassVar[use_nodes] = ...
13+
UNIQUE: ClassVar[use_nodes] = ...
14+
def __init__(self, value: int) -> None: ...
15+
@property
16+
def name(self) -> str: ...
17+
@property
18+
def value(self) -> int: ...
19+
20+
class direction:
21+
BACKWARD: ClassVar[direction] = ...
22+
FORWARD: ClassVar[direction] = ...
23+
def __init__(self, value: int) -> None: ...
24+
@property
25+
def name(self) -> str: ...
26+
@property
27+
def value(self) -> int: ...
28+
29+
class Coordinates:
30+
@overload
31+
def __init__(self) -> None: ...
32+
@overload
33+
def __init__(self, cx: float, cy: float) -> None: ...
34+
@overload
35+
def __init__(self, location: osmium.osm.Location) -> None: ...
36+
def valid(self) -> bool: ...
37+
@property
38+
def x(self) -> float: ...
39+
@property
40+
def y(self) -> float: ...
41+
42+
43+
class GeoJSONFactory:
44+
def __init__(self) -> None: ...
45+
@overload
46+
def create_linestring(self, list: osmium.osm.WayNodeList, use_nodes: use_nodes = ..., direction: direction = ...) -> str: ...
47+
@overload
48+
def create_linestring(self, way: osmium.osm.Way, use_nodes: use_nodes = ..., direction: direction = ...) -> str: ...
49+
def create_multipolygon(self, area: osmium.osm.Area) -> str: ...
50+
@overload
51+
def create_point(self, location: osmium.osm.Location) -> str: ...
52+
@overload
53+
def create_point(self, node: osmium.osm.Node) -> str: ...
54+
@overload
55+
def create_point(self, ref: osmium.osm.NodeRef) -> str: ...
56+
@property
57+
def epsg(self) -> int: ...
58+
@property
59+
def proj_string(self) -> str: ...
60+
61+
class WKBFactory:
62+
def __init__(self) -> None: ...
63+
@overload
64+
def create_linestring(self, list: osmium.osm.WayNodeList, use_nodes: use_nodes = ..., direction: direction = ...) -> str: ...
65+
@overload
66+
def create_linestring(self, way: osmium.osm.Way, use_nodes: use_nodes = ..., direction: direction = ...) -> str: ...
67+
def create_multipolygon(self, area: osmium.osm.Area) -> str: ...
68+
@overload
69+
def create_point(self, location: osmium.osm.Location) -> str: ...
70+
@overload
71+
def create_point(self, node: osmium.osm.Node) -> str: ...
72+
@overload
73+
def create_point(self, ref: osmium.osm.NodeRef) -> str: ...
74+
@property
75+
def epsg(self) -> int: ...
76+
@property
77+
def proj_string(self) -> str: ...
78+
79+
class WKTFactory:
80+
def __init__(self) -> None: ...
81+
@overload
82+
def create_linestring(self, list: osmium.osm._osm.WayNodeList, use_nodes: use_nodes = ..., direction: direction = ...) -> str: ...
83+
@overload
84+
def create_linestring(self, way: osmium.osm._osm.Way, use_nodes: use_nodes = ..., direction: direction = ...) -> str: ...
85+
def create_multipolygon(self, area: osmium.osm._osm.Area) -> str: ...
86+
@overload
87+
def create_point(self, location: osmium.osm._osm.Location) -> str: ...
88+
@overload
89+
def create_point(self, node: osmium.osm._osm.Node) -> str: ...
90+
@overload
91+
def create_point(self, ref: osmium.osm._osm.NodeRef) -> str: ...
92+
@property
93+
def epsg(self) -> int: ...
94+
@property
95+
def proj_string(self) -> str: ...
96+
97+
def haversine_distance(list: osmium.osm.WayNodeList) -> float: ...
98+
def lonlat_to_mercator(coordinate: Coordinates) -> Coordinates: ...
99+
def mercator_to_lonlat(coordinate: Coordinates) -> Coordinates: ...

src/osmium/helper.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
from typing import Optional, Callable, TypeVar
2+
13
from osmium._osmium import SimpleHandler
4+
from osmium.osm import Node, Way, Relation, Area, Changeset
5+
6+
T = TypeVar('T')
7+
HandlerFunc = Optional[Callable[[T], None]]
8+
29

3-
def make_simple_handler(node=None, way=None, relation=None, area=None, changeset=None):
10+
def make_simple_handler(node: HandlerFunc[Node] = None,
11+
way: HandlerFunc[Way] = None,
12+
relation: HandlerFunc[Relation] = None,
13+
area: HandlerFunc[Area] = None,
14+
changeset: HandlerFunc[Changeset] = None) -> SimpleHandler:
415
""" Convenience function that creates a `SimpleHandler` from a set of
516
callback functions. Each of the parameters takes an optional callable
617
that must expect a single positional parameter with the object being
@@ -10,14 +21,14 @@ class __HandlerWithCallbacks(SimpleHandler):
1021
pass
1122

1223
if node is not None:
13-
__HandlerWithCallbacks.node = staticmethod(node)
24+
setattr(__HandlerWithCallbacks, "node", staticmethod(node))
1425
if way is not None:
15-
__HandlerWithCallbacks.way = staticmethod(way)
26+
setattr(__HandlerWithCallbacks, "way", staticmethod(way))
1627
if relation is not None:
17-
__HandlerWithCallbacks.relation = staticmethod(relation)
28+
setattr(__HandlerWithCallbacks, "relation", staticmethod(relation))
1829
if area is not None:
19-
__HandlerWithCallbacks.area = staticmethod(area)
30+
setattr(__HandlerWithCallbacks, "area", staticmethod(area))
2031
if changeset is not None:
21-
__HandlerWithCallbacks.changeset = staticmethod(changeset)
32+
setattr(__HandlerWithCallbacks, "changeset", staticmethod(changeset))
2233

2334
return __HandlerWithCallbacks()

src/osmium/index.pyi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import List
2+
3+
import osmium.osm
4+
5+
class LocationTable:
6+
def clear(self) -> None: ...
7+
def get(self, id: int) -> osmium.osm.Location: ...
8+
def set(self, id: int, loc: osmium.osm.Location) -> None: ...
9+
def used_memory(self) -> int: ...
10+
11+
def create_map(map_type: str) -> LocationTable: ...
12+
def map_types() -> List[str]: ...

src/osmium/io.pyi

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import Any
2+
3+
from typing import overload
4+
5+
import osmium.osm
6+
7+
class File:
8+
has_multiple_object_versions: bool
9+
@overload
10+
def __init__(self, filename: str) -> None: ...
11+
@overload
12+
def __init__(self, filename: str, format: str) -> None: ...
13+
def parse_format(self, arg0: str) -> None: ...
14+
15+
class Header:
16+
has_multiple_object_versions: bool
17+
def __init__(self) -> None: ...
18+
def add_box(self, box: osmium.osm.Box) -> Header: ...
19+
def box(self) -> osmium.osm.Box: ...
20+
def get(self, key: str, default: str = ...) -> str: ...
21+
def set(self, key: str, value: str) -> None: ...
22+
23+
class Reader:
24+
@overload
25+
def __init__(self, filename: str) -> None: ...
26+
@overload
27+
def __init__(self, filename: str, types: osmium.osm.osm_entity_bits) -> None: ...
28+
def close(self) -> None: ...
29+
def eof(self) -> bool: ...
30+
def header(self) -> Header: ...
31+
32+
class Writer:
33+
@overload
34+
def __init__(self, filename: str) -> None: ...
35+
@overload
36+
def __init__(self, ffile: File) -> None: ...
37+
@overload
38+
def __init__(self, filename: str, header: Header) -> None: ...
39+
@overload
40+
def __init__(self, ffile: File, header: Header) -> None: ...
41+
def close(self) -> int: ...

src/osmium/osm/__init__.py

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,24 @@
1-
import osmium.osm.mutable
1+
from typing import Any, Callable, Sequence
2+
3+
from osmium.osm.mutable import create_mutable_node, create_mutable_way, create_mutable_relation
24
from ._osm import *
35

4-
def create_mutable_node(node, **args):
5-
""" Create a mutable node replacing the properties given in the
6-
named parameters. Note that this function only creates a shallow
7-
copy which is still bound to the scope of the original object.
8-
"""
9-
return osmium.osm.mutable.Node(base=node, **args)
10-
11-
def create_mutable_way(way, **args):
12-
""" Create a mutable way replacing the properties given in the
13-
named parameters. Note that this function only creates a shallow
14-
copy which is still bound to the scope of the original object.
15-
"""
16-
return osmium.osm.mutable.Way(base=way, **args)
17-
18-
def create_mutable_relation(rel, **args):
19-
""" Create a mutable relation replacing the properties given in the
20-
named parameters. Note that this function only creates a shallow
21-
copy which is still bound to the scope of the original object.
22-
"""
23-
return osmium.osm.mutable.Relation(base=rel, **args)
24-
25-
Node.replace = create_mutable_node
26-
Way.replace = create_mutable_way
27-
Relation.replace = create_mutable_relation
28-
29-
def _make_repr(*attr_list):
6+
setattr(Node, 'replace', create_mutable_node)
7+
setattr(Way, 'replace', create_mutable_way)
8+
setattr(Relation, 'replace', create_mutable_relation)
9+
10+
def _make_repr(*attrs: str) -> Callable[[object], str]:
3011
fmt_string = 'osmium.osm.{0}('\
31-
+ ', '.join([f'{x}={{1.{x}!r}}' for x in attr_list])\
12+
+ ', '.join([f'{x}={{1.{x}!r}}' for x in attrs])\
3213
+ ')'
3314

3415
return lambda o: fmt_string.format(o.__class__.__name__, o)
3516

36-
def _list_repr(obj):
17+
def _list_repr(obj: Sequence[Any]) -> str:
3718
return 'osmium.osm.{}([{}])'.format(obj.__class__.__name__,
3819
', '.join(map(repr, obj)))
3920

40-
def _list_elipse(obj):
21+
def _list_elipse(obj: Sequence[Any]) -> str:
4122
objects = ','.join(map(str, obj))
4223
if len(objects) > 50:
4324
objects = objects[:47] + '...'

0 commit comments

Comments
 (0)