python-attrs / python-attrs/attrs

Allow custom hashing function in `_make_hash`

Open
#926 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature Thinking
Dominant language
Python
Stars
5.8k
Forks
480
Avg merge
2h 15m
Merged PRs (30d)
2

Description

Hi again!

As we know, hash(x) in python yields different values across different interpreter sessions for security. It turns out that I am using attrs in all of my class definitions in a library and would like to bootstrap off of the programmatically generated hash function to essentially create a merkle tree. I would like to use hashlib.md5() or something similar and create a digest using all the attributes attrs can find in the class so that:

  1. the hash is consistent across runs.
  2. less importantly for me, but maybe more importantly for others, using a better hash allows for less collisions.

As always, please let me know if this is something attrs can/cannot support, and I will work with/around it accordingly. Thanks!

P.S. My workaround would be to define my own function to find all attributes using attr.asdict() and then creating a hash that way. Not terrible, but I figured it'd be worth asking for a "purer" solution.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating _make_hash and tracing how attrs generates hashes from class attributes. Review how a caller could provide a custom hashing function and determine the expected stable digest and collision behavior. Done means the supported API is documented and covered by tests for consistent results across interpreter sessions.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.