Skip to content

Colon slice behaves differently from built-in slice function #11640

Closed

Description

@gao462

Bug Report

Slice using colon notation has different behavior from slice using built-in slice function.

To Reproduce

Use colon notation will have error message.

import numpy as onp
import numpy.typing as onpt
imin: onpt.NDArray[onp.int64] = onp.array(0)
imax: onpt.NDArray[onp.int64] = onp.array(2)
M = onp.array([1.0, 2.0, 3.0, 4.0])
print(M[imin:imax])
error: Slice index must be an integer or None  [misc]
Found 1 error in 1 file (checked 1 source file)

But using built-in slice function has no problem.

import numpy as onp
import numpy.typing as onpt
imin: onpt.NDArray[onp.int64] = onp.array(0)
imax: onpt.NDArray[onp.int64] = onp.array(2)
M = onp.array([1.0, 2.0, 3.0, 4.0])
islice = slice(imin, imax)
print(M[islice])
Success: no issues found in 1 source file

Expected Behavior

Colon slice should has no error message as built-in slice function.

Actual Behavior

Colon slice has error message which is unexpected.

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
plugins = "numpy.typing.mypy_plugin"
check_untyped_defs = true
disallow_any_generics = true
ignore_missing_imports = true
no_implicit_optional = true
show_error_codes = true
strict_equality = true
warn_redundant_casts = true
warn_return_any = true
warn_unreachable = true
warn_unused_configs = true
no_implicit_reexport = false
  • Python version used: 3.9.7
  • Operating system and version: Linux

Activity

  1. sobolevn commented on Dec 3, 2021

    @sobolevn
    Member

    CC @Zac-HD

    It might be related to the recent feature you were interested in 🙂

  2. Zac-HD commented on Dec 3, 2021

    @Zac-HD
    Contributor

    Nope, not related - my PR was type-level slicing, this is value-level.

  3. erictraut commented on Aug 12, 2023

    @erictraut

    It looks like this bug has been fixed. I'm not able to repro the error using mypy 1.5.

  4. hauntsaninja commented on Aug 13, 2023

    @hauntsaninja
    Collaborator

    I fixed this in #14738

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

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions