-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Mypy does not recognize aliases of dataclasses.dataclass #5383
Comments
It's unlikely that mypy will support |
In the mean time, perhaps a single sentence added to the documentation might be helpful. |
That makes sense. Since you know where you (would have) looked in the docs, can you help us by sending a Pull Request? |
This documentation is a temporary solution. It should be altered or removed when python#5383 is fixed
Support for the The workaround I've resorted too is double-wrapping:
I'd also be wrapping the fields in |
Does there not exist any annotation or cast that will tell mypy to treat some variable as if it were Here's some of my attempts, with corresponding errors:
Next is my attempt to use annotations to request that mypy treat
Using cast rather than an annotation actually produces the exact same errors:
From my perspective, the problem here is that the special-case handling for dataclasses.dataclass isn't attached to its type. What is it in fact attached to? Its name? I don't know, but I expect it's the name. Adding one level of abstraction would fix the problem, I believe: define a type for P.S. I hope my use case isn't relevant, but: My intent is to ease the use of immutable-by-default design in my team by making a clean alias of |
If you need help figuring this out today, I recommend asking in the typing discussions group: https://github.com/python/typing/discussions |
If this proposal was implemented I think you'd be able to do something like this
which would be neat. |
To me, this improvement seems much too specific to I think the Right Way to fix this is |
I would like this too, but it might have been easier if there were a |
I think I just got this working by adding a custom mypy plugin like this: from typing import Type
import mypy.plugins.dataclasses
from mypy.plugin import Plugin
class CustomPlugin(Plugin):
pass
def plugin(version: str) -> Type[CustomPlugin]:
mypy.plugins.dataclasses.dataclass_makers.add('tjax._src.dataclasses.dataclass.dataclass')
# mypy.plugins.dataclasses.field_makers.add('tjax._src.dataclasses.helpers.field')
return CustomPlugin |
The dataclass transforms PEP (681) would be a great addition to MyPy 😄 It would solve this problem, and the problem of base classes that define dataclasses. |
Perhaps this issue should be renamed "Implement PEP 681" to give it a bit more visibility? It's a huge deal for projects like Flax that extensively use dataclasses (both using a modified decorator |
Use PEP 681, which is implemented. If you need something that is truly, exactly a dataclass alias, you can do something like |
In Python 3.7.0 and mypy
0.630+dev-3fb16a2b8c5439074e39b75feebc109a439eede3
, if I save the following code tomypy-test.py
I get the following mypy output
Notice that mypy has no trouble recognizing the signature of
A
's constructor. I can understand how mypy would have a hard time withdataclass_wrapper
, anddataclass_alias
is probably not critical in terms of use case. However,dataclass_alias2
is important if you want (as I do) to define many data classes with the same options, especially if you want to make these options available as part of your module's public interface.The text was updated successfully, but these errors were encountered: