python / python/mypy

Bad error message while checking subclass of generic classes

Open
#11,040 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-error-reporting
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

mypy command on a misspecified subclass of a generic class returns a pretty confusing error message (As per issue #8696)

To Reproduce

  1. Create a script containing this snippet:
from abc import ABC, abstractmethod
from typing import TypeVar, Iterable, Iterator

T = TypeVar('T')


class MyABC(ABC):

    @property
    @abstractmethod
    def items(self) -> Iterable[T]: ...

    def my_method(self) -> Iterator[T]:
        for item in self.items:
            yield item
  1. Run mypy on the created script

Expected Behavior

Since MyABC should inherit also from Generic[T] I'd expect that the error message would return something on the line of:

error: Incompatible types in "yield". The class should inherit from "Generic[T]"

Actual Behavior

The actual error message is:

error: Incompatible types in "yield" (actual type "T", expected type "T")

Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): default
  • Python version used: 3.6.5
  • Operating system and version: MacOS BigSur 11.5.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running mypy on the minimal script in the issue and confirm the confusing yield diagnostic. No repository file or test is named; trace the diagnostic from the mypy command entry point, then verify that the misspecified generic subclass produces an actionable message identifying the missing Generic[T] inheritance.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.