Re: [Python-Dev] Dataclasses, frozen and __post_init__

2018-02-16 Thread Guido van Rossum
On Fri, Feb 16, 2018 at 3:38 PM, Ben Lewis  wrote:

> I have been using dataclasses package in a pet project of mine. I'm sorry
> if this issue has already been raised. I came across a situation where I
> wanted to use the __post_init__ function to initialise some inherited
> fields from a dataclass with frozen=True. The problem is that because it is
> frozen, assigning to the field doesn't work.
>
> There are two workarounds without changing the base class to frozen=False,
> which could be in a library.
>
> 1. Use object.__setattr__, this is ugly and not very user or beginner
> friendly.
> 2. Extract __post_init__ out into a factory function. Then it also loses
> all the advantages of the __post_init__ and InitVar mechanism.
>
> Both frozen and unfrozen dataclasses should be able to use the same
> initialisation mechanism for consistency. Being consistent would ease of
> converting an unfrozen dataclass to a frozen one if the only code that
> actually modifies the instance is in __post_init__ function.
>
> I think frozen classes should be able to be mutated during the
> __post_init__ call. To implements this a frozen dataclass could have a flag
> to says it's not yet fully initialised and the flag would be checked in the
> frozen setattr/delattr methods. This flag could be located as a special
> attribute on the instance or be in a weak reference dict.
>

That's a pretty tricky proposal, and one that's been debated on and off for
a long time in other contexts. And that flag would somehow have to be part
of every instance's state.

In general the right way to initialize an immutable/frozen object is not
through __init__ but through __new__ -- have you tried that?

Also, a small example that demonstrates your need would do wonders to help
us understand your use case better.

-- 
--Guido van Rossum (python.org/~guido)
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] Dataclasses, frozen and __post_init__

2018-02-16 Thread Ben Lewis
Hello

I have been using dataclasses package in a pet project of mine. I'm sorry
if this issue has already been raised. I came across a situation where I
wanted to use the __post_init__ function to initialise some inherited
fields from a dataclass with frozen=True. The problem is that because it is
frozen, assigning to the field doesn't work.

There are two workarounds without changing the base class to frozen=False,
which could be in a library.

1. Use object.__setattr__, this is ugly and not very user or beginner
friendly.
2. Extract __post_init__ out into a factory function. Then it also loses
all the advantages of the __post_init__ and InitVar mechanism.

Both frozen and unfrozen dataclasses should be able to use the same
initialisation mechanism for consistency. Being consistent would ease of
converting an unfrozen dataclass to a frozen one if the only code that
actually modifies the instance is in __post_init__ function.

I think frozen classes should be able to be mutated during the
__post_init__ call. To implements this a frozen dataclass could have a flag
to says it's not yet fully initialised and the flag would be checked in the
frozen setattr/delattr methods. This flag could be located as a special
attribute on the instance or be in a weak reference dict.

Thanks
Ben Lewis
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] Summary of Python tracker Issues

2018-02-16 Thread Python tracker

ACTIVITY SUMMARY (2018-02-09 - 2018-02-16)
Python tracker at https://bugs.python.org/

To view or respond to any of the issues listed below, click on the issue.
Do NOT respond to this message.

Issues counts and deltas:
  open6456 (+17)
  closed 38153 (+32)
  total  44609 (+49)

Open issues with patches: 2511 


Issues opened (35)
==

#32810: Expose ags_gen and agt_gen in asynchronous generators
https://bugs.python.org/issue32810  opened by dabeaz

#32813: SSL shared_ciphers implementation wrong - returns configured b
https://bugs.python.org/issue32813  opened by noxxi

#32814: smtplib.send_message mishandles 8BITMIME RFC 6152
https://bugs.python.org/issue32814  opened by Segev Finer

#32818: multiprocessing segmentfault under Windows compatibility mode
https://bugs.python.org/issue32818  opened by Ma Lin

#32819: match_hostname() error reporting bug
https://bugs.python.org/issue32819  opened by christian.heimes

#32820: Add bits method to ipaddress
https://bugs.python.org/issue32820  opened by ewosborne

