cattrs does not unstructure os.PathLike to str
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 42/100
Research direction
Start by reproducing the example with cattrs 23.1.2 and inspect how unstructuring handles the os.PathLike annotation compared with pathlib.Path and Union[str, os.PathLike]. The issue is resolved when the bar field is unstructured to a string consistently and a regression test covers the reported example.
Written by the indexing model from the issue text.
Description
- cattrs version: 23.1.2
- Python version: 3.11.3
- Operating System: Arch Linux
Description
When unstructuring an attrs class where one of the fields is annotated as os.PathLike (and contains a Path), that field remains a Path (in my case, PosixPath) instead of being unstructured as str.
Curiously, annotating the field as Union[str, os.PathLike] or pathlib.Path (without changing the actual data type) resolves the issue.
What I Did
In [1]: import os
...: from pathlib import Path
...: from typing import Union
...:
...: import attrs
...: import cattrs
...: import cattrs.preconf.json
In [2]: @attrs.define
...: class MyClass:
...: foo: Union[str, os.PathLike] = Path("/foo")
...: bar: os.PathLike = Path("/bar")
...:
In [3]: cattrs.preconf.json.make_converter().unstructure(MyClass())
Out[3]: {'foo': '/foo', 'bar': PosixPath('/bar')}
In [4]: cattrs.unstructure(MyClass())
Out[4]: {'foo': '/foo', 'bar': PosixPath('/bar')}
I'm not saying that this must be a bug (rather than merely my mis-usage of cattrs), but I was not able to find anything in the docs that would suggest that I was doing something incorrectly.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 159
- Avg merge
- 12h 21m
- Merged PRs (30d)
- 6
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.
More from python-attrs/cattrs
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
python-attrs/cattrs#761 · 2 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
python-attrs/cattrs#513 · 4 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
python-attrs/cattrs#779 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 85/100
python-attrs/cattrs#774 ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
python-attrs/cattrs#746 · 2 comments ·
All issues in python-attrs/cattrs
Similar issues
-
triage/confirmed
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
agentscope-ai/agentscope#2775 ·
-
comp/desktop P3 type/bug
Difficulty 1/5 Under an hour Newbie friendliness 92/100
NousResearch/hermes-agent#118866 ·
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 90/100
apache/cloudstack#14222 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100