Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing import in example at dataclasses.rst #91673

Closed
xandrade opened this issue Apr 18, 2022 · 2 comments
Closed

Missing import in example at dataclasses.rst #91673

xandrade opened this issue Apr 18, 2022 · 2 comments
Labels
docs Documentation in the Doc dir

Comments

@xandrade
Copy link
Contributor

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

@ericvsmith
Copy link
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".

@xandrade
Copy link
Contributor Author

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
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

2 participants