DEV Community

Pavel Morava
Pavel Morava

Posted on

Dataclasses in Python 3.6

In my project about which I wrote in Django to the rescue I am stuck with Python 3.6.

While I have no bigger regrets about Python 3.8 as walrus operator is fine but not a killer feature for me, I was always regretful about not having dataclasses which were introduced in Python 3.7.

Yesterday, when I run pip list in my virtual environment, to my astonishment I found dataclasses in the list. Obviously, some of my libraries installed them along.

Immediately, I searched PyPI to confirm that my hunch was correct. Dataclasses has a backport for Python 3.6 here.

So if you are in a similar situation like me, you can add them to your project as an external dependency.

Discussion (4)

Collapse
mariofix profile image
Mario Hernandez

It's a very nice module replacement.
Now try to use typed functions along with dataclasses in 3.6 😁

Collapse
hanpari profile image
Pavel Morava Author

Is there any problem?

Collapse
mariofix profile image
Mario Hernandez

Yes for me. But it's more a compatibility issue. Only when I use dataclasses

Thread Thread
hanpari profile image
Pavel Morava Author

I guess so. I know that typing evolved pretty much in new Pythons. I wish I could use structural typing instead nominal.