Skip to content

Commit

Permalink
Add model module and tests
Browse files Browse the repository at this point in the history
model.Object is the dict with deprecation warnings class.
  • Loading branch information
sgillies committed Jun 10, 2019
1 parent c097a81 commit e5a879e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
29 changes: 29 additions & 0 deletions fiona/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Fiona data model"""

from collections.abc import MutableMapping
from warnings import warn

from fiona.errors import FionaDeprecationWarning


class Object(MutableMapping):

def __init__(self, **data):
self._data = data.copy()

def __getitem__(self, item):
return self._data[item]

def __iter__(self):
return iter(self._data)

def __len__(self):
return len(self._data)

def __setitem__(self, key, value):
warn("Object will become immutable in version 2.0", FionaDeprecationWarning, stacklevel=2)
self._data[key] = value

def __delitem__(self, key):
warn("Object will become immutable in version 2.0", FionaDeprecationWarning, stacklevel=2)
del self._data[key]
22 changes: 22 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Test of deprecations following RFC 1"""

import pytest

from fiona.errors import FionaDeprecationWarning
from fiona.model import Object


def test_setitem_warning():
"""Warn about __setitem__"""
obj = Object()
with pytest.warns(FionaDeprecationWarning):
obj['g'] = 1
assert obj['g'] == 1


def test_delitem_warning():
"""Warn about __delitem__"""
obj = Object(g=1)
with pytest.warns(FionaDeprecationWarning):
del obj['g']
assert 'g' not in obj

0 comments on commit e5a879e

Please sign in to comment.