Skip to content

Reopen #9221? error: Invalid index type "slice[int, None, None]" for "str";... #21777

Description

@edreamleo

See #9221 (closed)

Bug Report

Upgrading to mypy 2.3.0 introduced the bug.

Very strangely, reverting to mypy 2.2.0 did not change the behavior described next.

Also, the behavior seems independent of which of Leo's GitHub branches is active.

To Reproduce

cd c:\Repos\leo-editor
python -m leo.scripts.mypy_leo leo

The following mypy-leo.cmd script seems to produce the same results:

@echo off
cd c:\Repos\leo-editor
python -m leo.scripts.mypy_leo

Actual Behavior

mypy is now complaining about virtually every string slice within Leo. For example:

def findDirective(self, directive_name: str) -> bool:
    if directive_name.startswith('@'):
        directive_name = directive_name[1:]

produces this complaint:

leo\core\leoNodes.py:754: error: Invalid index type "slice[int, None, None]" for "str"; expected type "SupportsIndex | slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex]"  [index]

Expected Behavior

No complaints!

Your Environment

  • Mypy version used: mypy 2.3.0 (compiled: yes)
  • Mypy command-line flags: None.
    leo.scripts.mypy_leo may set flags, but the results don't seem to change.
  • Mypy configuration options from mypy.ini: See next section
  • Python version used: Python 3.14.0

.mypy.ini

# Config file for mypy

# Note: Do not put comments after settings.

[mypy]
python_version = 3.14
cache_dir = mypy_cache

# Defaults for all files.
check_untyped_defs = True
follow_imports = skip
ignore_missing_imports = True
incremental = False
show_error_codes = True
strict_optional = False

disable_error_code=attr-defined

# For v0.931, per https://github.com/python/mypy/issues/11936

exclude =
    
    # The first line must *not* start with |.
    # Thereafter, each line *must* start with |.
    # No trailing '|' on last entry!

    # Directories...
    doc/|dist/|editpane/|examples/|extensions/|external/|modes/|obsolete/|scripts/|themes/|unittests/|www/|
    
    # Special plugins...
    |freewin.py|scriptFile.py|beautify_node.py|viewrendered3|

    # Experimental plugins...
    |leo_pdf|leo_to_rtf|mnplugins|mod_speedups|settings_finder|stickynotes_plus|
    
    # qt_main.py is generated automatically, so it's pointless to add annotations.
    |qt_main

# #4766: more specific descriptions override less specific.
[mypy-leo.core.leoApp,leo.core.leoConfig,leo.core.leoNodes,leo.core.leoGlobals,leo.core.leoCommands]
strict_optional = True

Summary

This is an extremely strange bug.

Happily, adding disable_error_code=attr-defined,index to .mypy.ini calms mypy down.

Any suggestions would be greatly appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions