Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
grahamc opened this issue May 16, 2020 · 4 comments

Comments

@grahamc
Copy link

grahamc commented May 16, 2020

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.

@msullivan
Copy link
Collaborator

Yeah that seems painful.

@elbaulp
Copy link

elbaulp commented Sep 28, 2020

I am having the same issue, how did you solve it? defining the attributes inside __init__ did not work

@grahamc
Copy link
Author

grahamc commented Sep 28, 2020 via email

@Hnasar
Copy link
Contributor

Hnasar commented Nov 13, 2023

You can use final to narrow down the abstract members:

from typing import Type, Protocol, final

class MyProtocol(Protocol):
    name: str

@final
class MyImplementation(MyProtocol):
    pass
# main.py:7: error: Final class __main__.MyImplementation has abstract attributes "name"  [misc]

foo: Type[MyProtocol] = MyImplementation

Perhaps the error message should allude to that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants