Comments (6)
I added initial support in the following PR. It's not complete yet but your example will work on python 3.10+.
On Python < 3.10 it gives NameError: name 'PreCheckConfig' is not defined
. if you have any ideas on how to fix it, I'll be happy to listen. Otherwise I'll get to that later.
from mashumaro.
Good news! I managed to get it work on Python >= 3.8. If you have any other examples that don't work, I'd love to see them in a separate issue.
from mashumaro.
BTW, build_json_schema(Config)
also raises an exception
Traceback (most recent call last):
File "/home/ubuntu/workspace/example/python/repro.py", line 19, in <module>
print(build_json_schema(Config))
File "/home/ubuntu/workspace/example/.venv/lib/python3.10/site-packages/mashumaro/jsonschema/builder.py", line 45, in build_json_schema
schema = get_schema(instance, context, with_dialect_uri=with_dialect_uri)
File "/home/ubuntu/workspace/example/.venv/lib/python3.10/site-packages/mashumaro/jsonschema/schema.py", line 254, in get_schema
schema = schema_creator(instance, ctx)
File "/home/ubuntu/workspace/example/.venv/lib/python3.10/site-packages/mashumaro/jsonschema/schema.py", line 791, in on_collection
return on_typed_dict(instance, ctx)
File "/home/ubuntu/workspace/example/.venv/lib/python3.10/site-packages/mashumaro/jsonschema/schema.py", line 655, in on_typed_dict
properties={
File "/home/ubuntu/workspace/example/.venv/lib/python3.10/site-packages/mashumaro/jsonschema/schema.py", line 656, in <dictcomp>
key: get_schema(instance.derive(type=annotations[key]), ctx)
File "/home/ubuntu/workspace/example/.venv/lib/python3.10/site-packages/mashumaro/jsonschema/schema.py", line 254, in get_schema
schema = schema_creator(instance, ctx)
File "/home/ubuntu/workspace/example/.venv/lib/python3.10/site-packages/mashumaro/jsonschema/schema.py", line 709, in on_collection
if not issubclass(instance.origin_type, typing.Collection):
File "/usr/lib/python3.10/typing.py", line 1158, in __subclasscheck__
return issubclass(cls, self.__origin__)
File "/usr/lib/python3.10/abc.py", line 123, in __subclasscheck__
return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class
from mashumaro.
For comparison, it kinda works with pydantic 1.x. (I haven't tested 2.x):
from __future__ import annotations
from typing import TypedDict
from pydantic import create_model_from_typeddict
class PreCheckConfig(TypedDict):
enable: bool
class Config(TypedDict):
pre_check: PreCheckConfig
class ModelConfig:
extra = "forbid"
ConfigModel = create_model_from_typeddict(Config, __config__=ModelConfig)
print(ConfigModel.parse_obj({"pre_check": {"enable": True}}))
outputs:
pre_check={'enable': True}
from mashumaro.
Yeah, right now ForwardRef is not supported but I'm definitely planning on adding it later on. In the meantime, you can use dataclasses instead of TypedDict:
from __future__ import annotations
from dataclasses import dataclass
from mashumaro.mixins.dict import DataClassDictMixin
@dataclass
class PreCheckConfig:
enable: bool
@dataclass
class Config:
pre_check: PreCheckConfig
@dataclass
class ConfigChecker(DataClassDictMixin):
config: Config
ConfigChecker.from_dict({"config": {"pre_check": {"enable": True}}})
from mashumaro.
Thanks, Python 3.10 support is good for me already.
from mashumaro.
Related Issues (20)
- Switch to ruff from flake8
- Comprehension inlining on Python 3.12.0rc1 breaks some tests HOT 1
- Add support for Python 3.12
- Add support for PEP 696 HOT 1
- Can immutable dataclasses be hashed as dictionary keys? HOT 4
- Add an option to omit values equal to defaults on serialization
- Add an option to avoid collection data types copying
- Add support for LiteralString
- Generic dataclasses with Hashable typevar bound do not work HOT 3
- Query string support HOT 5
- Benchmark is not working in master branch HOT 2
- omit_default=True evaluates default_factory value at declaration time which can cause a circular reference HOT 3
- Multiple aliases for field HOT 6
- "`dict[str, T]` as a field type is not supported" HOT 2
- InitVar with no default value HOT 6
- The types defined inside the function result in syntactically invalid generated code
- Allow for more complex logic for subclass Discrimnator field matching HOT 3
- TypedDicts not working with `from __future__ import annotations` HOT 2
- omit_default breaks IntFlag serialization HOT 1
- Using Union with int/float casts to whichever appears first HOT 3
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 mashumaro.