Skip to content

Commit c557053

Browse files
committed
add type annotations for additional osm functions
1 parent b54d5cb commit c557053

File tree

2 files changed

+64
-36
lines changed

2 files changed

+64
-36
lines changed

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] + '...'

src/osmium/osm/mutable.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
class OSMObject(object):
1+
from typing import Optional, Union, Any, Mapping, Sequence, Tuple, TYPE_CHECKING
2+
from datetime import datetime
3+
4+
if TYPE_CHECKING:
5+
import osmium.osm
6+
7+
OSMObjectLike = Union['OSMObject', osmium.osm.OSMObject]
8+
NodeLike = Union[Node, osmium.osm.Node]
9+
WayLike = Union[Way, osmium.osm.Way]
10+
RelationLike = Union[Relation, osmium.osm.Relation]
11+
12+
TagSequence = Union[osmium.osm.TagList, Mapping[str, str], Sequence[Tuple[str, str]]]
13+
LocationLike = Union[osmium.osm.Location, Tuple[float, float]]
14+
NodeSequence = Union[osmium.osm.NodeRefList, Sequence[Union[osmium.osm.NodeRef, int]]]
15+
MemberSequence = Union[osmium.osm.RelationMemberList,
16+
Sequence[Union[osmium.osm.RelationMember, Tuple[str, int, str]]]]
17+
18+
class OSMObject:
219
"""Mutable version of ``osmium.osm.OSMObject``. It exposes the following
320
attributes ``id``, ``version``, ``visible``, ``changeset``, ``timestamp``,
421
``uid`` and ``tags``. Timestamps may be strings or datetime objects.
@@ -9,8 +26,11 @@ class OSMObject(object):
926
will be initialised first from the attributes of this base object.
1027
"""
1128

12-
def __init__(self, base=None, id=None, version=None, visible=None, changeset=None,
13-
timestamp=None, uid=None, tags=None, user=None):
29+
def __init__(self, base: Optional['OSMObjectLike'] = None,
30+
id: Optional[int] = None, version: Optional[int] = None,
31+
visible: Optional[bool] = None, changeset: Optional[int] = None,
32+
timestamp: Optional[datetime] = None, uid: Optional[int] = None,
33+
tags: Optional['TagSequence'] = None, user: Optional[str] = None) -> None:
1434
if base is None:
1535
self.id = id
1636
self.version = version
@@ -37,7 +57,9 @@ class Node(OSMObject):
3757
may either be an `osmium.osm.Location` or a tuple of lon/lat coordinates.
3858
"""
3959

40-
def __init__(self, base=None, location=None, **attrs):
60+
def __init__(self, base: Optional['NodeLike'] = None,
61+
location: Optional['LocationLike'] = None,
62+
**attrs: Any) -> None:
4163
OSMObject.__init__(self, base=base, **attrs)
4264
if base is None:
4365
self.location = location
@@ -52,7 +74,8 @@ class Way(OSMObject):
5274
``osmium.osm.NodeRef`` or simple node ids.
5375
"""
5476

55-
def __init__(self, base=None, nodes=None, **attrs):
77+
def __init__(self, base: Optional['WayLike'] = None,
78+
nodes: Optional['NodeSequence'] = None, **attrs: Any) -> None:
5679
OSMObject.__init__(self, base=base, **attrs)
5780
if base is None:
5881
self.nodes = nodes
@@ -67,9 +90,33 @@ class Relation(OSMObject):
6790
member type should be a single character 'n', 'w' or 'r'.
6891
"""
6992

70-
def __init__(self, base=None, members=None, **attrs):
93+
def __init__(self, base: Optional['RelationLike'] = None,
94+
members: Optional['MemberSequence'] = None, **attrs: Any) -> None:
7195
OSMObject.__init__(self, base=base, **attrs)
7296
if base is None:
7397
self.members = members
7498
else:
7599
self.members = members if members is not None else base.members
100+
101+
102+
def create_mutable_node(node: 'NodeLike', **args: Any) -> Node:
103+
""" Create a mutable node replacing the properties given in the
104+
named parameters. Note that this function only creates a shallow
105+
copy which is still bound to the scope of the original object.
106+
"""
107+
return Node(base=node, **args)
108+
109+
def create_mutable_way(way: 'WayLike', **args: Any) -> Way:
110+
""" Create a mutable way replacing the properties given in the
111+
named parameters. Note that this function only creates a shallow
112+
copy which is still bound to the scope of the original object.
113+
"""
114+
return Way(base=way, **args)
115+
116+
def create_mutable_relation(rel: 'RelationLike', **args: Any) -> Relation:
117+
""" Create a mutable relation replacing the properties given in the
118+
named parameters. Note that this function only creates a shallow
119+
copy which is still bound to the scope of the original object.
120+
"""
121+
return Relation(base=rel, **args)
122+

0 commit comments

Comments
 (0)