Skip to content

Defining a class method list causes mypy [valid-type] errors #13050

@stinodego

Description

@stinodego

Bug Report

To Reproduce

Consider the following minimal Python class:

class Checks:
    def ok(self, input: list[str]) -> None:
        pass

    def list(self) -> None:
        pass

    def fail(self, input: list[str]) -> None:
        pass

Expected Behavior

I would expect that running mypy on this class passes just fine.

Actual Behavior

In fact, mypy finds an error:

bug.py:8: error: Function "bug.Checks.list" is not valid as a type  [valid-type]
bug.py:8: note: Perhaps you need "Callable[...]" or a callback protocol?

Note that the first method (ok) does not result in an error, while the last method (fail) does. Somehow mypy is confused by the second method being called list. Now it thinks that the type hint list[str] somehow refers to this class method, instead of the built-in generic type.

What is going on here?

Your Environment

  • Mypy version used: 0.961
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9.13
  • Operating system and version: WSL Ubuntu 20.04

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