You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Structuring a non-dict type as TypedDict raises AttributeError #615
I am structuring a TypedDict type. The input is arbitrary (from JSON), and I want to offload the validation to cattrs.
The problem is, if the input is not a dict (in my case, not a JSON object), cattrs raises an AttributeError instead of the expected cattrs.errors.ClassValidationError.
Thanks! I will try it on my real example and let you know.
Regarding the fix, did you intentionally avoid an isinstance check? I wonder for example what happens if the input is a list (JSON array) -- a list does have a copy() method.
Hm, interesting. My thinking is this: catching an AttributeError adds essentially no overhead, and a separate isinstance check would be felt, especially on small dicts. It's also a little unclear what to check against (collections.abc.Mapping presumably).
Let me see what happens if a list is passed in when I get to a computer.
Activity
Tinche commentedon Jan 5, 2025
Hm, agreed. Let me get this for the next version.
Tinche commentedon Jan 6, 2025
I think I got this on main. Can you give it a try and let me know?
bluetech commentedon Jan 6, 2025
Thanks! I will try it on my real example and let you know.
Regarding the fix, did you intentionally avoid an
isinstancecheck? I wonder for example what happens if the input is a list (JSON array) -- a list does have acopy()method.Tinche commentedon Jan 6, 2025
Hm, interesting. My thinking is this: catching an AttributeError adds essentially no overhead, and a separate isinstance check would be felt, especially on small dicts. It's also a little unclear what to check against (
collections.abc.Mappingpresumably).Let me see what happens if a list is passed in when I get to a computer.
Tinche commentedon Jan 6, 2025
I think you're right, the error message kind of sucks when lists are involved.
Here's a new try: #617