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 01 Answer
Reset to default 2This 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
版权声明:本文标题:python - Pylance doesn't recognize list[int] as a type annotation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742396334a2466996.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论