graphql-python / graphene-django

Build powerful, efficient, and flexible GraphQL APIs with seamless Django integration.
http://docs.graphene-python.org/projects/django/en/latest/
MIT License
4.31k stars 769 forks source link

AttributeError: 'function' object has no attribute 'wrapped' in Django 2.2 #828

Open taylor-cedar opened 4 years ago

taylor-cedar commented 4 years ago

In Django 2.2 (works fine in 2.1) tests, connections are overridden/monkey patched with properties that throw errors, specifically the connection.cursor method.

https://github.com/django/django/blob/master/django/test/testcases.py#L210

Graphene also monkey patches connection.cursor.

https://github.com/graphql-python/graphene-django/blob/master/graphene_django/debug/sql/tracking.py#L43

This causes tests to fail when Django attempts to undo the monkey patch. https://github.com/django/django/blob/master/django/test/testcases.py#L220

The following error occurs:

ERROR: tearDownClass (point_of_sale.tests.graphene.queries.e2e_test_cash_and_check_batch_query.CashAndCheckBatchQueryTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/cedar/api/common/testing/test_cases/db_test_case.py", line 44, in tearDownClass
    super(TestCase, cls).tearDownClass()
  File "/usr/local/lib/python3.6/dist-packages/django/test/testcases.py", line 244, in tearDownClass
    cls._remove_databases_failures()
  File "/usr/local/lib/python3.6/dist-packages/django/test/testcases.py", line 240, in _remove_databases_failures
    setattr(connection, name, method.wrapped)
AttributeError: 'function' object has no attribute 'wrapped'

----------------------------------------------------------------------

This test is using the Django test client to test the /graphql endpoint. https://docs.djangoproject.com/en/3.0/topics/testing/tools/#overview-and-a-quick-example

r-azh commented 4 years ago

I have the same issue when testing /graphql endpoint.

VackarAfzal commented 4 years ago

I'm have the same issue, any updates on this?

stale[bot] commented 4 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

sebslomski commented 3 years ago

This is caused by the graphene-django debug middleware. In order fix it, set the graphene middlewares to []

crazyscientist commented 3 years ago

Hi there,

In order fix it, set the graphene middlewares to []

@sebslomski How and where would one do that? I'm asking because I have the following situation:

I have not set any graphene middlewares and my Django project uses (among others):

Django                         2.2.17
graphene                       2.1.8
graphene-django                2.15.0
graphene-django-optimizer      0.8.0
graphql-core                   2.3.2
graphql-relay                  2.0.1

Locally, on Ubuntu 20.04 with a Python 3.8 venv I can reproduce the reported traceback.

However in our Gitlab CI (openSUSE Leap 15.2 image with Python 3.6 and identical package versions) I do not get this error!

I'm wondering whether there might be another possible cause for this issue :thinking:

frankmeszaros commented 3 years ago

Hey @crazyscientist , I have been running into this exact issue for the past week and it was driving me crazy! I'm not sure if this will help you but this was what fixed it for me on a relatively standard graphene-django setup:

The error

in your settings.py (or wherever the settings for your tests live), we initially had:

# ... other stuff

GRAPHENE = {}

# ... other stuff

When this is the case, I was seeing the following 😒 : image

The Fix

in your settings.py (or wherever the settings for your tests live), we revised this to:

# ... other stuff

GRAPHENE = {
  "MIDDLEWARE": [] 
}

When this minor change was added, I was no longer getting the error πŸ˜„ :
image

πŸŽ‰ I hope this gets you a little farther along in your adventures, happy coding!

crazyscientist commented 3 years ago

@frankmeszaros Thank you for the detailed answer. That fixed the problem for me as well. :smiley:

chrisspen commented 3 years ago

@frankmeszaros Is that an acceptable solution? If the Graphene middleware is causing unittests to randomly fail by corrupting the database connection, removing the middleware is only hiding the problem, which is buggy middleware. Even worse, you're excluding the middleware from your tests, so now you have less test coverage of a component you might be using in production.

ganesanarun commented 3 years ago

I ran into same problem today when I used SimpleTestCase instead of TestCase, but overriding tearDownClass solved my problem though.

@classmethod
    def tearDownClass(cls):
        pass

Not sure, whether that will fix your problem.

delton-ecometrica commented 2 years ago

Has there been any update on this error

mahdyhamad commented 1 year ago

Same problem with Django == 3.2 and graphene-django == 2.15.0