-
Notifications
You must be signed in to change notification settings - Fork 192
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
Python: Unable to loop if key.value() is Null #778
Comments
Update: I am hitting the same issue while working on another use case with authentication.
user_id=434 has values for the two keys I think I can try to get around by using the requests and json module ... but ideally I would like to use the Python SDK. Maybe we can "loosen" up by not raising the KeyError if the value is None? |
@joeldodge79 @josephaxisa any chance you could take a look at this please? 🙏🏻 I can workaround by making a query using system activity to get rid of all null values and then work with the json body from sdk.run_look() but I am trying to make a sample demo doing a pure "pythonic" approach (without using a pre-built query from system activity). |
@lanlooker sorry for the delay on this, I missed the notification. The behavior you're experiencing is not unique to the SDK unfortunately; accessing a dictionary item that does not exist throws a KeyError in Python. However, I can think of a couple of ways to get around this. Consider the below dictionary: x = { 'foo': 'foo_val' }
if 'bar' in x:
print(x['bar'])
print(x.get('foo')) # foo_val
print(x.get('bar', None)) # None
print(x.get('bar', 'N/A')) # N/A
print(x.get('bar')) # None Hope this helps! |
TLDR; I believe the also sorry I missed the notification this is more complicated. This "dictionary" access is a facade in front of the actual model/instance interface the python sdk presents. When we wrote the initial [de]serialization for the model/instance interface we hit a snag where we if someone wrote this and sent it: user = User(first_name="Jill") we would send null for all unspecified json properties: {"first_name": "Jill", "last_name": null, ...} In order to work around this we decided to remove all user = User(first_name="Jill", last_name=EXPLICIT_NULL) # defined in models.py But that's all about creating input to be sent to the API. Here we're really talking about a response object/dict (though keeping in mind that often the response obj/dict is re-used as input). I think we should NOT be raising a KeyError if the field is None. This raises another question that is applicable to both the model/instance and dictionary interfaces: for a sparse response (e.g. We currently don't distinguish when we populate a response model/instance and, because the dictionary implementation is just a proxy to the model/instance, then we should expose what it would do. It's gonna look kind of odd though: user = User()
user.first_name # None
user['first_name'] # None |
Seems like I completely misinterpreted where the error was being thrown from. Thanks for clarifying @joeldodge79 |
Thank you both 🙏🏻 @joeldodge79 thank you! This is the exact issue I am having! I usually get a response from GET/ as a dict, hoping to transform this dict and use as a body of a different call (usually POST/), so not being able to access/transform a key-value pair if the value is null is a blocker. I hope that we remove the KeyError here. Also, thank you for the great write up on the context of the KeyError 🙏🏻 I've learned a great deal |
dictionary key access should reflect model property access as closely as possible. In this case, do NOT raise a KeyError for a model property that exists. https://git.io/JRrKm fixes #778
dictionary key access should reflect model property access as closely as possible. In this case, do NOT raise a KeyError for a model property that exists. https://git.io/JRrKm fixes #778
I am working on a use case to remove the "All Users" group from a folder, and I am using
all_content_metadata_accesses
.In Looker, if a folder doesn't have any groups as its editor or viewer, then group_id is null (json), so I expect to loop and write some conditions with group_id, similar to this:
However, I am unable to loop or do anything with the key-value pair 'group_id' if the value is None because we have an exception handling a key with a None value:
I'll post a reproduction case in our internal tool.
The text was updated successfully, but these errors were encountered: