Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm weird Type warning [duplicate]

Tags:

python

pycharm

Why does the following code:

v = [None for _ in range(3)]
v[-1] = 0                       <<<

tell me this?

Unexpected type(s): (int, int) Possible types: (int, None) (slice, Iterable[None]) ...

like image 627
Omar Cusma Fait Avatar asked Sep 28 '19 10:09

Omar Cusma Fait


People also ask

How do I fix warnings in Pycharm?

Apply fixes in the Problems tool window All detected problems are listed in the left part of the tool window. Click a problem to display inspection details on the right. icon on the toolbar or in the context menu. You can also press Alt+Enter and select a suitable fix from the popup menu.

How do I remove duplicates in Pycharm?

Navigate to the duplicates in the editor by using the Jump to Source or Show Source context menu commands. Eliminate duplicates from the source code by applying the Extract method refactoring to the detected repetitive blocks of code that are found and highlighted automatically.

How do I ignore weak warnings in Pycharm?

Disable inspectionsIn the Settings/Preferences dialog ( Ctrl+Alt+S ), select Editor | Inspections. Locate the inspection you want to disable, and clear the checkbox next to it. Apply the changes and close the dialog.

What does duplicated code fragment mean?

Code Inspection: Duplicated code fragmentReports duplicated blocks of code from the selected scope: the same file or the entire project. The inspection features quick-fixes that help you to set the size of detected duplicates, navigate to repetitive code fragments, and compare them in a tool window.


1 Answers

A simple workaround to get rid of such warning is to put a type hint on v variable:

v: list = [None for i in range(3)]
v[-1] = 0

PyCharm will treat it as v: list[Any] and allow you to assign a value of any type without warnings.

like image 118
sanyassh Avatar answered Sep 27 '22 21:09

sanyassh