On Fri, Jun 23, 2023 at 12:18 PM Eric V. Smith <e...@trueblade.com> wrote:
> > > On Jun 23, 2023, at 9:34 AM, Joao S. O. Bueno <gwid...@gmail.com> wrote: > > > > > On Fri, Jun 23, 2023 at 2:35 AM Jelle Zijlstra <jelle.zijls...@gmail.com> > wrote: > >> >> >> El jue, 22 jun 2023 a las 8:22, Randolf Scholz (<randolf.sch...@gmail.com>) >> escribió: >> >>> Dataclasses should provide a way to ignore a type hinted attributes, and >>> not consider them as fields. >>> >>> For example, some attributes might be derived during `__post_init__` >>> from the values of the fields or other variables. >>> >>> If one wants to still type hint these attributes, one has to awkward >>> workarounds to avoid having dataclass interpret them as fields. ( >>> https://stackoverflow.com/questions/76532816) >>> >> > But it’s not clear (to me) why not being a field is desirable. Why is it > important? > Can't know - not my design, it was Randolf's question. They might represent an internal state that should not be relayed on serialization or conversion, for example. I can imagine some scenarios where I'd want some instance attributes to be shorter lived and non-transient, although, I'd more likely build the class "manually" instead of a dataclass - or, more likely, put the dataclass under a wrapper layer that would handle the "perishable" states. > Eric > > >>> I propose `NON_FIELDS` sentinel, analogous to `KW_ONLY`. (alternative >>> name suggestions welcome). when writing a dataclass, all attributes after >>> this sentinel are ignored and not considered fields. >>> >>> ``` >>> @dataclass >>> class Foo: >>> field0: int >>> field1: int >>> >>> _: KW_ONLY >>> >>> fieldN: int >>> >>> _: NON_FIELDS >>> >>> attr0: int # @dataclass will ignore this type hint. >>> >> >> How is this different from `attr0: int = field(init=False)`? >> > > attr0 would be listed as a `field` in the introspectable attributes of the > dataclass in this way. > That is why I did not suggest that in my initial answer to Randolf on > stackoverflow: > > https://stackoverflow.com/questions/76532816/type-hint-extra-attributes-not-fields/76533091#76533091 > > I like the dataclasses.attribute idea, though - (but it will also require > static type checking tools to review their > dataclass special casing - it looks like there is no escape from that). > > > >> >> >>> ``` >>> >>> Additionally one could consider adding an `attribute` typing construct, >>> such that `attr0: attribute[int]` would mark it as a non-field attribute. >>> _______________________________________________ >>> Python-ideas mailing list -- python-ideas@python.org >>> To unsubscribe send an email to python-ideas-le...@python.org >>> https://mail.python.org/mailman3/lists/python-ideas.python.org/ >>> Message archived at >>> https://mail.python.org/archives/list/python-ideas@python.org/message/P67URFV2JJRFD6J5TXD44EEBO4IRTEYF/ >>> Code of Conduct: http://python.org/psf/codeofconduct/ >>> >> _______________________________________________ >> Python-ideas mailing list -- python-ideas@python.org >> To unsubscribe send an email to python-ideas-le...@python.org >> https://mail.python.org/mailman3/lists/python-ideas.python.org/ >> Message archived at >> https://mail.python.org/archives/list/python-ideas@python.org/message/HS5E5XNHKLO47Q6UPF5QVUCIK2FR6VSF/ >> Code of Conduct: http://python.org/psf/codeofconduct/ >> > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/C5QJQT5YV7UOKFF57PWD4VSF4RWUDOSF/ > Code of Conduct: http://python.org/psf/codeofconduct/ > >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/B3YHKCWMD2LN6VYNMGY4WHZWZFKGP3TC/ Code of Conduct: http://python.org/psf/codeofconduct/