[dataclasses] add a NON_FIELDS sentinel after which all attributes are ignored.
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) 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. ``` Additionally one could consider adding an `attribute` typing construct, such that `attr0: attribute[int]` would mark it as a non-field attribute.
Hmm, It does make you wonder if using the presence of an annotation to indicate a field was a good choice -- but here we are. Additionally one could consider adding an `attribute` typing construct,
such that `attr0: attribute[int]` would mark it as a non-field attribute.
Better yet, a dataclasses.attribute object that would indicate that: from dataclasses import dataclass, field, attribute @dataclass class C: x: int = 0 mylist: list[int] = field(default_factory=list) y: int = attribute(value=0) x and mylist are fields, y is not. y would get set to the value specified. Then the type annotation is left unadulterated -- much easier for the type checkers. -CHB -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
El jue, 22 jun 2023 a las 8:22, Randolf Scholz (<randolf.scholz@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)
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)`?
```
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-leave@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/P67URF... Code of Conduct: http://python.org/psf/codeofconduct/
On Fri, Jun 23, 2023 at 2:35 AM Jelle Zijlstra <jelle.zijlstra@gmail.com> wrote:
El jue, 22 jun 2023 a las 8:22, Randolf Scholz (<randolf.scholz@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)
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-... 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-leave@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/P67URF... Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/HS5E5X... Code of Conduct: http://python.org/psf/codeofconduct/
A possible suggestion for your problem: On strawberry-graphql we have a solution for that, by annotating the field with "strawberry.Private"[1], like this: @strawberry.type class Foo: str_attr: str str_private_attr: Private[str] That Private is defined as: Private = Annotated[T, StrawberryPrivate()] Meaning we could also do "str_private_attr: Annotated[str, StrawberryPrivate()]" When introspecting the dataclass fields to generate the graphql schema, we will check if the annotation is annotated, and if it is and contains a "StrawberryPrivate()" instance in its __args__, we will exclude that field from the graphql API. [1] https://github.com/strawberry-graphql/strawberry/blob/main/strawberry/privat... On Fri, Jun 23, 2023 at 10:32 AM Joao S. O. Bueno <gwidion@gmail.com> wrote:
On Fri, Jun 23, 2023 at 2:35 AM Jelle Zijlstra <jelle.zijlstra@gmail.com> wrote:
El jue, 22 jun 2023 a las 8:22, Randolf Scholz (<randolf.scholz@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)
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-...
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-leave@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/P67URF... Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/HS5E5X... Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/C5QJQT... Code of Conduct: http://python.org/psf/codeofconduct/
-- Thiago Bellini Ribeiro | https://bellini.dev
participants (5)
-
Christopher Barker -
Jelle Zijlstra -
Joao S. O. Bueno -
Randolf Scholz -
Thiago Bellini Ribeiro