Skip to content

Missing import in example at dataclasses.rst  #91673

Description

@xandrade

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. ericvsmith commented on Apr 18, 2022

    @ericvsmith
    Member

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

  2. xandrade commented on Apr 18, 2022

    @xandrade
    ContributorAuthor

    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 = []
    
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 dir

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions