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

Do not crash on AttributeError in dict_list_reduce helper function #2684

Closed
wants to merge 1 commit into from

Conversation

dlax
Copy link
Contributor

@dlax dlax commented Oct 13, 2015

I got the following error on a freshly installed CKAN 2.4.1 (source install) after adding a few datasets on a request to the home page.

[...]
File '/usr/local/src/ckan/ckan/templates/snippets/package_item.html', line 66 in block "resources_inner"
  {% for resource in h.dict_list_reduce(package.resources, 'format') %}
File '/usr/local/src/ckan/ckan/lib/helpers.py', line 872 in dict_list_reduce
  value = item.get(key)
AttributeError: 'int' object has no attribute 'get'

Not sure why this occurs... Looking closer, I saw 0 and 1 integers values as item (key was 'format').

value = item.get(key)
except AttributeError:
# item might not be a dict.
continue
Copy link
Contributor

Choose a reason for hiding this comment

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

As the function is documented as taking a list of dicts, it's possibly not the right thing to do, to just silently ignore the bad data passed in. Perhaps if you raise an error that points out that it has been passed bad data (e.g. not a list of dicts) may help in tracking down the real cause of the problem (possibly in a schema somewhere?).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ross Jones wrote:

As the function is documented as taking a list of dicts, it's possibly
not the right thing to do, to just silently ignore the bad data passed
in. Perhaps if you raise an error that points out that it has been
passed bad data (e.g. not a list of dicts) may help in tracking down the
real cause of the problem (possibly in a schema somewhere?).

I agree this is not the right thing to do (just did not read the
docstring :P). I can debug this live if you point me to something to
look for. The full traceback does not look very helpful from my point of
view...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It appears that the package.resources object passed to dict_list_reduce is actually not a list but a dict (corresponding to one resource). But the package at stake actually has two resources.

Copy link
Contributor

Choose a reason for hiding this comment

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

~~I think the thing to investigate is why when calling dict_list_reduce with

h.dict_list_reduce(package.resources, 'format')

we get things that are not resource dicts. package.resources should definitely be a list of dicts.

Perhaps just dumping {{package.resources}} in the package_item template might show what it is being passed, I can only imagine that a plugin/custom-schema is interfering somehow.~~

There's definitely some schema weirdness going on. Do you have a custom plugin? Which plugins do you use?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I'm using ckanext-dcat and this dataset comes from a DCAT harvest.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

package.resources is just one dict which looks like:

{0: {u'cache_last_updated': None, 
       u'issued': u'2015-10-13T15:00:17.332357', 
       u'uri': u'http://localhost:8080/distribution/1301',
       u'package_id': u'6b0af45b-81c5-425c-bcb2-42f291937ae7',
      ...}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

This is weird, perhaps @amercader might know if this is a dcat extension specific weirdness.

As a temporary workaround, you can probably get away with (obviously untested)..

h.dict_list_reduce(package.resources.values(), 'format')

but it should only be a temporary workaround.

@kmbn
Copy link
Contributor

kmbn commented Jan 22, 2019

We decided to close old pull requests that are not actively worked on so that we can focus our effort and attention on PRs made against the current version of CKAN. A lot has changed in the last several months and many PRs will have gone stale or may now be in conflict with the latest version.

Please note: this isn't a judgement about the quality of your pull request. We just don't have the resources to review older PRs.

If you would like to reopen your PR (and update it if necessary), please do!

@kmbn kmbn closed this Jan 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants