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

REGR: mypyc 0.971 seems to not handle keyword-only arguments when unpickling (and shallow copying) #13227

Open
twoertwein opened this issue Jul 23, 2022 · 6 comments
Labels
bug mypy got something wrong topic-mypyc mypyc bugs

Comments

@twoertwein
Copy link

Bug Report

The following worked with 0.961: object's whose __init__ has a keyword-only argument can no longer be unpickled.

To Reproduce

test.py

from typing import Any

class Test:
    def __init__(self, *, x: Any) -> None:
        ...

save_load.py

import pickle
from test import Test

test = Test(x=1)

with open("test.pickle", mode="w+b") as file:
    pickle.dump(test, file)
with open("test.pickle", mode="r+b") as file:
    pickle.load(file)
$ mypyc test.py
$ python save_load.py 
Traceback (most recent call last):
  File "save_load.py", line 9, in <module>
    pickle.load(file)
TypeError: __init__() missing required keyword-only argument 'x'

Expected Behavior
No TypeError

Actual Behavior
TypeError

Your Environment

  • Mypy version used: 0.971
  • Python version used: 3.10.4
  • Operating system and version: Fedora Silverblue
@twoertwein twoertwein added the bug mypy got something wrong label Jul 23, 2022
@AlexWaygood AlexWaygood added the topic-mypyc mypyc bugs label Jul 23, 2022
@twoertwein
Copy link
Author

slightly smaller reproducer:

test.py:

class Test:
    def __init__(self, *, x: int) -> None:
        ...
$ mypyc test.py
$ python -c "from test import Test; from copy import copy; copy(Test(x=1))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib64/python3.10/copy.py", line 102, in copy
    return _reconstruct(x, None, *rv)
  File "/usr/lib64/python3.10/copy.py", line 265, in _reconstruct
    y = func(*args)
  File "/usr/lib64/python3.10/copyreg.py", line 101, in __newobj__
    return cls.__new__(cls, *args)
TypeError: __init__() missing required keyword-only argument 'x'

@twoertwein twoertwein changed the title REGR: mypyc 0.971 seems to not handle keyword-only arguments when unpickling REGR: mypyc 0.971 seems to not handle keyword-only arguments when unpickling (and shallow copying) Jul 23, 2022
@hauntsaninja
Copy link
Collaborator

Sounds like this might be https://github.com/python/mypy/pull/12600/files#r873178396

@hauntsaninja
Copy link
Collaborator

Looks like mypyc docs isn't up to date cc @JukkaL mypyc/mypyc#920

@hauntsaninja
Copy link
Collaborator

If you make me an RTD admin (like you did for mypy), I can get the docs working again

@twoertwein
Copy link
Author

Thank you @hauntsaninja, using this decorator leads to a new issue without pickle/copy (and without keyword-only arguments):

from mypy_extensions import mypyc_attr

@mypyc_attr(serializable=True)
class Test:
    def __init__(self, x: int) -> None:
        self.x = x

    def test(self) -> int:
        return self.x
$ mypyc test.py
$ python -c "from test import Test; Test(x=1).test()"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "test.py", line 11, in test
    return self.x
AttributeError: attribute 'Test' of 'x' undefined

@ingambe
Copy link

ingambe commented Oct 1, 2022

Is there any news regarding this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-mypyc mypyc bugs
Projects
None yet
Development

No branches or pull requests

4 participants