-
Notifications
You must be signed in to change notification settings - Fork 132
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
pchampin
wants to merge
2
commits into
digitalbazaar:master
Choose a base branch
from
pchampin:fix-issue50
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.