“TypeError:float参数required,not dict”使用.format方法时出错

时间:2014-10-27 22:40:53

标签: python python-requests string.format

我试图在字符串中输出与http请求相关的数据:

r = requests.post(url, data, params)
assert r.status_code == 200, '\nurl - {}\nresponse status - {}\nrequest headers - {}\nrequest body - {}\nresponse headers - {}\nresponse body - {}'.format(
        r.url, r.status_code, r.request.headers, r.request.body, r.headers, r.text)

在我的本地计算机(python 2.7.5)上运行它非常有效,所有相关数据都会显示和格式化。

从Jenkins构建作业执行此操作时,它将返回异常:

      assert r.status_code == 200, '\nurl - {url}\nresponse status - {status}\nrequest headers - {r_headers!s}\nrequest body - {r_body}\nresponse headers - {headers}\nresponse body - {body}'.format(
            url=r.url, status=r.status_code, r_headers=r.request.headers, r_body=r.request.body, headers=r.headers,
            body=r.text)
       TypeError: float argument required, not dict
     

test_watch.py​​:58:TypeError

通过将.format方法更改为仅一次报告一个变量进行一些调试,我发现Jenkins作业在插入请求标头时遇到字符串格式问题。是的,它是一个字典,但.format()方法应该处理这个就好了。为什么抱怨类型?

完整的回溯并非特别有用:

      assert r.status_code == 400, 'Negative test failure' \
                                     '\nurl - {url}\nresponse status - {status}\nrequest headers - {r_headers!s}\nrequest body - {r_body}\nresponse headers - {headers}\nresponse body - {body}'.format(
            url=r.url, status=r.status_code, r_headers=r.request.headers, r_body=r.request.body, headers=r.headers,
            body=r.text)
     

E TypeError:需要float参数,而不是dict

test_watch.py​​:58:TypeError

总结:这适用于我的本地机器,请求标头以字符串输出:

assert r.status_code == 400, 'Negative test failure' \
                                 '\nrequest headers - {r.request.headers}'.format(r=r)

从Jenkins构建服务器运行相同的代码:

      assert r.status_code == 400, 'Negative test failure' \
                                     '\nrequest headers - {r.request.headers}'.format(r=r)
       TypeError: float argument required, not dict
       test_watch.py:58: TypeError

更新:我更改了代码以捕获TypeError,然后引发了一个自定义AssertionError,从相同的部分(我复制/粘贴)以相同的方式构建。这非常令人困惑,为什么.format单向工作,但完全失败了另一个?

try:
            assert r.status_code == 400, 'Negative test failure' \
                                         '\nurl - {r.url}\nresponse status - {r.status_code}\nrequest headers - {r.request.headers}\nrequest body - {r.request.body}\nresponse headers - {r.headers}\nresponse body - {r.text}'.format(r=r)
except TypeError as e:
            message = 'TypeError raised! {}'.format(e)
            message += '\nurl - {r.url}\nresponse status - {r.status_code}\nrequest headers - {r.request.headers}\nrequest body - {r.request.body}\nresponse headers - {r.headers}\nresponse body - {r.text}'.format(r=r)
            raise AssertionError(message)

执行此代码时,我得到:

E               AssertionError: TypeError raised! float argument required, not dict
E               url - http://api4.qa.ebay.com/user/v1/watchlist/watch?listingStatus=BLARGLE&limit=25
E               response status - 200
E               request headers - {<good headers outputted as a dict>}
E               request body - <good content here>
E               response headers - {no problems}
E               response body - {all my data was printed!}

1 个答案:

答案 0 :(得分:0)

我仍然不知道为什么会出现这种错误,但我能够解决它:

最初的Jenkins实例在Mac OS 10.9.4上运行我的工作。设置我的工作以在10.9.5上运行新的从服务器允许我的代码成功而无需更改。

OSX 10.9.4和10.9.5之间是否存在已知问题?