Git Product home page Git Product logo

Comments (8)

Fatal1ty avatar Fatal1ty commented on May 23, 2024 1

I changed the way howSerializationStrategy classes are used. @rominf you might be interested in reading README here and here. At the moment these changes are in the master branch but I will release 2.0 version soon since it's not backward compatible.

from mashumaro.

Andrei-Pozolotin avatar Andrei-Pozolotin commented on May 23, 2024

perhaps a better approach is to use separate serialization annotations. i.e.:

@dataclass
class DateTimeFormats(DataClassDictMixin):
    @FormattedDateTime(fmt='%d%m%Y%H%M%S') 
    short:datetime
    @FormattedDateTime(fmt='%A %B %d, %Y, %H:%M:%S')
    verbose:datetime

from mashumaro.

dand-oss avatar dand-oss commented on May 23, 2024

from future import annotations
from mashumaro import DataClassJSONMixin

@DataClass
class aClass(DataClassJSONMixin):
mem_t: member_TT # un declare

@DataClass
class member_TT(DataClassJSONMixin):
pass

produces

File "/i/t/miniconda/lib/python3.7/site-packages/mashumaro/serializer/base/dict.py", line 19, in init_subclass
raise exc
File "/i/t/miniconda/lib/python3.7/site-packages/mashumaro/serializer/base/dict.py", line 15, in init_subclass
builder.add_to_dict()
File "/i/t/miniconda/lib/python3.7/site-packages/mashumaro/serializer/base/metaprogramming.py", line 163, in add_to_dict
for fname, ftype in self.fields.items():
File "/i/t/miniconda/lib/python3.7/site-packages/mashumaro/serializer/base/metaprogramming.py", line 53, in fields
for fname, ftype in typing.get_type_hints(self.cls).items():
File "/i/t/miniconda/lib/python3.7/typing.py", line 976, in get_type_hints
value = _eval_type(value, base_globals, localns)
File "/i/t/miniconda/lib/python3.7/typing.py", line 263, in _eval_type
return t._evaluate(globalns, localns)
File "/i/t/miniconda/lib/python3.7/typing.py", line 467, in _evaluate
eval(self.forward_code, globalns, localns),
File "", line 1, in
NameError: name 'member_TT' is not defined

from mashumaro.

rominf avatar rominf commented on May 23, 2024

It breaks my program in Python 3.8 too. I would consider this as a bug, but not an enhancement.

Are there plans to fix this?

from mashumaro.

Fatal1ty avatar Fatal1ty commented on May 23, 2024

perhaps a better approach is to use separate serialization annotations. i.e.:

@dataclass
class DateTimeFormats(DataClassDictMixin):
    @FormattedDateTime(fmt='%d%m%Y%H%M%S') 
    short:datetime
    @FormattedDateTime(fmt='%A %B %d, %Y, %H:%M:%S')
    verbose:datetime

The current implementation of SerializationStrategy is a dirty hack that won't work in Python 3.10. At this moment I'm working on improving the customization capabilities of mashumaro and I think I can replace SerializationStrategy with something like this:

@dataclass
class DateTimeFormats(DataClassDictMixin):
    short: datetime = field(
        default_factory=datetime.now,
        metadata={
            "serialization_strategy": FormattedDateTime("%d%m%Y%H%M%S")
        },
    )
    verbose: datetime = field(
        default_factory=datetime.now,
        metadata={
            "serialization_strategy": FormattedDateTime("%A %B %d, %Y, %H:%M:%S")
        },
    )

Since in would break backward compatibility I could do it in version 2.x. But anyway with upcoming changes in this library you can do the same things from the example even without SerializationStrategy:

@dataclass
class DateTimeFormats(DataClassDictMixin):
    short: datetime = field(
        default_factory=datetime.now,
        metadata={
            'deserialize':
                lambda s: datetime.strptime(s, '%d%m%Y%H%M%S')
        }
    )
    verbose: datetime = field(
        default_factory=datetime.now,
        metadata={
            'deserialize':
                lambda s: datetime.strptime(s, '%A %B %d, %Y, %H:%M:%S')
        }
    )

or with SerializationStrategy if you'd like:

@dataclass
class DateTimeFormats(DataClassDictMixin):
    short: datetime = field(
        default_factory=datetime.now,
        metadata={
            'deserialize': FormattedDateTime("%d%m%Y%H%M%S")._deserialize
        }
    )
    verbose: datetime = field(
        default_factory=datetime.now,
        metadata={
            'deserialize': FormattedDateTime("%A %B %d, %Y, %H:%M:%S")._deserialize
        }
    )

I haven't done serialize option yet but it's definitely needed and will be done soon. Also from this point of view serialization_strategy option in metadata would be no more than syntax sugar that would allow you not to define both serialize and deserialize options.

So, if anyone is interested in SerializationStrategy suggestions and discussion are welcome.

from mashumaro.

Fatal1ty avatar Fatal1ty commented on May 23, 2024

from future import annotations
from mashumaro import DataClassJSONMixin

@DataClass
class aClass(DataClassJSONMixin):
mem_t: member_TT # un declare

@DataClass
class member_TT(DataClassJSONMixin):
pass

@dand-oss your example is about another known problem with forward references #28

from mashumaro.

Fatal1ty avatar Fatal1ty commented on May 23, 2024

It breaks my program in Python 3.8 too. I would consider this as a bug, but not an enhancement.

Are there plans to fix this?

It's not a bug but a hacky feature that should be reimplemented in another way :)

from mashumaro.

Fatal1ty avatar Fatal1ty commented on May 23, 2024

The new changes are in 2.0 now.

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.