[Python] Python Warning - Expected type 'Union[Integral, slice]', got 'str' instead

Discussão em 'Python' iniciado por Stack, Julho 25, 2025.

  1. Stack

    Stack Membro Participativo

    My code below in python is giving me a warning on the line:

    some_new_object['someVar'] = cd['someVar']


    The warning is

    Expected type 'Union[Integral, slice]', got 'str' instead


    Code:

    def some_object():
    return {
    'someId': 0,
    'someVar' : ''
    }

    def warn_test(in_list):
    try:
    new_list = []
    some_new_object = some_object()

    for cd in in_list:
    if cd['someVar']:
    new_list.append(cd)

    for cd in new_list:
    some_new_object['someVar'] = cd['someVar']
    in_list.append(some_new_object.copy())

    return in_list
    except Exception:
    print 'baaa'

    #Main Program
    new_obj = some_object()
    new_obj['someId'] = 1
    new_obj['someVar'] = 'Next'

    new_obj2 = some_object()
    new_obj2['someId'] = 1
    new_obj2['someVar'] = None

    new_list = []
    new_list.append(new_obj)
    new_list.append(new_obj2)

    out_list = warn_test(new_list)
    for obj in out_list:
    print obj


    If I change the function warn_test to this:

    def warn_test(in_list):
    try:
    new_list = []
    some_new_object = some_object()

    for cd in in_list:
    if cd['someVar']:
    some_new_object['someVar'] = cd['someVar']
    new_list.append(some_new_object.copy())

    for cd in new_list:
    in_list.append(cd)

    return in_list
    except Exception:
    print 'baaa'


    It gives me no warning.

    Can someone help me to understand why I get the warning, and how I can access the cd['someVar'] in the second iteration without getting a warning?

    I know this code is weird, I need this for a project I am working on, I made this test to share here, but it gives me the same Warning so a solution for this will fix it in my system. (No warnings is one of the Must Haves for this system)

    Continue reading...

Compartilhe esta Página