-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
"Invalid type" when using class attribute as type variable. #7052
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
Comments
Accessing nested class on an instance is not considered statically decidable. You should instead use it as class attribute: @dataclass
class SearchToken:
column: Pg.Column |
(For the better error message we already have an issue #4030) |
Oh wait sorry, I thought it is a nested class, but it is actually a variable, this is even more tricky. You can try |
Thanks for your reply!
Do you mean like this: class Pg:
Column = Union[Column] ?
My example is a simplified version of the actual code. The original version was rather like this:
@dataclass
class Column:
name: str
# More stuff going on here
class Pg:
Column = Column
# More stuff going on here
async def connect(self):
self.pool = await init_pg()
pg = Pg() The from postgres import pg and have everything they need. That's why I ran into the following issue in
from postgres import pg
@dataclass
class SearchToken:
column: pg.Column
# More stuff going on here I fixed the issue by moving all pg types ( from postgres import pg, pgtype But now I have to work with |
Yes, exactly, and then you should be able to use it as
Well, some patterns that come from dynamic typing world are sometimes not very reasonable in the static world. Some things can be mitigated by writing a mypy plugin if this is really important (I think someone tried to write a plugin for |
Are you reporting a bug, or opening a feature request?
A bug I guess. Please excuse me if I'm missing something here.
Code to reproduce the issue:
What is the actual behavior/output?
Mypy marks the last line as an error:
What is the behavior/output you expect?
Mypy not complaining about the last line or a better error message, that guides me somewhere to solve the given scenario. I feel kinda lost with this right now.
What are the versions of mypy and Python you are using?
Python 3.7.3
mypy 0.710
Do you see the same issue after installing mypy from Git master?
I couldn't get this to work with
pipenv
.What are the mypy flags you are using?
I don't use any.
The text was updated successfully, but these errors were encountered: