Fix by_name() raising TypeError instead of returning None when nothing matches - #133
Open
keeltrace wants to merge 1 commit into
Open
Fix by_name() raising TypeError instead of returning None when nothing matches#133keeltrace wants to merge 1 commit into
keeltrace wants to merge 1 commit into
Conversation
…g matches In the no-client instance-library path, _instance_lookup.get(name, None) left objects as None when the name had no match, and the subsequent len(objects) raised TypeError. Return an empty list as the default instead, so by_name() correctly returns None. Fixes HumanBrainProject#130
Author
|
AI generated Maintainer review summary for Fairgraph PR #133. Summary reviewed and approved for posting by Keeltrace.
Overall: approve after CI passes; only clean up the PR description. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
AI Fully Automated PR - Review With Care
Summary
by_name()on aKGObjectsubclass without a client (the localinstance-library path used by controlled terms) raised
TypeError: object of type 'NoneType' has no len()when the requestedname did not match any instance, instead of returning
None.Reproduction (fairgraph master at time of fix; no KG connection needed):
Root cause
In
fairgraph/kgobject.py, the instance-library branch ofby_name()used
_instance_lookup.get(name, None). When nothing matched,objectswas set to
None, and the followingif len(objects) == 0:checkdereferenced it. The
"contains"branch already initialised an emptylist, so only exact matches were affected.
Fix
One line: default to an empty list —
cls._instance_lookup.get(name, [])— so the existing "no match → return None" path works as documented and
matches the behaviour of
from_id()with an unknown id.Tests
New offline regression test file
test/test_by_name.py(no network orKG credentials required), covering:
None(regression for this issue)match="contains"with no match returnsNoneall=Truereturns allmatchvalue raisesValueErrorVerified that the new test fails with the exact reported
TypeErrorwhen the one-line fix is reverted, and passes with it applied.
Full suite: 88 passed, 57 skipped (KG-connection tests), 0 failures.
Fixes #130