#32821: Add snippet on how to configure a "split" stream for console
https://bugs.python.org/issue32821  opened by mariocj89

#32822: finally block doesn't re-raise exception if return statement e
https://bugs.python.org/issue32822  opened by David Rebbe

#32823: Regression in test -j behavior and time in 3.7.0b1
https://bugs.python.org/issue32823  opened by terry.reedy

#32824: Docs: Using Python on a Macintosh has bad info per Apple site
https://bugs.python.org/issue32824  opened by griswolf

#32825: warn user of creation of multiple Tk instances
https://bugs.python.org/issue32825  opened by mps

#32829: Lib/ be more pythonic
https://bugs.python.org/issue32829  opened by dilyan.palauzov

#32831: IDLE: Add docstrings and tests for codecontext
https://bugs.python.org/issue32831  opened by csabella

#32832: doctest should support custom ps1/ps2 prompts
https://bugs.python.org/issue32832  opened by Sergey.Kirpichev

#32833: argparse doesn't recognise two option aliases as equal
https://bugs.python.org/issue32833  opened by Krzysztof Leszczyński

#32834: test_gdb fails with Posix locale in 3.7
https://bugs.python.org/issue32834  opened by serhiy.storchaka

#32835: Add documention mentioning that Cygwin isn't fully compatible
https://bugs.python.org/issue32835  opened by jayyin11043

#32836: Symbol table for comprehensions (list, dict, set) still includ
https://bugs.python.org/issue32836  opened by mjpieters

#32838: Fix Python versions in the table of magic numbers
https://bugs.python.org/issue32838  opened by serhiy.storchaka

#32839: Add after_info as a function to tkinter
https://bugs.python.org/issue32839  opened by csabella

#32840: Must install python 3.6.3 when 3.6.4 already installed
https://bugs.python.org/issue32840  opened by NaCl

#32841: Asyncio.Condition prevents cancellation
https://bugs.python.org/issue32841  opened by bar.harel

#32843: More revisions to test.support docs
https://bugs.python.org/issue32843  opened by csabella

#32844: subprocess may incorrectly redirect a low fd to stderr if anot
https://bugs.python.org/issue32844  opened by izbyshev

#32846: Deletion of large sets of strings is extra slow
https://bugs.python.org/issue32846  opened by terry.reedy

#32847: Add DirectoryNotEmptyError subclass of OSError
https://bugs.python.org/issue32847  opened by barry

#32849: Fatal Python error: Py_Initialize: can't initialize sys standa
https://bugs.python.org/issue32849  opened by rudolphf

#32850: Run gc_collect() before complaining about dangling threads
https://bugs.python.org/issue32850  opened by smurfix

#32852: trace changes sys.argv from list to tuple
https://bugs.python.org/issue32852  opened by altendky

#32853: struct's docstring implies alignment is always performed
https://bugs.python.org/issue32853  opened by Eli_B

#32854: Add ** Map Unpacking Support for namedtuple
https://bugs.python.org/issue32854  opened by John Crawford

#32855: Add documention stating supported Platforms
https://bugs.python.org/issue32855  opened by jayyin11043

#32856: Optimize the `for y in [x]` idiom in comprehensions
https://bugs.python.org/issue32856  opened by serhiy.storchaka

#32857: tkinter after_cancel does not behave correctly when called wit
https://bugs.python.org/issue32857  opened by csabella

#32858: Improve OpenSSL ECDH support
https://bugs.python.org/issue32858  opened by sruester



Most recent 15 issues with no replies (15)
==

#32855: Add documention stating supported Platforms
https://bugs.python.org/issue32855

#32853: struct's docstring implies alignment is always performed
https://bugs.python.org/issue32853

#32852: trace changes sys.argv from list to tuple
https://bugs.python.org/issue32852

#32849: Fatal Python error: Py_Initialize: can't initialize sys standa
https://bugs.python.org/issue32849

#32847: Add DirectoryNotEmptyError subclass of OSError
https://bugs.python.org/issue32847

#32843: More revisions to