Title: pickle documentation says that unpickling may not call __new__
Type: behavior Stage: needs patch
Components: Documentation Versions: Python 3.7, Python 3.6, Python 3.5
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: alexandre.vassalotti, docs@python, july, serhiy.storchaka
Priority: normal Keywords:

Created on 2016-07-27 17:07 by july, last changed 2017-02-18 20:47 by serhiy.storchaka.

Messages (1)
msg271465 - (view) Author: July Tikhonov (july) * Date: 2016-07-27 17:07
A note just below object.__setstate__() documentation
says that
… the type should implement __getnewargs__() or __getnewargs_ex__() to establish such an invariant; otherwise, neither __new__() nor __init__() will be called.

I believe that note about not calling __new__() was relevant in python2. I could not find case in python3 in which __new__() would not be called. And __init__() is not called anyway, as far as I understand (unless explicitly by __setstate__() or something).

Python 3.6.0a3+ (default:da9898e7e90d, Jul 27 2016, 19:51:12) 
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class C:
...   def __getstate__(self): return {'foo' :}
...   def __setstate__(self, state): = state['foo']
...   def __new__(cls):
...     print('__new__ is called'); return super().__new__(cls)
...   def __init__(self):
...     print('__init__ is called'); = None; super().__init__()
>>> c = C(); = 'bar'
__new__ is called
__init__ is called
>>> import pickle
>>> c2 = pickle.loads(pickle.dumps(c))
__new__ is called
Date User Action Args
2017-02-18 20:47:59serhiy.storchakasetnosy: + alexandre.vassalotti, serhiy.storchaka
stage: needs patch
type: behavior

versions: + Python 3.7, - Python 3.4
2017-02-18 20:47:07serhiy.storchakalinkissue29597 superseder
2016-07-27 17:07:25julycreate