question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: index() got an unexpected keyword argument 'headers' with sample code

See original GitHub issue

I have written a pytest version of the sample code.

import elasticsearch

class FooService:

    def __init__(self):
        self.es = elasticsearch.Elasticsearch(hosts=[{'host': 'localhost', 'port': 9200}])

    def create(self, index, body):
        es_object = self.es.index(index, body)
        return es_object.get('_id')

    def read(self, index, id):
        es_object = self.es.get(index, id)
        return es_object.get('_source')

import pytest
from elasticmock import elasticmock

@elasticmock
def test_create_and_read_object():
    # Variables used to test
    index = 'test-index'
    expected_document = {
        'foo': 'bar'
    }

    # Instantiate service
    service = FooService()

    # Index document on ElasticSearch
    id = service.create(index, expected_document)
    assert id is not None

    # Retrieve document from ElasticSearch
    document = service.read(index, id)
    assert expected_document == document

When I run it I get the following error

$ pytest em.py 
========================================== test session starts ===========================================
platform darwin -- Python 3.7.6, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: /Users/wmcneill/Documents/Misc
plugins: celery-4.4.2
collected 1 item                                                                                         

em.py F                                                                                            [100%]

================================================ FAILURES ================================================
______________________________________ test_create_and_read_object _______________________________________

    @elasticmock
    def test_create_and_read_object():
        # Variables used to test
        index = 'test-index'
        expected_document = {
            'foo': 'bar'
        }
    
        # Instantiate service
        service = FooService()
    
        # Index document on ElasticSearch
>       id = service.create(index, expected_document)

em.py:31: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
em.py:9: in create
    es_object = self.es.index(index, body)
../../greenfield/document_store/venv/lib/python3.7/site-packages/elasticmock/behaviour/server_failure.py:27: in decorated
    response = f(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

args = (<elasticmock.fake_elasticsearch.FakeElasticsearch object at 0x1051cc110>, 'test-index', {'foo': 'bar'})
kwargs = {}, params = {}, headers = {}, p = 'request_timeout'

    @wraps(func)
    def _wrapped(*args, **kwargs):
        params = {}
        headers = {}
        if "params" in kwargs:
            params = kwargs.pop("params").copy()
        if "headers" in kwargs:
            headers = {
                k.lower(): v for k, v in (kwargs.pop("headers") or {}).items()
            }
        if "opaque_id" in kwargs:
            headers["x-opaque-id"] = kwargs.pop("opaque_id")
    
        for p in es_query_params + GLOBAL_PARAMS:
            if p in kwargs:
                v = kwargs.pop(p)
                if v is not None:
                    params[p] = _escape(v)
    
        # don't treat ignore, request_timeout, and opaque_id as other params to avoid escaping
        for p in ("ignore", "request_timeout"):
            if p in kwargs:
                params[p] = kwargs.pop(p)
>       return func(*args, params=params, headers=headers, **kwargs)
E       TypeError: index() got an unexpected keyword argument 'headers'

../../greenfield/document_store/venv/lib/python3.7/site-packages/elasticsearch/client/utils.py:92: TypeError
======================================== short test summary info =========================================
FAILED em.py::test_create_and_read_object - TypeError: index() got an unexpected keyword argument 'head...
=========================================== 1 failed in 0.28s ============================================

This is ElasticMock version 1.5.0 and elasticsearch version 7.6.0.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
stbecommented, Apr 8, 2020

Hey! I happened to stumble across the same issue. I believe the reason is that some methods have changed between elasticsearch-py 6.x and 7.x and now also accept an optional headers argument. For example the ping:

In 6.x:

def ping(self, params=None):

In 7.x:

def ping(self, params=None, headers=None):

So the methods in elasticmock/fake_elasticsearch.py do not account for that parameter that gets applied by the query_params decorator.

0reactions
DementeTech2commented, Apr 22, 2020

If it worth I have the same issue with info() and because I’m working with elasticsearch 6.8 I just fix the library version:

elasticsearch>=6.0.0,<7.0.0

That solve my issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

so-elastalert-test: unexpected keyword argument 'headers' error
so-elastalert-test: unexpected keyword argument 'headers' error. ... TypeError: deprecated_search() got an unexpected keyword argument ...
Read more >
python - __init__() got an unexpected keyword argument ...
However, when this is run, I get the error that 'headers' is an unexpected argument. This is the code in my views.py:
Read more >
[Answered]-__init__() got an unexpected keyword argument ...
Coding example for the question __init__() got an unexpected keyword argument 'headers' ... I get the error that 'headers' is an unexpected argument....
Read more >
Flask got an unexpected keyword argument path
I can't use orca because I get the following error: The plotly. TypeError: softmax() got an unexpected keyword argument 'axis' 解决 ...
Read more >
Python elasticsearch client : TypeError: index() got an ...
Running the getting started example from elasticsearch docs (Python ... TypeError: index() got an unexpected keyword argument 'document'.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found