Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mashumaro explodes when used as a vendor'd install #60

Closed
alinsavix opened this issue Nov 27, 2021 · 5 comments
Closed

mashumaro explodes when used as a vendor'd install #60

alinsavix opened this issue Nov 27, 2021 · 5 comments

Comments

@alinsavix
Copy link

I have a project where I am installing mashumaro in a "vendor" subdirectory (via pdistx) for use by my code; because of the environment the code is running in, installing mashumaro globally is not an option, neither is updating PYTHON_PATH or similar. This means that my import line in my code ends up needing to look like: from vendor.mashumaro import DataClassJSONMixin

Setting up the vendor directory (see reproduction steps below) and running the following script:

from dataclasses import dataclass
from vendor.mashumaro import DataClassJSONMixin

@dataclass
class TestClassTwo(DataClassJSONMixin):
    field1: int

@dataclass
class TestClassOne(DataClassJSONMixin):
    field1: TestClassTwo

...results in the following stack trace:

Traceback (most recent call last):
  File "D:\Users\username\Documents\testdir\test2.py", line 10, in <module>
    class TestClassOne(DataClassJSONMixin):
  File "D:\Users\username\Documents\testdir\vendor\mashumaro\serializer\base\dict.py", line 14, in __init_subclass__
    builder.add_from_dict()
  File "D:\Users\username\Documents\testdir\vendor\mashumaro\serializer\base\metaprogramming.py", line 229, in add_from_dict
    self._from_dict_set_value(fname, ftype, metadata, alias)
  File "D:\Users\username\Documents\testdir\vendor\mashumaro\serializer\base\metaprogramming.py", line 250, in _from_dict_set_value
    unpacked_value = self._unpack_field_value(fname=fname, ftype=ftype, parent=self.cls, metadata=metadata)
  File "D:\Users\username\Documents\testdir\vendor\mashumaro\serializer\base\metaprogramming.py", line 800, in _unpack_field_value
    raise UnserializableField(fname, ftype, parent)
vendor.mashumaro.exceptions.UnserializableField: Field "field1" of type TestClassTwo in TestClassOne is not serializable

This appears to happen because of the following hardcoded class name in meta/helpers.py:

DataClassDictMixinPath = "mashumaro.serializer.base.dict.DataClassDictMixin"

...which is no longer correct when the code is run in this way (the correct path would be vendor.mashumaro.serializer.base.dict.DataClassDictMixin).

I'm not sure what the best way to fix this is, but in the extremely short term I'm able to patch the class name for my specific use case. It would be great if I was able to vendor an install without having to edit it afterwards!

Reproduction steps:

  1. install pdistx if you don't already have it installed
  2. Create an empty directory for the test
  3. In that directory, create a requirements.txt file, as such:
mashumaro==2.9
msgpack==1.0.3
pyyaml==6.0
typing-extensions==4.0.0
  1. execute pdistx vendor -r requirements.txt vendor. This will create a 'vendor' subdirectory with mashumaro and its dependencies
  2. Create a test script using the test script at the beginning of this issue, execute it, and observe the stack trace.

Thanks for the excellent tool!

@Fatal1ty
Copy link
Owner

Hi @alinsavix

It could be replaced with something like this:

DataClassDictMixinPath = f"{__name__.rsplit('.', 2)[:-2][0]}.serializer.base.dict.DataClassDictMixin"

I could make this change next week.

@alinsavix
Copy link
Author

Verified, this does indeed solve the problem (and seems to do so across several different usage scenarios). Excellent. Will definitely look forward to seeing this fix rolled in, when you have the opportunity to do so.

Thanks for the quick response!

@Fatal1ty
Copy link
Owner

Fixed in develop branch

@Fatal1ty
Copy link
Owner

Fixed in 2.9.1.

@alinsavix
Copy link
Author

<3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants