Skip to content

Crash with slices e.g. Annotated[int, 3:4] #10266

@patrick-kidger

Description

@patrick-kidger

Bug Report / To Reproduce / Actual Behavior

This:

# file.py
from typing import Annotated
def func(x: Annotated[int, 3:4]):
    pass

# command line
mypy file.py

produces:

file.py:4: error: syntax error in type comment
Found 1 error in 1 file (errors prevented further checking)

Your Environment

  • Mypy version used: 0.812
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9.2
  • Operating system and version: Windows 10

Further details

Any use of slices seems to give the error, e.g. Annotated[int, "a": 4].

I actually ran into this whilst implementing a type with a custom __class_getitem__, using slice notation as a convenient notation to pass pairs of values: MyType["key1": value1, "key2": value2]. Which I realise is a bit rogue -- I don't have any real hope of mypy integration for the type -- but it would at least be nice if it can be used without crashing mypy, with just a # type: ignore where it's imported.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions