DEV Community

Farah BELDJOUDI
Farah BELDJOUDI

Posted on

Why my dataclass test failed when the values matched

logstats is a file parser script that takes an auth log file path as an argument and reports 4 stats on about it (number of lines, number of IP lines, number of unique IPs and a list with the 5 top IPs in terms of frequency of appearance). It uses a generator to yield information at every line carrying an IP followed by " port" of the file and it catches FileNotFoundError and IsADirectoryError exceptions.

In the making of this script, I've encountered a few problems, the most recent one is figuring out why my test failed when I made the IP and time returned as type-hinted dataclasses instead of dictionary items, and I figured out it was because I created two dataclasses with the same name and the same fields in both the script and the test file. It was tricky to figure out because the traceback shows they have the same value but the == assertion fails nonetheless. Something other than the fields had to differ, so I checked where the test's Record came from and the file had its own copy of the class defined. The test now imports Record from the script and therefore the same class.

This has made me learn that a dataclass's == only compares instances of the same class.
https://github.com/akaligakiru04/logstats

Top comments (0)