Comments (2)
Hi @GuyKh
ResponseWithDescriptor[T].from_dict
is equal to ResponseWithDescriptor.from_dict
at runtime due to type erasure (this is how generic types work in Python). In your example, you just call from_dict
, which belongs to ResponseWithDescriptor[Any]
, so to speak.
if you look at how does it work, you will see that method from_dict
is compiled and set as attribute to your class. You have only one class here — ResponseWithDescriptor
. It’s not possible to set an attribute to ResponseWithDescriptor[T]
because at runtime it’s the same type.
I can see different options for you:
- Create a subclass of
ResponseWithDescriptor
withT
replaced by something specific (see example) - Use
ResponseWithDescriptor[T]
as a field type in another generic dataclass with parameterT
that will be parametrized later (as above) - Use codecs instead of mixins
from mashumaro.codecs import BasicDecoder
data_type_1_decoder = BasicDecoder(ResponseWithDescriptor[DataType1])
# create and reuse this decoder, because it’s expensive to create it from scratch each time you need it
data_type_1_decoder.decode({…})
from mashumaro.
@Fatal1ty works like a charm.
I took the 3rd option, creating for every DataType a member of the class (decoder):
// Contract.py
decoder = BasicDecoder(ResponseWithDescriptor[Contracts])
And then using it as follows:
from contract import Contract, Contracts
from contract import decoder as contract_decoder
...
def get_default_contract(token: JWT, bp_number: str) -> Contract:
return _get_response_with_descriptor(token, url, contract_decoder)
...
def _get_response_with_descriptor(jwt_token: JWT, request_url: str,
decoder: BasicDecoder[ResponseWithDescriptor[T]]) -> T:
...
return decoder.decode(response.json())
Thank you for this quick response - especially with the broad option range. Truly appreciated
from mashumaro.
Related Issues (20)
- Suppress warning about Union type HOT 4
- Take description from docstring HOT 4
- Incorrect type annotation for the result of `to_json` in `DataClassORJSONMixin`
- Different results between standard library json and orjson HOT 3
- Python 3.12.4 Compatibility
- Some tests error out with typing_extensions>=4.12
- More issues on Python 3.12.4 HOT 5
- [BUG] to_msgpack doesn't behave properly with Discriminator(include_subtypes=True) HOT 1
- [BUG] Can't generate expected JSON Schema when using `dict` HOT 6
- Unable to convert msg to msgpack (was originally working on mashumaro 2.9 HOT 1
- strict_map_keys for DataClassMessagePackMixin HOT 1
- unable to serialize torch.tensor as float HOT 1
- `Discriminator` not respected by `JSONEncoder` when it works for `to_json`. HOT 6
- Importing module in a particular order causes a SyntaxError HOT 2
- can not use `datetime` as `datetime` directly when using `from_dict` HOT 2
- Support slices as fields HOT 1
- Tuples are not hashable. HOT 7
- Unable to use PEP 604 annotations HOT 3
- Is it possible to serialise a dataclass-mixin within another dataclass-mixin? HOT 2
- Packaging for openSUSE: checks failing due to "invalid syntax", "ImportError"
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.