You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Recognizing wrapped classes as supporting a Protocol #6568
When running mypy on this code, I'd expect no warning, but I get
test.py:16: error: Argument 1 to "dataclass_only" has incompatible type "A"; expected "IsDataclass".
While checking for __dataclass_fields__ might not look like the best thing to go for, it's what dataclasses.is_dataclass checks for as well, so this seems like the most reliable condition right now.
I'd assume it happens because the dataclass-wrapper adds this attribute, and mypy only checks the class definition itself. Can this be fixed?
Reacted by Lundy Bernard, Ryan Soklaski, Joshua Olson, Felipe S. S. Schneider, Jasha Sommer-Simpson and Ellis Breen
This can be supported by just adding __dataclass_fields__Var to the symbol table in the plugin. This is quite exotic use case, so setting priority to low. On the other hand, if you have time, PRs are welcome, this shouldn't be hard to fix.
I'm not 100% sure what you mean with adding the symbol to the plugin table, do you mean this?
@dataclassclassA:
__dataclass_fields__: Dict
Because that does fix it without side effects, since the dataclass wrapper just overrides it. Then again, once I start adding fields to my dataclasses only to fulfill a protocol I might as well inherit from a classic no-op interface class.
Low prio is what I think as well, I'll look if I can manage to write a proper fix.
So we are talking about mypy/plugins/dataclasses.py here, right @ilevkivskyi ?
I would like to have that fixed because I see no other reliable way of type hinting data classes.
Have never touched mypy source code, but I can give it a try.
For now, in order to leave it commented here, I managed to track the type checking to the subtypes submodule. Today, I am simply trying to get the gist of what is mypy's flow.
The function call that isn't returning what we expect in order to validate the protocol is at subtypes.py(481):
subtype=find_member(member, left, left)
Where member is the attribute we are checking (i.e.,__dataclass_fields__), and left is A class.
Given self.names is an empty SymbolTable, name is never in cls.names. Tweaking with dataclasses' symbol table should make __dataclass_fields__ visible here, then making A compliant with the protocol IsDataclass.
It seems I was following the wrong thread there 😅 Ended up at the semanal.analyze_class class, which analyzes the definitions (defn.defs.body). Still, that was very instructive :)
How does Var relate to TypeInfo? Creating a Var via Var(random_name, Dict[Any, Any] initializes it with VAR_NO_INFO, for example.
I see it is assigned at some places, like semanal.make_name_lvalue_var
Sorry for the lack of update, got caught up in a series of other stuff at the second semester of 2019.
Even though I debugged and inspected some of the mypy code, I could not manage to figure out the right way to approach it nor actually understand what is the mental model used on it.
I would like to see this resolved as well. I left some comments addressing the outstanding change requests on #8578, hopefully this would unblock the author. Though it looks like he will need to address some merge conflicts as well.
If the author is unresponsive, is it good practice to create a duplicate PR with his changes?
Activity
ilevkivskyi commentedon Apr 2, 2019
This can be supported by just adding
__dataclass_fields__Varto the symbol table in the plugin. This is quite exotic use case, so setting priority to low. On the other hand, if you have time, PRs are welcome, this shouldn't be hard to fix.a-recknagel commentedon Apr 3, 2019
I'm not 100% sure what you mean with adding the symbol to the plugin table, do you mean this?
Because that does fix it without side effects, since the dataclass wrapper just overrides it. Then again, once I start adding fields to my dataclasses only to fulfill a protocol I might as well inherit from a classic no-op interface class.
Low prio is what I think as well, I'll look if I can manage to write a proper fix.
ilevkivskyi commentedon Apr 3, 2019
You misread what I wrote, I meant fixing this in mypy plugin for dataclasses (dataclasses in mypy are supported by a bundled/builtin plugin).
a-recknagel commentedon Apr 3, 2019
I see, thanks for the info =)
viniciusd commentedon Aug 13, 2019
So we are talking about
mypy/plugins/dataclasses.pyhere, right @ilevkivskyi ?I would like to have that fixed because I see no other reliable way of type hinting data classes.
Have never touched mypy source code, but I can give it a try.
ilevkivskyi commentedon Aug 13, 2019
Yes.
Please go ahead! If you feel stuck, feel free to ask here or on Gitter.
viniciusd commentedon Aug 13, 2019
I will certainly have some questions, thanks :)
For now, in order to leave it commented here, I managed to track the type checking to the
subtypessubmodule. Today, I am simply trying to get the gist of what is mypy's flow.The function call that isn't returning what we expect in order to validate the protocol is at
subtypes.py(481):Where member is the attribute we are checking (i.e.,
__dataclass_fields__), and left isAclass.find_memberconsists of:Where:
Given
self.namesis an emptySymbolTable,nameis never incls.names. Tweaking with dataclasses' symbol table should make__dataclass_fields__visible here, then makingAcompliant with the protocolIsDataclass.Thoughts here so far?
ilevkivskyi commentedon Aug 14, 2019
Yes, we already add
__init__and other things to the symbol table inplugins/dataclasses.py, just add aVarthere with a type likeDict[str, Any].viniciusd commentedon Aug 14, 2019
It seems I was following the wrong thread there 😅 Ended up at the
semanal.analyze_classclass, which analyzes the definitions (defn.defs.body). Still, that was very instructive :)Gonna go for the plugin now, thanks
viniciusd commentedon Aug 14, 2019
How does
Varrelate toTypeInfo? Creating aVarviaVar(random_name, Dict[Any, Any]initializes it withVAR_NO_INFO, for example.I see it is assigned at some places, like
semanal.make_name_lvalue_varilevkivskyi commentedon Aug 15, 2019
For instance/class attributes,
Var.infoshould be set to the class where it was defined (can be get asClassDefContext.cls.info).viniciusd commentedon Aug 15, 2019
That makes sense, thanks!
Aren't we supposed to have some sort of
Dictnode-type atmypy/types.py? I seeTypedDictthere, which doesn't seem to be the right fit here1 remaining item
amosson commentedon Jan 7, 2020
I couldn't tell if there was any progress on this so I attempted solution. It seems to work, but I'm sure I'm missing some things.
My solution was to add the following to the DataclassTransformer.transform method
I tried to give dataclass_fields a type but couldn't get the syntax of
ctx.api.named_type()correct to set it toDict[str, Any]Any / all comments are appreciated.
viniciusd commentedon Jan 7, 2020
Sorry for the lack of update, got caught up in a series of other stuff at the second semester of 2019.
Even though I debugged and inspected some of the mypy code, I could not manage to figure out the right way to approach it nor actually understand what is the mental model used on it.
finete commentedon May 10, 2020
@ilevkivskyi is what @amosson has written right? Any comments?
I would like the help fix this, but unsure how to proceed.
@amosson can you create a PR or fork? and we will work on this.
ilevkivskyi commentedon May 11, 2020
This sounds reasonable. Note there is a draft PR #8578, but I don't know if the author is still interested.
lundybernard commentedon Jul 30, 2021
I have been waiting on a fix for this for a while. Is there anything we can do to get #8578 moving again?
EdwardLarson commentedon Aug 11, 2021
I would like to see this resolved as well. I left some comments addressing the outstanding change requests on #8578, hopefully this would unblock the author. Though it looks like he will need to address some merge conflicts as well.
If the author is unresponsive, is it good practice to create a duplicate PR with his changes?
lundybernard commentedon Aug 20, 2021
tkukushkin updated the branch and fixed the failing tests last week. Its just waiting on a review from @JelleZijlstra.
Add __dataclass_fields__ and __attrs_attrs__ to dataclasses (#8578)
fix ConfigProtocol checking
fix ConfigProtocol checking
IoannRove commentedon Nov 22, 2022
Hi! It seems to have broken in the version 0.991
hauntsaninja commentedon Nov 22, 2022
I think you'll need to specify it as a ClassVar
IoannRove commentedon Nov 22, 2022
It works. Thank you!
Auto-update pre-commit hooks