Skip to content

Slice index false positive #3442

Closed
Closed

Description

@pkch
from typing import Union, Any

d: Union[int, None]
q: Any
q[0:d]  # error: Slice index must be an integer or None

Reported by @chrish42.

Activity

  1. gvanrossum commented on May 24, 2017

    @gvanrossum
    Member

    Probably a typeshed issue.

  2. JelleZijlstra commented on May 24, 2017

    @JelleZijlstra
    Member

    I don't think so, this is mypy's general slicing check, not a specific __getitem__ override. Looks like the bug is in visit_slice_expr in checkexpr.py. The error message says that int or None is accepted, but the code only accepts int.

  3. chrish42 commented on May 24, 2017

    @chrish42

    Furthermore, None as a slice index works in at least some cases:

    >>> range(10)[3:None]
    range(3, 10)
    

    So, at least for some cases, the code in visit_slice_expr should be more lenient and accept None as a slice index.

  4. gvanrossum commented on May 24, 2017

    @gvanrossum
    Member

    OK, I thought mypy usually eventually defer to a dunder method, but if it doesn't in this case it's a mypy bug, perhaps related to strict optional.

  5. added a commit that references this issue on May 29, 2017
    9ec8a95
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions