django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

[BUG] all async test routers failed with TypeError: 'list' object is not callable

Open tim-hub opened this issue 7 months ago • 4 comments

Describe the bug all async test routers failed with

 TypeError: 'list' object is not callable

Everything was fine on 1.3.0 + 5.2, however, when I updated to 1.4.1 all tests failed.

Versions (please complete the following information):

  • Python version: 3.12
  • Django version: 5.2
  • Django-Ninja version: 1.4.1
  • Pydantic version: 2.11.3

Test case:

class TestTradeRouterWithData(TestCase):

    def setUp(self):
        # self.api = NinjaAPI()
        # self.api.add_router(router=trade_router, prefix=0)
        self.client = TestAsyncClient(trade_router)

        baker.make(
            "core.Trade",
            _quantity=10,
            _fill_optional=True,
            data={"trader_name": "Nancy Pelosi", "issuer_name": "Apple"},
        )
        baker.make(
            "core.Trade",
            _quantity=50,
            _fill_optional=True,
            traded_date=make_aware(
                datetime(2023, 1, 12, 0, 0, 0, 0), DEFAULT_TIMEZONE_INFO
            ),
            data={"trader_name": "trader-2", "issuer_name": "Google 2"},
        )
        baker.make(
            "core.Trade",
            _quantity=50,
            _fill_optional=True,
            data={"trader_name": "trader 3", "issuer_name": "Google"},
        )

        self.assertEqual(Trade.objects.all().count(), 110)
        super().setUp()

    def tearDown(self):
        del self.client
        Trade.objects.all().delete()
        super().tearDown()

    async def test_fetch_trade(self):
        response: NinjaResponse = await self.client.get("")
        self.assertEqual(response.status_code, 200)

It was fine on 1.3.0 but failed on 1.4.1 and 1.4.0 with TypeError: 'list' object is not callable

Is it a bug or did I not use the right way to write my tests?

Any ideas? thanks. :)

tim-hub avatar Apr 09 '25 03:04 tim-hub

~~I am trying to verify whether it is related to the change on ninja/testing/client.py~~

I tried to revert ninja/testing/client.py only, it does not help, so I think it is not related to the change on this ninja/testing/client.py.

tim-hub avatar Apr 09 '25 05:04 tim-hub

@tim-hub could you show full stack trace ?

vitalik avatar Apr 09 '25 07:04 vitalik

@tim-hub could you show full stack trace ?

Thanks @vitalik



Error
Traceback (most recent call last):
  File "~/Library/Caches/pypoetry/virtualenvs/tradeinsight.info-312/lib/python3.12/site-packages/asgiref/sync.py", line 254, in __call__
    return call_result.result()
           ^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "~/Library/Caches/pypoetry/virtualenvs/tradeinsight.info-312/lib/python3.12/site-packages/asgiref/sync.py", line 331, in main_wrap
    result = await self.awaitable(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/Development/ti/core/tests/api/test_trade_router.py", line 258, in test_fetch_trade
    response: NinjaResponse = await self.client.get("")
                              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/Development/ti/core/tests/api/test_trade_router.py", line 190, in _call
    return NinjaResponse(await func(request, **kwargs))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/Library/Caches/pypoetry/virtualenvs/tradeinsight.info-312/lib/python3.12/site-packages/ninja/operation.py", line 493, in _async_view
    return await cast(AsyncOperation, operation).run(request, *a, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/Library/Caches/pypoetry/virtualenvs/tradeinsight.info-312/lib/python3.12/site-packages/ninja/operation.py", line 347, in run
    error = await self._run_checks(request)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/Library/Caches/pypoetry/virtualenvs/tradeinsight.info-312/lib/python3.12/site-packages/ninja/operation.py", line 364, in _run_checks
    error = await self._run_authentication(request)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/Library/Caches/pypoetry/virtualenvs/tradeinsight.info-312/lib/python3.12/site-packages/ninja/operation.py", line 394, in _run_authentication
    return self.api.on_exception(request, exc)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/Library/Caches/pypoetry/virtualenvs/tradeinsight.info-312/lib/python3.12/site-packages/ninja/main.py", line 520, in on_exception
    return handler(request, exc)
           ^^^^^^^^^^^^^^^^^^^^^
  File "~/Library/Caches/pypoetry/virtualenvs/tradeinsight.info-312/lib/python3.12/site-packages/ninja/errors.py", line 129, in _default_exception
    raise exc  # let django deal with it
    ^^^^^^^^^
  File "~/Library/Caches/pypoetry/virtualenvs/tradeinsight.info-312/lib/python3.12/site-packages/ninja/operation.py", line 392, in _run_authentication
    result = callback(request)
             ^^^^^^^^^^^^^^^^^
TypeError: 'list' object is not callable

I think the error is raised from operation.py

tim-hub avatar Apr 09 '25 20:04 tim-hub

Any ideas for this issue, @vitalik

If there is no straight forward causes, I will just give up and stay on an old version for now.

tim-hub avatar May 27 '25 04:05 tim-hub

This issue seems being resolved in release 1.4.3

https://github.com/vitalik/django-ninja/releases/tag/v1.4.3t

Not sure how

tim-hub avatar Jun 16 '25 04:06 tim-hub