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

TypeError on _map_subobject #22

Open
ericPavone opened this issue Apr 19, 2024 · 3 comments
Open

TypeError on _map_subobject #22

ericPavone opened this issue Apr 19, 2024 · 3 comments

Comments

@ericPavone
Copy link

The error occurs in the _map_subobject function when attempting to instantiate a new object incorrectly. Specifically, the error arises in the else block when obj is not a dictionary and the skip_none_values flag is set to True. In this case, the code attempts to create a new object of the same type as obj by filtering out null or empty values from the sequence using a list comprehension. However, the object instantiation fails because the constructor for the object is unable to handle the additional argument passed through the list comprehension. This results in a TypeError indicating that the constructor expected only one positional argument but received two.

@anikolaienko
Copy link
Owner

Hi @ericPavone,
Thank you for reporting the issue. Sorry for taking a long time to reply.
I'm looking into it and try to reproduce.
Meanwhile if you have a working test case you can provide it would really help.
Thanks.

@ericPavone
Copy link
Author

ericPavone commented May 13, 2024

Hi @anikolaienko i tryied to contrib pushing the fix but with python 3.12 i got some issues on running the tests... anyway

   def _map_subobject(
        self, obj: S, _visited_stack: Set[int], skip_none_values: bool = False
    ) -> Any:
        """Maps subobjects recursively"""
        if _is_primitive(obj) or _is_enum(obj):
            return obj

        obj_id = id(obj)
        if obj_id in _visited_stack:
            raise CircularReferenceError()

        if type(obj) in self._mappings:
            target_cls, _ = self._mappings[type(obj)]
            result: Any = self._map_common(
                obj, target_cls, _visited_stack, skip_none_values=skip_none_values
            )
        else:
            _visited_stack.add(obj_id)

            if _is_sequence(obj):
                if isinstance(obj, dict):
                    result = {
                        k: self._map_subobject(
                            v, _visited_stack, skip_none_values=skip_none_values
                        )
                        for k, v in obj.items()
                        if v or not skip_none_values
                    }
                else:
                    if skip_none_values:
                        result = type(obj)(  # type: ignore [call-arg]
                            [x for x in obj if x]
                        )
                    else:
                        result = type(obj)(obj)
            else:
                result = deepcopy(obj)

            _visited_stack.remove(obj_id)

        return result
    This is the method fixed, i addedd a check for the null values if "skip_none_values" is true.
    For my use case worked, hope it will be usefull for others!

@anikolaienko
Copy link
Owner

Thanks @ericPavone , I'll try your code. Still would be amazing if you showed a unit test or just a runnable code sample, in which current code fails. For any fix or feature I usually create a unit test.

FYI, I plan to release a new version this week with few new features and support for latest libraries.

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