Coder Social home page Coder Social logo

Add support for ForwardRef about mashumaro HOT 6 CLOSED

messense avatar messense commented on August 18, 2024
Add support for ForwardRef

from mashumaro.

Comments (6)

Fatal1ty avatar Fatal1ty commented on August 18, 2024 1

@messense

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.

Fatal1ty avatar Fatal1ty commented on August 18, 2024 1

@messense

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.

messense avatar messense commented on August 18, 2024

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.

messense avatar messense commented on August 18, 2024

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.

Fatal1ty avatar Fatal1ty commented on August 18, 2024

@messense

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.

messense avatar messense commented on August 18, 2024

Thanks, Python 3.10 support is good for me already.

from mashumaro.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.