bdon / bdon/OSMExpress

Python wrapper API has changed; python examples are not working

Open
#48 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
272
Forks
22
PR merge metrics
No merged PRs in 30d

Description

The python examples in the repository aren't working. As an example, here's me running `read_way.py`.

```
$ python python/examples/read_way.py washington.osmx 761811137
Traceback (most recent call last):
File "/Users/jake/Code/FOSS/protomaps/OSMExpress/python/examples/read_way.py", line 18, in
for node_id in way.nodes:
^^^^^^^^^
AttributeError: '_GeneratorContextManager' object has no attribute 'nodes'
```

The `augmented_diff.py` script fails similarly:

```
python python/examples/augmented_diff.py washington.osmx 155421965.osc 155421965.adiff
No old loc found for tagless node 5694096597
Traceback (most recent call last):
File "/Users/jake/Code/FOSS/protomaps/OSMExpress/python/examples/augmented_diff.py", line 138, in
set_old_metadata(prev_version)
File "/Users/jake/Code/FOSS/protomaps/OSMExpress/python/examples/augmented_diff.py", line 74, in set_old_metadata
elem.set('version',str(o.metadata.version))
^^^^^^^^^^
AttributeError: '_GeneratorContextManager' object has no attribute 'metadata'
```

The root cause is that [pycapnp `from_bytes()` now returns a context manager](https://github.com/capnproto/pycapnp/issues/287), requiring callers to change from this:

```python
way = messages_capnp.Way.from_bytes(msg)
# do stuff with way
```

...to this:

```python
with messages_capnp.Way.from_bytes(msg) as way:
# do stuff with way
```

This change was introduced in pycapnp 1.1.1. It looks like the Python examples were developed against an older version of OSMX that used pycapnp 0.6.4, but #46 bumped the pycapnp version to 2.0.0. This effectively changed the API of `nodes.get()`, `ways.get()`, etc by making them return context managers rather than return nodes/ways/etc directly, but I assume this wasn't noticed at the time and the examples weren't updated to match.

I think there are two possible fixes: either downgrade the Python osmx wrapper to use pycapnp <= 1.1.0, or change the Python examples to use `with`. I'm not sure which is more appropriate. It's worth noting that if pycapnp ~= 2.0.0 is kept, the next release of osmx will be a breaking API change for any users of the Python wrapper.

EDIT: Actually it looks like a version with the new context-manager API has [already been published](https://pypi.org/project/osmx/#history), though as a 0.0.x release I think most users would know not to expect a stable API 😉

I might be able to work on a PR for this at some point soon, but wanted to open this issue for now in case I don't end up having time to.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with python/examples/read_way.py and python/examples/augmented_diff.py, then inspect the Python wrapper paths behind nodes.get() and ways.get() alongside the pycapnp dependency version. Reproduce the commands shown in the issue and determine the appropriate compatibility approach. Done means both examples run without the reported context-manager attribute errors and the wrapper behavior is consistent with the supported pycapnp version.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.