Python Forum

Full Version: How to fix "'dict_values' object has no attribute 'inject_wsgi'" in session_transacti
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The problem is that when running tests on GitHub Workflow, the error AttributeError: 'dict_values' object has no attribute 'inject_wsgi' occurs and tests fails, but this error does not occur when running tests locally and everything goes well.

Full log error:
    ______ ERROR at setup of test_fetch_members ______
    
        @pytest.fixture(scope="function")
        def client() -> FlaskClient:
    >       yield create_client()
    
    unittests/tests/conftest.py:33: 
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
    unittests/tests/conftest.py:13: in create_client
        with client.session_transaction() as session:
    /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/contextlib.py:137: in __enter__
        return next(self.gen)
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
    
    self = <FlaskClient <Flask 'app'>>, args = ()
    kwargs = {'environ_overrides': {}}, app = <Flask 'app'>, environ_overrides = {}
    
        @contextmanager
        def session_transaction(
            self, *args: t.Any, **kwargs: t.Any
        ) -> t.Generator[SessionMixin, None, None]:
            """When used in combination with a ``with`` statement this opens a
            session transaction.  This can be used to modify the session that
            the test client uses.  Once the ``with`` block is left the session is
            stored back.
        
            ::
        
                with client.session_transaction() as session:
                    session['value'] = 42
        
            Internally this is implemented by going through a temporary test
            request context and since session handling could depend on
            request variables this function accepts the same arguments as
            :meth:`~flask.Flask.test_request_context` which are directly
            passed through.
            """
            if self.cookie_jar is None:
                raise RuntimeError(
                    "Session transactions only make sense with cookies enabled."
                )
            app = self.application
            environ_overrides = kwargs.setdefault("environ_overrides", {})
    >       self.cookie_jar.inject_wsgi(environ_overrides)
    E       AttributeError: 'dict_values' object has no attribute 'inject_wsgi'
    /opt/hostedtoolcache/Python/3.11.3/x64/lib/python3.11/site-packages/flask/testing.py:146: AttributeError
conftest.py

    from flask.testing import FlaskClient
    from app import app_instance
    import pytest

    def create_client() -> FlaskClient:
        app_instance.app.config["TESTING"] = True
    
        with app_instance.test_client as client:
            with app_instance.context:
                with client.session_transaction() as session:
                    session["logged_in"] = True
            return client
    
    
    @pytest.fixture(scope="function")
    def client() -> FlaskClient:    
        yield create_client()
app.py

    from flask.testing import FlaskClient
    from flask import Flask
    
    
    class App:
        def __init__(self):
            self.app = Flask(__name__)
    
        def run(self, **args):
            self.app.run(**args)
    
        @property
        def test_client(self) -> FlaskClient:
            return self.app.test_client()
    
        @property
        def context(self) -> FlaskClient:
            return self.app.app_context()
    
        @property
        def flask_app(self) -> Flask:
            return self.app
    
    app_instance = App()
GitHub Workflow:
    name: Python application
    
    on:
      push:
        branches: [ "master" ]
      pull_request:
        branches: [ "master" ]
    
    permissions:
      contents: read
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/checkout@v3
        - name: Set up Python 3.11.3
          uses: actions/setup-python@v3
          with:
            python-version: "3.11.3"
    
        - name: Install dependencies
          run: |
            python -m pip install --upgrade pip
            pip install -r requirements.txt
    
        - name: Test with pytest
          run: |
            pytest
I hope for your help in trying to figure out why this error occurs on GitHub.