Opened 7 years ago

Last modified 6 years ago

#29303 new Bug

non_atomic_requests decorator alters _non_atomic_requests attribute of original function — at Version 2

Reported by: Alasdair Nicol Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Alasdair Nicol)

When calling non_atomic_requests with a function, it alters the _non_atomic_requests attribute of the original function.

Here's an example:

from django.test import TestCase

# Create your tests here.

from django.test import TestCase
from django.db import transaction

@transaction.non_atomic_requests(using='default')
def my_view(request):
        return HttpResponse('')

class TestViews(TestCase):

    def test(self):
        assert my_view._non_atomic_requests == {'default'}  # passes

        wrapped_view = transaction.non_atomic_requests(using='other')(my_view)

        assert wrapped_view._non_atomic_requests == {'default', 'other'}  # passes
        assert my_view._non_atomic_requests == {'default'}  # fails

I realise that this is a contrived example. It isn't an issue when non_atomic_requests is used as a decorator:

@transaction.non_atomic_requests(using='default')
@transaction.non_atomic_requests(using='other')
def my_view(request)
    return HttpResponse('')

Change History (2)

comment:1 by Tim Graham, 7 years ago

I'm not able to reproduce. I created this test file:

from django.test import TestCase
from django.db import transaction

@transaction.non_atomic_requests(using='default')
def my_view(request):
    return HttpResponse('')

class TestViews(TestCase):

    def test(self):
        assert my_view._non_atomic_requests == {'default'}  # passes

        wrapped_view = transaction.non_atomic_requests(using='other')

        assert wrapped_view._non_atomic_requests == {'default', 'other'}  # passes
        assert my_view._non_atomic_requests == {'default'}  # fails

I get:

Traceback (most recent call last):
  File "/home/tim/code/mysite/polls/test_29303.py", line 15, in test
    assert wrapped_view._non_atomic_requests == {'default', 'other'}  # passes
AttributeError: 'function' object has no attribute '_non_atomic_requests'

and if I remove the assertion, the next assertion passes.

comment:2 by Alasdair Nicol, 7 years ago

Description: modified (diff)

Sorry, there was a mistake in the wrapped_view line, I didn't apply it to the view. It should be:

wrapped_view = transaction.non_atomic_requests(using='other')(my_view)

I've updated the example in the ticket description.

Note: See TracTickets for help on using tickets.
Back to Top