Comments (5)
Thanks for reporting this - definitely looks like a typing bug. The concern I have here is that we can't use Sequence
as the type hint, as not all sequence types are JSON serializable (consider sets, for example).
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 :).
from pydantic.
One drawback is that this requires homogeneity in terms of the subtype of JsonValue
used within a list or dict, which we might not want...
from pydantic.
I'm pretty sure sets are not Sequence
s: https://mypy-play.net/?mypy=latest&python=3.12&gist=f71b466422216f85473dbb9880dd5f5c&flags=warn-unreachable.
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.
from pydantic.
Good point about sets. What about deque
types? Deques are sequences, but aren't json serializable...
from pydantic.
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.
from pydantic.
Related Issues (20)
- Dynamically include/exclude fields from dumps HOT 1
- `TypeAdapter` with Union has `Any` type hint argument starting with Pydantic 2.7.0+ HOT 4
- "NameError: name 'T' is not defined" when using "from __future__ import annotations" with generic function HOT 4
- Allow schema() to respect PlainSerializer annotations to fully enable using Enum names HOT 1
- `include` and `exclude` are not passed to field serializer contexts HOT 5
- @field_validator not working when wrapped with @classmethod HOT 1
- Attributes declared as iterables are replaced in the instances by `pydantic-core` `ValidatorIterator` instance HOT 9
- Unable to validate deep JSON data HOT 1
- Spurious detections of cyclic references HOT 2
- ValidationError when using Generic constructor for subfield HOT 2
- Set reusable field validator directly on Field HOT 1
- TypeAdapter for collections.abc.MutableMapping fails when validate_python called on a collections.UserDict in strict mode HOT 12
- Enum and min_length Validation HOT 2
- Serialization Warning when using Literal and model_dump HOT 4
- `SecretStr` fields throw type hint errors at user when passing a string HOT 2
- Nested models with model_validators sets a field to None HOT 2
- Validation of Enum Field with Decimal Fails HOT 5
- `model_validate_json` cannot validate `Hashable` HOT 1
- Python regex flags are not supported HOT 6
- Time serialization can silently drop timezone HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pydantic.