Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed fix for issue #50 #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/pyld/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,8 @@ def expand(self, input_, options):
options = options or {}
options.setdefault('keepFreeFloatingNodes', False)
options.setdefault('documentLoader', _default_document_loader)
options.setdefault('strict', False)
options.setdefault('droppedKeys', None)

# if input is a string, attempt to dereference remote document
if _is_string(input_):
Expand Down Expand Up @@ -1966,6 +1968,7 @@ def _expand(
active_ctx, active_property, vocab=True)

rval = {}
droppedKeys = options['droppedKeys']
for key, value in sorted(element.items()):
if key == '@context':
continue
Expand All @@ -1979,6 +1982,10 @@ def _expand(
not (
_is_absolute_iri(expanded_property) or
_is_keyword(expanded_property))):
if options['strict']:
raise ValueError("Unrecognized key %s" % key)
elif droppedKeys is not None:
droppedKeys.add(key)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning a value from a function by modifying an argument is quite an unpopular method with Python programmers. This is a side effect; I would discourage it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anatoly-scherbakov could you refactor this in a way that still fixes the issue?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anatoly-scherbakov I disagree that it is inherently a bad thing for a function to mutate one of its arguments, as long as the contract is clear about it.
That being said, I agree that having this "output argument" included in the "options" argument (which is clearly an input argument otherwise), is not great.

I don't consider that changing the return value of expand, in order to include the information about dropped keys, is desirable (it would break existing code).

I believe that a better solution to be to add a 4th parameter to expand: dropped=None. If that parameter is explicitly specified, it must be an (empty?) list, and the contract would be that this list would be populated with the dropped keys.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if the contract is explicitly specified, this is not what a Python developer is expecting from a library, thus violating the Principle of Least Astonishment.

I have proposed a PR: #186 — but its not ready for merge as it should be really targeted at another PR of mine.

continue

if _is_keyword(expanded_property):
Expand Down
54 changes: 54 additions & 0 deletions tests/test-issue50.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import unittest

import pyld.jsonld as jsonld

class TestIssue50(unittest.TestCase):

CTX = { "foo": { "@id": "http://example.com/foo" } }
DATA = { "fooo": "bar" }
RESULT = []

def test_silently_ignored(self):
got = jsonld.expand(self.DATA,
{'expandContext': self.CTX})
self.assertEqual(got, self.RESULT)

def test_strict_fails(self):
with self.assertRaises(ValueError):
got = jsonld.expand(self.DATA,
{'expandContext': self.CTX, 'strict': True})

def test_dropped_keys(self):
dk = set()
got = jsonld.expand(self.DATA,
{'expandContext': self.CTX, 'droppedKeys': dk})
self.assertEqual(got, self.RESULT)
self.assertSetEqual(dk, {"fooo"})


DATA2 = { "@id": "foo", "foo": "bar", "fooo": "baz", "http://example.com/other": "blah" }
RESULT2 = [{ "@id": u"foo",
"http://example.com/foo": [{"@value": "bar"}],
"http://example.com/other": [{"@value": "blah"}],
}]

def test_silently_ignored_2(self):
got = jsonld.expand(self.DATA2,
{'expandContext': self.CTX})
self.assertEqual(got, self.RESULT2)

def test_strict_fails_2(self):
with self.assertRaises(ValueError):
got = jsonld.expand(self.DATA2,
{'expandContext': self.CTX, 'strict': True})

def test_dropped_keys_2(self):
dk = set()
got = jsonld.expand(self.DATA2,
{'expandContext': self.CTX, 'droppedKeys': dk})
self.assertEqual(got, self.RESULT2)
self.assertSetEqual(dk, {"fooo"})


if __name__ == "__main__":
unittest.main()