New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(typing): use abstract container types where possible #832
refactor(typing): use abstract container types where possible #832
Conversation
Hm, I'll need to check why the tests are failing for Linux and macOS. Surprisingly, they're passing for Windows. 🤔 |
I agree with the change, once the CI goes ✔️ . Keep in mind that probably those annotations have a runtime effect with Pydantic. It might be the cause of the test problems. |
I think you're right that the problem originates from Pydantic. Pydantic v1.10.2 handles the from typing import Any, Dict, Mapping
from pydantic import parse_obj_as
from pydantic.dataclasses import dataclass
@dataclass
class Model:
m: Mapping[Any, Any]
d: Dict[Any, Any]
v = [["key", "value"]]
x = parse_obj_as(Model, {"m": v, "d": v})
print(x.m) # ['key']
print(x.d) # {'key': 'value'} This behavior of |
I've reverted one usage of |
I've created an issue over at Pydantic to indicate the |
I've submitted a PR to Pydantic which fixes the I suggest to proceed here without waiting for that PR to get merged because it's uncertain if and when it'll get merged and when a patch release for Pydantic will become available since they're working on Pydantic v2. |
It seems this bug won't be fixed in Pydantic v1 and parsing a list of pairs as a dict seems to be a coincidental feature (see pydantic/pydantic#4588 (comment)). |
Yet again... I can't believe how much often that fails... 🤦🏼♂️ |
I've replaced a few occurrences of concrete container types (
Dict
andList
) by abstract container types (Mapping
andSequence
) which is slightly better practice AFAIK. See, e.g.,for an interesting article on this topic.