Skip to content

'"dict" is not subscriptable' error when using a type alias for dict in a .pyi stub file, with Python <= 3.8 #11859

Closed

Description

@AlexWaygood

Bug Report

This error only occurs when mypy is run using Python <= 3.8. Here is a minimal reproducible example:

_DictReadMapping = dict

class Foo(_DictReadMapping[str, int]): ...  # error: "dict" is not subscriptable
Bar = _DictReadMapping[int, str]  # error: "dict" is not subscriptable

The following, however, do not cause an error in Python <= 3.8 in a .pyi stub file:

_DictReadMapping = dict

Baz: _DictReadMapping[str, int]  # type alias used as an annotation: okay
class Spam(dict[str, int]): ...  # `dict` used as superclass without a type alias: okay

Expected Behavior

In Python <= 3.8, dict is not subscriptable at runtime. However, it should be subscriptable in a .pyi stub file, which does not need to ever be "run". Moreover, it is subscriptable in a .pyi file if a type alias is not being used. The behaviour should be the same, whether or not you are using a type alias.

Your Environment

This bug has been reproduced on MacOS, Ubuntu and Windows on Python 3.6, 3.7 and 3.8. The mypy version being used is 0.930.

Use case

I came across this bug in an early draft of python/typeshed#6717. As you can see, using this kind of syntax fails the typeshed CI for mypy, but not for any other type checker. (In this case, there was an easy workaround.)

Activity

  1. AlexWaygood commented on Dec 29, 2021

    @AlexWaygood
    MemberAuthor

    (Cc. @JukkaL, @sobolevn, since this is somewhat related to python/typeshed#4820)

  2. sobolevn commented on Dec 29, 2021

    @sobolevn
    Member

    Related to #11863

    I can refactor TypeAlias logic after my initial PR is merged 👍

  3. added a commit that references this issue on Jan 6, 2022
    fcf949f
  4. added 2 commits that reference this issue on Jan 6, 2022
    e0a89b7
    221a7f3
  5. added a commit that references this issue on Jan 20, 2022
    dc87dfa
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