Skip to content

Fix: TypeError: object of type 'NoneType' has no len() #1121

New issue

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

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

Already on GitHub? # to your account

Closed
wants to merge 2 commits into from

Conversation

ericinfins
Copy link

TypeError: object of type 'NoneType' has no len()

Fixes #

Changed the sequence

Description of the Change

Checklist

  • PR only contains one change (considered splitting up PR)
  • unit-test added
  • documentation updated
  • CHANGELOG.md updated (only for user relevant changes)
  • author name in AUTHORS

Fix the error:  TypeError: object of type 'NoneType' has no len()
@sliverc
Copy link
Member

sliverc commented Jan 20, 2023

Thanks for bringing this up. The check is odd, and makes sense to do it the other way around. However, I am not sure whether it is at all possible (or should be possible) that data is None. Could you share how you have encountered this error, data being None?

@ericinfins
Copy link
Author

ericinfins commented Jan 23, 2023

I got the issue when I create the Response object without any data

In my API, I use the following line to return to the front:

Response(status=201)

In this line when self.data is None

class Response(SimpleTemplateResponse):
....

@property
    def rendered_content(self):
          .....
          ret = renderer.render(self.data, accepted_media_type, context) # this line the self.data is None

These are the logs

[Tue Jan 17 13:07:11.098182 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976] ERROR 2023-01-17 13:07:11,096 log 22877 140608666109696 Internal Server Error: /django/test/some_id/actions/xxx/
[Tue Jan 17 13:07:11.098258 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976] Traceback (most recent call last):
[Tue Jan 17 13:07:11.098278 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
[Tue Jan 17 13:07:11.098283 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     response = get_response(request)
[Tue Jan 17 13:07:11.098290 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/django/core/handlers/base.py", line 145, in _get_response
[Tue Jan 17 13:07:11.098296 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     response = self.process_exception_by_middleware(e, request)
[Tue Jan 17 13:07:11.098300 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/django/core/handlers/base.py", line 143, in _get_response
[Tue Jan 17 13:07:11.098304 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     response = response.render()
[Tue Jan 17 13:07:11.098308 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/django/template/response.py", line 105, in render
[Tue Jan 17 13:07:11.098313 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     self.content = self.rendered_content
[Tue Jan 17 13:07:11.098317 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/rest_framework/response.py", line 70, in rendered_content
[Tue Jan 17 13:07:11.098321 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     ret = renderer.render(self.data, accepted_media_type, context)
[Tue Jan 17 13:07:11.098325 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/rest_framework_json_api/renderers.py", line 544, in render
[Tue Jan 17 13:07:11.098337 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     return self.render_errors(data, accepted_media_type, renderer_context)
[Tue Jan 17 13:07:11.098342 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/rest_framework_json_api/renderers.py", line 529, in render_errors
[Tue Jan 17 13:07:11.098373 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     utils.format_errors(data), accepted_media_type, renderer_context
[Tue Jan 17 13:07:11.098377 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]   File "/var/project/env/lib64/python3.6/site-packages/rest_framework_json_api/utils.py", line 388, in format_errors
[Tue Jan 17 13:07:11.098381 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976]     if len(data) > 1 and isinstance(data, list):
[Tue Jan 17 13:07:11.098388 2023] [wsgi:error] [pid 22877] [remote 127.0.0.1:44976] TypeError: object of type 'NoneType' has no len()

@sliverc
Copy link
Member

sliverc commented Jan 27, 2023

Do you think you can write/add a test which reproduces this behavior? The issue is even though your fix looks simple the result is actually an invalid error object according to the JSON:API spec as an error object may not be None. Once there is a reproducing test, it will be easier to figure out what a complete bugfix would be for this issue.

@sliverc
Copy link
Member

sliverc commented Jun 13, 2023

This has been around for a while. Closing it for now as not being able to process this PR. In case you get around, adding a test to reproduce your error, simply ping me.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants