python / python/mypy

"Can only assign concrete classes to a variable of type "Type[MyProtocol]"" is hard to debug

Open
#8,836 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug diagnostics priority-2-low topic-error-reporting topic-protocols topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

If you consider bad error messages a bug, it is a bug. Otherwise, it is a feature request.

The following code sample is wrong because name is not optional. Defining name's value in MyImplementation or its constructor would fix this code.

from typing import Type, Protocol

class MyProtocol(Protocol):
    name: str

class MyImplementation(MyProtocol):
    pass

foo: Type[MyProtocol] = MyImplementation

However, running mypy from master or 0.770 produces an error which is very challenging to debug:

main.py:9: error: Can only assign concrete classes to a variable of type "Type[MyProtocol]"

Ideally, mypy would tell me the underlying problem. In this case, I took nearly a whole day of experimenting to fix it, ultimately needing to delete code until I got the error message to change. It would be ideal if mypy could say "and it is not concrete because name is not defined statically or in __init__." or something.

  • What are the versions of mypy and Python you are using?
    mypy 0.770, python 3.8, yes it persists on master

  • What are the mypy flags you are using? (For example --strict-optional)

This behavior seems unchanged by flags.

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

Reproduce the example in main.py with mypy 0.770 or master, then trace the diagnostic that reports "Can only assign concrete classes to a variable of type Type[MyProtocol]." Done means the error explains why MyImplementation is not concrete, including the missing name definition described in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Feature
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.