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 — Missing import in example at dataclasses.rst · Issue #91673 · python/cpython

Missing import in example at dataclasses.rst  #91673

New issue
Copy link
New issue
Copy link
Closed
Closed
Missing import in example at dataclasses.rst #91673
Copy link
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dir

Description

@xandrade
xandrade
opened on Apr 18, 2022
Issue body actions

Documentation

Found that the typing List is used in example, but import is missed at dataclasses.rst file.

In this example:

  @dataclass
  class D:
      x: List = []
      def add(self, element):
          self.x += element

the import from typing import List is missing

Activity

  1. xandrade
    added
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    on Apr 18, 2022
  2. ericvsmith commented on Apr 18, 2022

    @ericvsmith
    ericvsmith
    on Apr 18, 2022
    Member
    More actions

    I'm not sure how important this is. The example already says the code isn't valid. Maybe just use "list" instead of "List".

    Reacted by Jelle Zijlstra and Shantanu
  3. xandrade commented on Apr 18, 2022

    @xandrade
    xandrade
    on Apr 18, 2022
    ContributorAuthor
    More actions

    Sure @ericvsmith, but the example in the documentation doesn't run.
    Your suggestion for using list instead of list works, as long it is defined as x: list = field(default_factory=list).
    However, I think the example is about forcing the datatype in the list using List from typing.

    This works (as example only):

    from dataclasses import dataclass
    from typing import List
    
    @dataclass
    class User:
        x: list = field(default_factory=list)
        y: List[int] = field(default_factory=list)
    

    this doesn't work

    from dataclasses import dataclass
    
    @dataclass
    class User:
        x: list = []
    
  4. xandrade
    closed this as completedon Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirDocumentation in the Doc dir

    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.