TypeError:从 jira 获取拒绝原因的信息时,“NoneType”对象不可订阅

问题描述 投票:0回答:1

对于问题中的问题:

    fields = issue['fields']
    results.append({
        'key': issue['key'],
        'summary': fields['summary'],
        'description': fields['description'],
        'fix_details': get_custom_field_value(fields, 'customfield_25600'),  # Replace with actual custom field ID
        'url': f"{JIRA_SERVER}/browse/{issue['key']}",
        'status' : fields['status']['name'],
        'rejected_reason': fields['customfield_19822']['value']
    })

我有一段Python代码,但是当我尝试执行它时,它在最后一行的这个块中失败了

'rejected_reason': fields['customfield_19822']['value']
,因为有多个记录,该字段值未填充或为
None
。此代码适用于该字段存在一些有效值的记录。

File "c:\projects\LL\Jira_LL\jira_LL.py", line 84, in query
    'rejected_reason': fields['customfield_19822']['value']
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not subscriptable

我尝试过添加if else

 'rejected_reason': fields['customfield_19822']['value']
             if rejected_reason is not None: 
                   rejected_reason={rejected_reason}
             else
                   rejected_reason=NULL

但这失败了

     if rejected_reason is not None:
                               ^
SyntaxError: invalid syntax
python dictionary
1个回答
0
投票

正如您发现的,您不能将完整的

if
/
else
块放入字典块内。您可以使用条件表达式(有时称为三元表达式)。

所以虽然这看起来像你想要的,但它不起作用:

d = {
    if data['x'] is None:
        'field': 'empty'
    else:
        'field': data['x']['y']
}

请改用条件表达式:

d = {
    'field': data['x']['y'] if data['x'] is not None else 'empty'
}

# or with more error checking for this specific case:
d = {
    'field': data['x']['y'] if 'x' in data and data['x'] is not None else 'empty'
}

© www.soinside.com 2019 - 2024. All rights reserved.