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]) ...
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With