I am attempting to just add a CSV file to my issues as a test, but I keep receiving the error:
```
RuntimeError: dictionary keys changed during iteration
```
Here is the code (I've removed the parameters for server, username and password:
```
from jira import JIRA
options = {"server": "serverlinkgoeshere"}
jira = JIRA(options, basic_auth=('username', 'password'))
issuesList = jira.search_issues(jql_str='', startAt=0, maxResults=100)
for issue in issuesList:
with open("./csv/Adobe.csv",'rb') as f:
jira.add_attachment(issue=issue, attachment=f)
f.close()
```
I'm at a loss, I'm not changing any dictionary keys in my code. Here is the full error message:
```
Traceback (most recent call last):
File "C:/Users/USER/PycharmProjects/extractor/main/jiraCSVDupdate.py", line 8, in <module>
jira.add_attachment(issue=issue, attachment=f)
File "C:\Users\USER\AppData\Roaming\Python\Python38\site-packages\jira\client.py", line 126, in wrapper
result = func(*arg_list, **kwargs)
File "C:\Users\USER\AppData\Roaming\Python\Python38\site-packages\jira\client.py", line 787, in add_attachment
url, data=m, headers=CaseInsensitiveDict({'content-type': m.content_type, 'X-Atlassian-Token': 'nocheck'}), retry_data=file_stream)
File "C:\Users\USER\AppData\Roaming\Python\Python38\site-packages\jira\utils\__init__.py", line 41, in __init__
for key, value in super(CaseInsensitiveDict, self).items():
RuntimeError: dictionary keys changed during iteration
```
References:
Jira add_attachment example:
https://jira.readthedocs.io/en/master/examples.html#attachments
add_attachment source code:
https://jira.readthedocs.io/en/master/_modules/jira/client.html#JIRA.add_attachment
The bug is in the jira library. From .../jira/utils/__init__.py:
for key, value in super(CaseInsensitiveDict, self).items():
if key != key.lower():
self[key.lower()] = value
self.pop(key, None)
Should be something like:
for key in [x for x in super(CaseInsensitiveDict, self).keys() if x != x.lower()]:
self[key.lower()] = self.pop(key, None)
(Iterate over a copy of the keys that need to be modified instead of over the dict to be modified.)
Modifying a dict while iterating over it has always been a bug, but Python 3.8 enforces this.
Any timeline on implementing suggested fix? Being able to use 3.8 would solve some dependency hell I'm currently facing.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Still looking to see if there is a fix as of end of May 2020
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The fix is to stop using code that does the wrong thing, as given December 2019
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The official fix hasn't been merged yet but exists here https://github.com/pycontribs/jira/pull/877/commits/9caed80a031421182102d64ea0d4645947fd2e64
They essentially iterate over the dictionary looking for non lowercase key names, then in the next loop they recreate the key in lowercase and remove the one with uppercase. Two separate loops, rather than modifying the dict while also iterating it.
You need to modify the site-packages/jira/utils/__init__.py file and make the changes you see at the link above, until it gets merged and pushed out officially.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello, @Chuck_Philips. Thanks! It works.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Solution. Changed Python Interpreter from 3.8 to 3.7
Hope this helps someone else in the future.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Helped me today, thank you!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What if i can't? (the code runs on a remote vm not under my management)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I agree - the problem is not the version of Python (nor is it in the Jira API), the problem is in the code, and your patch corrects it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You'll need to rework your python code so that it does not alter the object while it is running over it.
This is a slightly different error, but is the same solution - https://hackernoon.com/filtering-dictionary-in-python-3-3eb99f92e6ee
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Guys, it's really quite weird especially in case Ubuntu 20.04 ('Python 3.8.2' version by default). I need to do custom migrations for some project, but I can't do this due to described problem with Python 3.8.2. So, it would be really helpful to get proper solution ASAP! Please, check Chuck_Phillips suggestion. Is it suitable? Thank you in advance!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Nic Brough -Adaptavist- can you please advise about this part:
from jira import JIRA
jira = JIRA(options, basic_auth=(user, apikey))
project = jira.create_project('TTTP', 'Temporary Toast Test Project')
print(project)
After its execution I get an error also:
'RuntimeError: dictionary keys changed during iteration'
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Your code is altering a collection while the collection is being iterated over.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Nic Brough -Adaptavist- why are you telling people who are simply using an API provided to them, to stop using it? It is up to the developers to officially fix this problem.
The problem lies in the Jira module, not any persons individual code presented here. Its not their code, its the API code that is behaving badly.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You need to be clear about what you mean by API.
You need to be clear that there is nothing wrong with the Jira API. The problem is with the Python module. Until the Python (module) developers fix that, the only useful advice here is "don't use functions that are broken", or, better, "use the calls the right way"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.