Skip to content

Commit

Permalink
ENH: warn in read_postgis_ functions if no index column is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
bifbof committed Aug 2, 2023
1 parent 07b201a commit 72ad3c5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
23 changes: 23 additions & 0 deletions tests/io/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest
from trackintel.io.util import _index_warning_default_none


class Test_index_warning_default_none:
def test_index_set(self):
"""Test if a set index creates no warning."""

@_index_warning_default_none
def foo(index_col=None):
return

foo(index_col=None)

def test_index_default(self):
"""Test if default index creates a warning."""

@_index_warning_default_none
def foo(index_col=None):
return

with pytest.warns(UserWarning):
foo()
20 changes: 1 addition & 19 deletions trackintel/io/file.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import ast
import warnings
from functools import wraps
from inspect import signature

import geopandas as gpd
import pandas as pd
Expand All @@ -15,22 +12,7 @@
read_triplegs_gpd,
read_trips_gpd,
)


def _index_warning_default_none(func):
"""Decorator function that warns if index_col None is not set explicit."""

@wraps(func) # copy all metadata
def wrapper(*args, **kwargs):
bound_values = signature(func).bind(*args, **kwargs) # binds only available args and kwargs
if "index_col" not in bound_values.arguments:
warnings.warn(
"Assuming default index as unique identifier. "
"Pass 'index_col=None' as explicit argument to avoid a warning when reading csv files."
)
return func(*args, **kwargs)

return wrapper
from trackintel.io.util import _index_warning_default_none


@_index_warning_default_none
Expand Down
7 changes: 7 additions & 0 deletions trackintel/io/postgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sqlalchemy.types import JSON

import trackintel as ti
from trackintel.io.util import _index_warning_default_none


def _handle_con_string(func):
Expand Down Expand Up @@ -40,6 +41,7 @@ def wrapper(*args, **kwargs):
return wrapper


@_index_warning_default_none
@_handle_con_string
def read_positionfixes_postgis(
sql,
Expand Down Expand Up @@ -140,6 +142,7 @@ def write_positionfixes_postgis(
)


@_index_warning_default_none
@_handle_con_string
def read_triplegs_postgis(
sql,
Expand Down Expand Up @@ -239,6 +242,7 @@ def write_triplegs_postgis(
)


@_index_warning_default_none
@_handle_con_string
def read_staypoints_postgis(
sql,
Expand Down Expand Up @@ -340,6 +344,7 @@ def write_staypoints_postgis(
)


@_index_warning_default_none
@_handle_con_string
def read_locations_postgis(
sql,
Expand Down Expand Up @@ -461,6 +466,7 @@ def write_locations_postgis(
)


@_index_warning_default_none
@_handle_con_string
def read_trips_postgis(
sql,
Expand Down Expand Up @@ -590,6 +596,7 @@ def write_trips_postgis(
)


@_index_warning_default_none
@_handle_con_string
def read_tours_postgis(
sql,
Expand Down
19 changes: 19 additions & 0 deletions trackintel/io/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import warnings
from functools import wraps
from inspect import signature


def _index_warning_default_none(func):
"""Decorator function that warns if index_col None is not set explicit."""

@wraps(func) # copy all metadata
def wrapper(*args, **kwargs):
bound_values = signature(func).bind(*args, **kwargs) # binds only available args and kwargs
if "index_col" not in bound_values.arguments:
warnings.warn(
"Assuming default index as unique identifier. "
"Pass 'index_col=None' as explicit argument to avoid a warning."
)
return func(*args, **kwargs)

return wrapper

0 comments on commit 72ad3c5

Please sign in to comment.