support decoding to `collections.Counter`
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.1k
- Forks
- 193
- Avg merge
- 3d 20h
- Merged PRs (30d)
- 15
Description
Description
This is something pydantic supports, but not msgspec:
from collections import Counter
c: Counter[str] = Counter()
c["abc"] += 3
c["xyz"] += 1
def pyd() -> None:
import pydantic
CounterStr = pydantic.TypeAdapter(Counter[str])
c_serialized = CounterStr.dump_json(c)
print(c_serialized)
c_deserialized = CounterStr.validate_json(c_serialized)
print(c_deserialized)
def msg() -> None:
import msgspec
CounterStr = msgspec.json.Decoder(Counter[str])
c_serialized = msgspec.json.encode(c)
print(c_serialized)
c_deserialized = CounterStr.decode(c_serialized)
print(c_deserialized)
pyd()
msg()
output:
b'{"abc":3,"xyz":1}'
Counter({'abc': 3, 'xyz': 1})
b'{"abc":3,"xyz":1}'
Traceback (most recent call last):
...
msgspec.ValidationError: Expected `Counter`, got `dict`
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the msgspec.json.Decoder(Counter[str]) example and compare its behavior with pydantic's Counter handling. Add support so decoding the encoded mapping returns a Counter with the same counts, and cover the behavior with a regression test for the shown example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100