-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: warn in
read_postgis_
functions if no index column is provided
- Loading branch information
Showing
4 changed files
with
50 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |