-
-
Notifications
You must be signed in to change notification settings - Fork 31k
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
Conversation
In this example: ``` @DataClass class D: x: List = [] def add(self, element): self.x += element ``` the import ```from typing import List``` is missing
Every change to Python requires a NEWS entry. Please, add it using the blurb_it Web app or the blurb command-line tool. |
Is there a corresponding issue? |
Ah, it's #91673. |
Every change to Python requires a NEWS entry. Please, add it using the blurb_it Web app or the blurb command-line tool. |
@ericvsmith also noted there is a trailing whitespace and the Azure Pipelines PR is failing (library/dataclasses.rst:704). |
Doc/library/dataclasses.rst
Outdated
@@ -700,6 +700,8 @@ variable ``x``, as expected. | |||
|
|||
Using dataclasses, *if* this code was valid:: | |||
|
|||
from typing import List | |||
|
|||
@dataclass | |||
class D: | |||
x: List = [] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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```
Every change to Python requires a NEWS entry. Please, add it using the blurb_it Web app or the blurb command-line tool. |
As reported in #91673, the import is missed for the typing
List
at dataclasses.rst file.In this example:
the import
from typing import List
is missing