GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add a way to hide fields in dataclasses · Issue #89609 · python/cpython

Add a way to hide fields in dataclasses #89609

New issue
Copy link
New issue
Copy link
Open
Open
Add a way to hide fields in dataclasses#89609
Copy link
Assignees
ericvsmith
Labels
3.11only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

formigacomcaimbra
mannequin
opened on Oct 12, 2021
Issue body actions
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. formigacomcaimbra
      added
      3.11only security fixesonly security fixes
      type-featureA feature request or enhancementA feature request or enhancement
      on Oct 12, 2021
    2. ericvsmith commented on Oct 12, 2021

      @ericvsmith
      ericvsmith
      on Oct 12, 2021
      Member
      More actions

      Please explain what "hiding" does.

    3. formigacomcaimbra commented on Oct 12, 2021

      @formigacomcaimbra
      formigacomcaimbramannequin
      on Oct 12, 2021
      MannequinAuthor
      More actions

      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.

      Reacted by cschroeer
    4. ericvsmith commented on Oct 13, 2021

      @ericvsmith
      ericvsmith
      on Oct 13, 2021
      Member
      More actions

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

    5. ericvsmith
      self-assigned this
      on Oct 13, 2021
    6. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    7. iritkatriel
      added
      stdlibStandard Library Python modules in the Lib/ directoryStandard 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

    • @ericvsmith
      ericvsmith

    Labels

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.