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

gh-91673: Missing import in dataclasses documentation #91672

Closed
wants to merge 3 commits into from
Closed

gh-91673: Missing import in dataclasses documentation #91672

wants to merge 3 commits into from

Conversation

xandrade
Copy link
Contributor

@xandrade xandrade commented Apr 18, 2022

As reported in #91673, the import is missed for the typing List 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

In this example:

```
  @DataClass
  class D:
      x: List = []
      def add(self, element):
          self.x += element
```
the import ```from typing import List``` is missing
@xandrade xandrade requested a review from ericvsmith as a code owner April 18, 2022 19:58
@cpython-cla-bot
Copy link

cpython-cla-bot bot commented Apr 18, 2022

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-bot
Copy link

Every change to Python requires a NEWS entry.

Please, add it using the blurb_it Web app or the blurb command-line tool.

@bedevere-bot bedevere-bot added docs Documentation in the Doc dir awaiting review labels Apr 18, 2022
@xandrade xandrade marked this pull request as draft April 18, 2022 20:04
@xandrade xandrade changed the title [3.10] Missing import in dataclasses documentation gh-91672: Missing import in dataclasses documentation Apr 18, 2022
@xandrade xandrade marked this pull request as ready for review April 18, 2022 20:15
@ericvsmith
Copy link
Member

Is there a corresponding issue?

@ericvsmith
Copy link
Member

Ah, it's #91673.

@xandrade xandrade marked this pull request as draft April 18, 2022 20:19
@xandrade xandrade changed the title gh-91672: Missing import in dataclasses documentation gh-91673: Missing import in dataclasses documentation Apr 18, 2022
@xandrade xandrade marked this pull request as ready for review April 18, 2022 20:20
@bedevere-bot
Copy link

Every change to Python requires a NEWS entry.

Please, add it using the blurb_it Web app or the blurb command-line tool.

@xandrade
Copy link
Contributor Author

@ericvsmith also noted there is a trailing whitespace and the Azure Pipelines PR is failing (library/dataclasses.rst:704).
Please accept my apologies for the few fixes in this PR, first one for CPython 😨

@@ -700,6 +700,8 @@ variable ``x``, as expected.

Using dataclasses, *if* this code was valid::

from typing import List

@dataclass
class D:
x: List = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @ericvsmith said on the issue, it's easier to just put x: list = [] here.

Copy link
Contributor Author

@xandrade xandrade Apr 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JelleZijlstra I think they really mean List, from typing. But I can be wrong. With x: list = [], you get the following error:

ValueError: mutable default <class 'list'> for field x is not allowed: use default_factory

In that case, it should be (I think):

x: list = field(default_factory=list)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using list is fine in recent versions; see PEP-585.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, but x: list = [] doesn't work either (tried in 3.7, 3.8 and 3.9). Maybe in 3.10?

from dataclasses import dataclass
  
@dataclass
class D:
    x: list = []

throws the following exception:

ValueError                                Traceback (most recent call last)
Input In [2], in <cell line: 4>()
      1 from dataclasses import dataclass, field
      2 from typing import List
      4 @dataclass
----> 5 class D:
      6     x: list = []

File /opt/conda/lib/python3.9/dataclasses.py:1021, in dataclass(cls, init, repr, eq, order, unsafe_hash, frozen)
   1018     return wrap
   1020 # We're called as @dataclass without parens.
-> 1021 return wrap(cls)

File /opt/conda/lib/python3.9/dataclasses.py:1013, in dataclass.<locals>.wrap(cls)
   1012 def wrap(cls):
-> 1013     return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)

File /opt/conda/lib/python3.9/dataclasses.py:863, in _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
    858 cls_annotations = cls.__dict__.get('__annotations__', {})
    860 # Now find fields in our class.  While doing so, validate some
    861 # things, and set the default values (as class attributes) where
    862 # we can.
--> 863 cls_fields = [_get_field(cls, name, type)
    864               for name, type in cls_annotations.items()]
    865 for f in cls_fields:
    866     fields[f.name] = f

File /opt/conda/lib/python3.9/dataclasses.py:863, in <listcomp>(.0)
    858 cls_annotations = cls.__dict__.get('__annotations__', {})
    860 # Now find fields in our class.  While doing so, validate some
    861 # things, and set the default values (as class attributes) where
    862 # we can.
--> 863 cls_fields = [_get_field(cls, name, type)
    864               for name, type in cls_annotations.items()]
    865 for f in cls_fields:
    866     fields[f.name] = f

File /opt/conda/lib/python3.9/dataclasses.py:747, in _get_field(cls, a_name, a_type)
    739     # Should I check for other field settings? default_factory
    740     # seems the most serious to check for.  Maybe add others.  For
    741     # example, how about init=False (or really,
   (...)
    744 
    745 # For real fields, disallow mutable defaults for known types.
    746 if f._field_type is _FIELD and isinstance(f.default, (list, dict, set)):
--> 747     raise ValueError(f'mutable default {type(f.default)} for field '
    748                      f'{f.name} is not allowed: use default_factory')
    750 return f

ValueError: mutable default <class 'list'> for field x is not allowed: use default_factory

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of the example is that it doesn't work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @JelleZijlstra. Since the example isn't supposed to work anyway, I think changing List to list is good enough. No other changes are needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks guys for your time

Updated ```x: list = []``` by ```x: list = field(default_factory=list)``` on the example to avoid error ```ValueError: mutable default <class 'list'> for field x is not allowed: use default_factory```
@bedevere-bot
Copy link

Every change to Python requires a NEWS entry.

Please, add it using the blurb_it Web app or the blurb command-line tool.

@xandrade xandrade closed this Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants