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

datatype with __slots__ breaks copy.deepcopy #14771

Closed
2 tasks done
zechigan opened this issue Feb 29, 2024 · 2 comments · Fixed by #14778
Closed
2 tasks done

datatype with __slots__ breaks copy.deepcopy #14771

zechigan opened this issue Feb 29, 2024 · 2 comments · Fixed by #14778
Assignees
Labels
accepted Ready for implementation bug Something isn't working P-medium Priority: medium python Related to Python Polars

Comments

@zechigan
Copy link

zechigan commented Feb 29, 2024

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Issue description

The issue is twofold:

  1. perf(python): Add __slots__ to most Polars classes #13236 added the __slots__ attributes to all the datatypes, which forced the removal of __dict__. However, on https://github.com/Object905/polars/blob/815732bc0f5bc06e07f3a995c164fa87e3c53ce8/py-polars/polars/datatypes/classes.py#L94 __dict__ is still being accessed
  2. After fixing the first issue by replacing self.__dict__ with self.__slots__, one passes a datatype into copy.deepcopy (or specifically for my case, as a field on a dataclass, which gets passed into dataclasses.asdict) will quickly find that the function relies on the data structure having the __dict__ attribute and as a result throws: https://github.com/python/cpython/blob/6359141867595f84f9e803e1053ae51db3203c49/Lib/copy.py#L268

Reproducible example

>>> import polars as pl
>>> from copy import deepcopy
>>> deepcopy(pl.Int64())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/zechigan/anaconda3/lib/python3.11/copy.py", line 161, in deepcopy
    rv = reductor(4)
         ^^^^^^^^^^^
  File "/Users/zechigan/anaconda3/lib/python3.11/site-packages/polars/datatypes/classes.py", line 96, in __reduce__
    return (_custom_reconstruct, (type(self), object, None), self.__dict__)
                                                             ^^^^^^^^^^^^^
AttributeError: 'Int64' object has no attribute '__dict__'. Did you mean: '__dir__'?
  1. (After addressing 1 by replacing self.__dict__ with self.__slots__)
>>> from dataclasses import dataclass, asdict
>>> from typing import Any
>>> import polars as pl
>>> @dataclass
... class DummyDataClass:
...     schema: Any
... 
>>> dc = DummyDataClass(pl.DataFrame({'a':[1]}).schema)
>>> asdict(dc)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/zechigan/anaconda3/lib/python3.11/dataclasses.py", line 1277, in asdict
    return _asdict_inner(obj, dict_factory)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/zechigan/anaconda3/lib/python3.11/dataclasses.py", line 1284, in _asdict_inner
    value = _asdict_inner(getattr(obj, f.name), dict_factory)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/zechigan/anaconda3/lib/python3.11/dataclasses.py", line 1314, in _asdict_inner
    return type(obj)((_asdict_inner(k, dict_factory),
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/zechigan/anaconda3/lib/python3.11/dataclasses.py", line 1315, in <genexpr>
    _asdict_inner(v, dict_factory))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/zechigan/anaconda3/lib/python3.11/dataclasses.py", line 1318, in _asdict_inner
    return copy.deepcopy(obj)
           ^^^^^^^^^^^^^^^^^^
  File "/Users/zechigan/anaconda3/lib/python3.11/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/zechigan/anaconda3/lib/python3.11/copy.py", line 280, in _reconstruct
    y.__dict__.update(state)
    ^^^^^^^^^^
AttributeError: 'Int64' object has no attribute '__dict__'. Did you mean: '__dir__'?

Expected behavior

polars datatypes can be safely passed into copy.deepcopy

Installed versions

--------Version info---------
Polars:               0.20.13
Index type:           UInt32
Platform:             macOS-10.16-x86_64-i386-64bit
Python:               3.11.3 (main, Apr 19 2023, 18:51:09) [Clang 14.0.6 ]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          2.2.1
connectorx:           <not installed>
deltalake:            <not installed>
fsspec:               2023.3.0
gevent:               <not installed>
hvplot:               0.8.4
matplotlib:           3.7.1
numpy:                1.24.3
openpyxl:             3.0.10
pandas:               1.5.3
pyarrow:              11.0.0
pydantic:             <not installed>
pyiceberg:            <not installed>
pyxlsb:               <not installed>
sqlalchemy:           1.4.39
xlsx2csv:             <not installed>
xlsxwriter:           <not installed>
@zechigan zechigan added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Feb 29, 2024
@stinodego stinodego added P-medium Priority: medium and removed needs triage Awaiting prioritization by a maintainer labels Feb 29, 2024
@stinodego
Copy link
Member

stinodego commented Feb 29, 2024

Thanks for opening an issue.

Indeed the __reduce__ method is no longer correct.

However, if I simply remove that method, deepcopy seems to work just fine. Could you elaborate on your point 2? I cannot reproduce your example if __reduce__ is deleted.

@zechigan
Copy link
Author

Sorry, updated my example. @stinodego you are right, removing __reduce__ did solve both of my problems!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation bug Something isn't working P-medium Priority: medium python Related to Python Polars
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants