Skip to content

Typechecker does not check that "types" are actually types #1231

Closed
@sid-kap

Description

@sid-kap

MyPy does not show an error for the following code:

import typing

def foo(s: set[int]) -> int:
  return len(s)

def goo(l: list[int]) -> int:
  return len(l)

However, this code is not actually valid: it throws the following error at runtime:

Traceback (most recent call last):
  File "error.py", line 3, in <module>
    def foo(s: set[int]) -> int:
TypeError: 'type' object is not subscriptable

The first step to fixing this code is to change set to Set and list to List. This, however, leads to another runtime error:

Traceback (most recent call last):
  File "error.py", line 3, in <module>
    def foo(s: Set[int]) -> int:
NameError: name 'Set' is not defined

Finally, we need to add the following imports:

from typing import Set, List

and the program finally runs without an error.

So there are two issues here:

  1. mypy should catch when a "not subscriptable" thing (whatever that means) is written instead of an actual type
  2. mypy should detect when types are used without being imported

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