PyO3 / PyO3/pyo3

`&mut self` reference in `__setstate__` signature is incompatible with frozen pyclasses

Open
#5,371 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

It'd be really nice if you could somehow return a new instance from __setstate__ instead of mutating an instance that is handed to you in the signature.

Right now that's not possible. See #4465 for more related discussion.

Consider the PyDecoder class in tokenizers:

https://github.com/huggingface/tokenizers/blob/95b882a3f5a61f95adc03b96e4c46e0b79b10d06/bindings/python/src/decoders.rs#L34

I can't easily make this class frozen because it has a __setstate__ implementation that takes &mut self. The only way to avoid that is to use interior mutability (e.g. with a Mutex) but I'm having trouble with that because PyDecoderWrapper contains an Arc<RwLock>. It occurs to me that I wouldn't need mutability at all for this class, except to make the class pickleable.

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

Read the discussion in #4465 and inspect the linked tokenizers bindings/python/src/decoders.rs entry at the PyDecoder setstate implementation, including PyDecoderWrapper. Trace the current PyO3 signature and frozen-class constraints; done means the supported deserialization behavior is defined and covered for classes that cannot mutate their instance.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.