Skip to content

Commit

Permalink
Fix double import in geoarrow.pyarrow resulting in double registeri…
Browse files Browse the repository at this point in the history
…ng extension types (#6)

* see if we still get a pytest import fail

* no relative imports

* use importlib

* one more

* catch error to prevent failed import
  • Loading branch information
paleolimbot authored Sep 25, 2023
1 parent bea9ae3 commit acdb60c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 27 deletions.
19 changes: 7 additions & 12 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,15 @@ jobs:
run: |
pytest geoarrow-pandas/tests -v -s
- name: Run doctests
- name: Run doctests (geoarrow-pyarrow)
if: success() && matrix.python-version == '3.11'
run: |
# Because of namespace packaging we have to add __init__.py in geoarrow and
# rebuild to avoid confusing pytest
for pkg in pyarrow pandas; do
pushd geoarrow-$pkg
touch src/geoarrow/__init__.py
pip uninstall --yes geoarrow-$pkg
pip install .
pytest --pyargs geoarrow.$pkg --doctest-modules
popd
done
pytest --pyargs geoarrow.pyarrow --doctest-modules --import-mode=importlib
- name: Run doctests (geoarrow-pandas)
if: success() && matrix.python-version == '3.11'
run: |
pytest --pyargs geoarrow.pandas --doctest-modules --import-mode=importlib
coverage:
needs: [test]
Expand Down
21 changes: 15 additions & 6 deletions geoarrow-pyarrow/src/geoarrow/pyarrow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from geoarrow.c.lib import GeometryType, Dimensions, CoordType, EdgeType, CrsType

from ._type import (
from geoarrow.pyarrow._type import (
VectorType,
WktType,
WkbType,
Expand All @@ -35,11 +35,11 @@
unregister_extension_types,
)

from ._kernel import Kernel
from geoarrow.pyarrow._kernel import Kernel

from ._array import array
from geoarrow.pyarrow._array import array

from . import _scalar
from geoarrow.pyarrow import _scalar

from ._compute import (
parse_all,
Expand Down Expand Up @@ -90,5 +90,14 @@ def dataset(*args, geometry_columns=None, use_row_groups=None, **kwargs):
else:
return GeoDataset(parent, geometry_columns=geometry_columns)


register_extension_types()
try:
register_extension_types()
except Exception as e:
import warnings

warnings.warn(
"Failed to register one or more extension types.\n"
"If this warning appears from pytest, you may have to re-run with --import-mode=importlib.\n"
"You may also be able to run `unregister_extension_types()` and `register_extension_types()`.\n"
"The original error was {e}"
)
12 changes: 10 additions & 2 deletions geoarrow-pyarrow/src/geoarrow/pyarrow/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

from geoarrow.c import lib

from ._kernel import Kernel
from ._type import WktType, WkbType, VectorType, wkb, wkt, large_wkb, large_wkt
from geoarrow.pyarrow._kernel import Kernel
from geoarrow.pyarrow._type import (
WktType,
WkbType,
VectorType,
wkb,
wkt,
large_wkb,
large_wkt,
)


class VectorArray(pa.ExtensionArray):
Expand Down
6 changes: 3 additions & 3 deletions geoarrow-pyarrow/src/geoarrow/pyarrow/_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import pyarrow.compute as pc

from geoarrow.c.lib import GeometryType, Dimensions, CoordType, EdgeType
from . import _type
from ._array import array
from ._kernel import Kernel
from geoarrow.pyarrow import _type
from geoarrow.pyarrow._array import array
from geoarrow.pyarrow._kernel import Kernel

_max_workers = 1

Expand Down
4 changes: 2 additions & 2 deletions geoarrow-pyarrow/src/geoarrow/pyarrow/_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import pyarrow.compute as _compute
import pyarrow.parquet as _pq
from geoarrow.c.lib import CoordType
from ._type import wkt, wkb, VectorType
from ._kernel import Kernel
from geoarrow.pyarrow._type import wkt, wkb, VectorType
from geoarrow.pyarrow._kernel import Kernel


class GeoDataset:
Expand Down
2 changes: 1 addition & 1 deletion geoarrow-pyarrow/src/geoarrow/pyarrow/_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pyarrow as pa

from geoarrow.c import lib
from ._type import VectorType
from geoarrow.pyarrow._type import VectorType


class Kernel:
Expand Down
3 changes: 2 additions & 1 deletion geoarrow-pyarrow/src/geoarrow/pyarrow/_scalar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pyarrow as pa

from ._type import VectorType
from geoarrow.pyarrow._type import VectorType


class VectorScalar(pa.ExtensionScalar):
Expand Down Expand Up @@ -51,6 +51,7 @@ def scalar_cls_from_name(name):
else:
raise ValueError(f'Expected valid extension name but got "{name}"')


# Inject array_cls_from_name exactly once to avoid circular import
if VectorType._scalar_cls_from_name is None:
VectorType._scalar_cls_from_name = scalar_cls_from_name

0 comments on commit acdb60c

Please sign in to comment.