Skip to content

Add a way to hide fields in dataclasses #89609

Open

Description

@formigacomcaimbra
BPO 45446
Nosy @ericvsmith
PRs
  • bpo-45446: add a way to hide dataclass fields #28904
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ericvsmith'
    closed_at = None
    created_at = <Date 2021-10-12.15:23:21.079>
    labels = ['type-feature', '3.11']
    title = 'Add a way to hide fields in dataclasses'
    updated_at = <Date 2021-10-13.16:12:44.238>
    user = 'https://bugs.python.org/formigacomcaimbra'

    bugs.python.org fields:

    activity = <Date 2021-10-13.16:12:44.238>
    actor = 'eric.smith'
    assignee = 'eric.smith'
    closed = False
    closed_date = None
    closer = None
    components = []
    creation = <Date 2021-10-12.15:23:21.079>
    creator = 'formigacomcaimbra'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45446
    keywords = ['patch']
    message_count = 3.0
    messages = ['403749', '403756', '403845']
    nosy_count = 3.0
    nosy_names = ['eric.smith', 'python-dev', 'formigacomcaimbra']
    pr_nums = ['28904']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue45446'
    versions = ['Python 3.11']

    Activity

    1. ericvsmith commented on Oct 12, 2021

      @ericvsmith
      Member

      Please explain what "hiding" does.

    2. formigacomcaimbra commented on Oct 12, 2021

      formigacomcaimbramannequin
      MannequinAuthor

      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.

      @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:

      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

      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.

    3. ericvsmith commented on Oct 13, 2021

      @ericvsmith
      Member

      "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.

    4. self-assigned this
      on Oct 13, 2021
    5. transferred this issue fromon Apr 10, 2022
    6. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Nov 27, 2023
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.11only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions