Comments (8)
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.
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.
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.
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.
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.
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.
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.
The new changes are in 2.0 now.
from mashumaro.
Related Issues (20)
- 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
- Not parsing Generics correctly HOT 2
- Unserializable field in 3.12 if defined as a Generic TypeVar with mixin bounds HOT 6
- Allow propagation of class based discriminator settings to subclasses 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.