admin管理员组

文章数量:1335552

I want to use the built-in type annotation list[int] instead of typing.List[int], but Pylance doesn't recognize list[int] as a valid type expression. What's weird is that there is no issue when I do type annotation for the function argument variable. How can I get Pylance to understand that list[int] is a valid type annotation?

Pylance error:

Variable not allowed in type expression Pylance (reportInvalidTypeForm)

Terminal error:

PS C:\Users\user\Programming\Python\pythonED> & "C:/Program Files/Python313/python.exe" "c:/Users/user/Programming/Python/pythonED/exercise14.py"
Traceback (most recent call last):
  File "c:\Users\user\Programming\Python\pythonED\exercise14.py", line 8, in <module>
    list: list[int] =  [1, 3, 5, 3, 5, 6, 8, 4, 23, 7, 8, 5, 7, 5, 7, 9, 9, 5, 4, 7, 5]
          ~~~~^^^^^
TypeError: list indices must be integers or slices, not type

This is my code:

def loop_list(b_list: list[int]) -> list[int]:
    list = []
    for i in b_list:
        if i not in list:
            list.append(i)
    return list

list: list[int] =  [1, 3, 5, 3, 5, 6, 8, 4, 23, 7, 8, 5, 7, 5, 7, 9, 9, 5, 4, 7, 5]
print(loop_list(list))

The only solution I found to use was the typing module's List function. But after some reading, it appeared that the typing module has become antiquated due to built-in type annotations in newer versions of Python. So, I would rather use the built-in feature than the module.

I want to use the built-in type annotation list[int] instead of typing.List[int], but Pylance doesn't recognize list[int] as a valid type expression. What's weird is that there is no issue when I do type annotation for the function argument variable. How can I get Pylance to understand that list[int] is a valid type annotation?

Pylance error:

Variable not allowed in type expression Pylance (reportInvalidTypeForm)

Terminal error:

PS C:\Users\user\Programming\Python\pythonED> & "C:/Program Files/Python313/python.exe" "c:/Users/user/Programming/Python/pythonED/exercise14.py"
Traceback (most recent call last):
  File "c:\Users\user\Programming\Python\pythonED\exercise14.py", line 8, in <module>
    list: list[int] =  [1, 3, 5, 3, 5, 6, 8, 4, 23, 7, 8, 5, 7, 5, 7, 9, 9, 5, 4, 7, 5]
          ~~~~^^^^^
TypeError: list indices must be integers or slices, not type

This is my code:

def loop_list(b_list: list[int]) -> list[int]:
    list = []
    for i in b_list:
        if i not in list:
            list.append(i)
    return list

list: list[int] =  [1, 3, 5, 3, 5, 6, 8, 4, 23, 7, 8, 5, 7, 5, 7, 9, 9, 5, 4, 7, 5]
print(loop_list(list))

The only solution I found to use was the typing module's List function. But after some reading, it appeared that the typing module has become antiquated due to built-in type annotations in newer versions of Python. So, I would rather use the built-in feature than the module.

Share Improve this question edited Nov 22, 2024 at 12:48 InSync 10.9k4 gold badges17 silver badges56 bronze badges asked Nov 19, 2024 at 21:53 myzomyzo 1 0
Add a comment  | 

1 Answer 1

Reset to default 2

This is happening because you have a variable called list, which is shadowing the builtin type. To fix this, just give the variable a different name:

nums: list[int] =  [1, 3, 5, 3, 5, 6, 8, 4, 23, 7, 8, 5, 7, 5, 7, 9, 9, 5, 4, 7, 5]

This is good practice in general since you should avoid shadowing builtins whenever possible.

Pylance doesn't like your current code because list is a variable, and if it let you use variables for type annotations, it wouldn't be a "static" type checker anymore. Python doesn't like your code because if list is an instance of Python's builtin list class, list[int] means "the int'th item in the list," not "a type annotation representing a list that contains ints."

Your type annotations in the original function don't trip either of those because at that point, list hasn't been redefined yet.

本文标签: pythonPylance doesn39t recognize listint as a type annotationStack Overflow