-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
JsonValue is contravariant, should be covariant #9445
Comments
Thanks for reporting this - definitely looks like a typing bug. The concern I have here is that we can't use Here's one possible alternative: if TYPE_CHECKING:
# This seems to only be necessary for mypy
J = TypeVar('J', bound='JsonValue')
JsonValue: TypeAlias = Union[
List[J],
Dict[str, J],
str,
bool,
int,
float,
None,
] Let me know your thoughts on that. I can open a PR to fix this issue if you approve of said approach as well :). |
One drawback is that this requires homogeneity in terms of the subtype of |
I'm pretty sure sets are not What would be the problem with JsonValue: TypeAlias = Union[
Sequence['JsonValue'],
Mapping[str, 'JsonValue'],
str,
bool,
int,
float,
None,
] ? Other than this discriminator having to be made more general. |
Good point about sets. What about |
Can you give a runnable example? I don't understand why it isn't possible to serialize any sequence as a JSON array and to deserialize any JSON array as a list. |
Initial Checks
Description
See the code sample. It produces the following error in mypy:
and in pyright:
This works with an identical type where
types.Dict
is replaced withcollections.abc.Mapping
andtypes.List
is replaced withcollections.abc.Sequence
.Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: