[issue45446] Add a way to hide fields in dataclasses

2021-10-13 Thread Eric V. Smith


Eric V. Smith  added the comment:

"hide" is not a good name for this.

Does attrs have anything equivalent? This seems like a pretty niche usage, so 
I'm not inclined to include it without evidence of a wide-spread need for it.

--
assignee:  -> eric.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45446] Add a way to hide fields in dataclasses

2021-10-12 Thread Nium


Nium  added the comment:

The objective of this feature is to add a way to omit fields of a dataclass 
when `asdict` or `astuple` is used.

The main propuse is to add the `hide` parameter in `field` and add it as an 
attribute in the `Field` class.
```py
@dataclass
class User:
name: str
ws: WebSocket = field(hide=True)


user = User("NiumXp", None)
assert asdict(user) == {"name": "NiumXp"}
```
`asdict` will ignore all fields that have the `hide` attribute set as `True`.

---

Actually a possible solution to do it is doing this:
```py
from dataclasses import *

_asdict = asdict


class WebSocket:
pass


def asdict(obj):
def factory(it):
fields = []

for raw_field in it:
name, _ = raw_field

field = obj.__dataclass_fields__[name]
if not field.metadata.get("hide"):
fields.append(raw_field)

return dict(fields)
return _asdict(obj, dict_factory=factory)


@dataclass
class User:
name: str
ws: WebSocket = field(metadata={"hide": True})


user = User("NiumXp", None)
assert asdict(user) == {"name": "NiumXp"}
```
We need to make the same in `astuple` and to avoid writing 
`field(metadata={"hide": True)` multiple times we can use

```py
from functools import partial
from dataclasses import field as _field

field = _field(metadata={"hide": True})
```

But, this means that we can't reuse the `metadata` parameter.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45446] Add a way to hide fields in dataclasses

2021-10-12 Thread Eric V. Smith


New submission from Eric V. Smith :

Please explain what "hiding" does.

--
nosy: +eric.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45446] Add a way to hide fields in dataclasses

2021-10-12 Thread Roundup Robot


Change by Roundup Robot :


--
keywords: +patch
nosy: +python-dev
nosy_count: 1.0 -> 2.0
pull_requests: +27195
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/28904

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45446] Add a way to hide fields in dataclasses

2021-10-12 Thread Nium


Change by Nium :


--
nosy: formigacomcaimbra
priority: normal
severity: normal
status: open
title: Add a way to hide fields in dataclasses
type: enhancement
versions: Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com