Comments (5)
Seems to be a duplicate of #281
from dataclasses-json.
Oh! seems I missed it
anyways, I wrote a small function that gets all initvars from a dataclass, only did basic testing of course but maybe this could help:
@classmethod
def _get_initvar_names(cls) -> list[str]:
annotations = cls.__annotations__.copy()
for base in cls.__bases__:
if hasattr(base, "__annotations__"):
annotations.update(base.__annotations__)
return [name for name, typ in annotations.items() if isinstance(typ, dataclasses.InitVar)]
On another note, I saw that the issue remains open from 2021, are there currently any intentions to resolve it?
from dataclasses-json.
Hi @NiroHaim, this repo has been without much activity for a long time, so issues have been piling up. A few new maintainers have been added (including myself), so we will try to reduce the backlog as much as possible. This includes looking into the issue described here. I cannot guarantee when, but we will definitely have a look when time allows.
from dataclasses-json.
When I am looking at the documentation for InitVar
, it looks like it is supposed to be passed to the constructor, but it should not be accessible from the outside afterwards, e.g. through fields()
. Therefore, I would not expect it being returned from to_dict()
or to_json()
either. Therefore, A.from_dict(A(a_init=1).to_dict())
would never work if we were to respect this assumption.
@NiroHaim What are you expectations to handling this discrepancy?
Looping in contributors of the original issue @AbdulRahmanAlHamali and @ALiangLiang. Feel free to contribute to the discussion.
from dataclasses-json.
Hey @matt035343 , Thanks for the response
I believe that the correct behaviour would indeed not be to include a_init
as a key in the to_dict()
result, that would just be odd in the general case. (Also changing the output of to_dict
would probably be a pain for some projects to upgrade which you would probably want to avoid)
However, I do think that as the InitVars are valid init attributes that are expected to be received, I expect them not to be filtered out when a user chooses to add them to a dict and use from_dict
on said dict.
If you want to limit the usage of from_dict
to only receive input from the to_dict
function that Is fine, but if the purpose is for them to be seperate functions that also work in tandem, I think you should filter out all InitVars through a method similar to what I suggested above and just make sure that the values are there for the init call inside the from_dict
function.
**Do note that adding the option for passing initvars to from_dict
would allow fun features such as wrapping from_dict to change the private _a
to a_init
keyword and making the a
prop a counter of how many times this json has been converted to A
type by adding 1 to a_init
each time.
This is of course a rather dull case, however the option of passing InitVars would be very beneficial by allowing the user more freedom when using and wrapping the class.
from dataclasses-json.
Related Issues (20)
- [minimal-repro-testcase] from v0.5.12 on, tuple fields get truncated HOT 5
- [BUG] No way to default explicit nulls/Nones to a value
- [BUG] Numpy decode issue...
- [BUG] 0.6.2 changes CatchAll behavior in unexpected ways HOT 2
- [FEATURE] Omitting undefined Optional field when serializing HOT 2
- [BUG] Nested tuple with union of tuple does not deserialize correctly HOT 7
- [BUG] Nullable fields are not showing up when deserializing with field(default=None, metadata=config(exclude=lambda x: x is None)) HOT 4
- [BUG] Generic Collections fields - not respecting `default_factory` `field` specifications
- [BUG] new key "dataclass_json_config": null HOT 1
- AttributeError: 'list' object has no attribute 'keys' HOT 3
- [BUG] Decoding incompatible with Freezegun
- AttributeError: 'list' object has no attribute 'items' HOT 1
- [BUG] encoders / decoders don't apply to list / dict of the class
- [BUG] Getting `UserWarning` for defining an attribute with type `dict[str, bytes] | None`
- decoding optional decimal fields
- Parse JSON without modifying the dataclass HOT 1
- [BUG] SNAKE LetterCase conversion adding extra underscores for numeric parts HOT 1
- [BUG] SNAKE LetterCase conversion adding extra underscores for numeric parts
- [BUG] decorator @validates_schema not working when schema loads
- [FEATURE] Raise ValidationError if JSON doesn't match any of the types in Union
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 dataclasses-json.