Movatterモバイル変換


[0]ホーム

URL:


homepage

Issue33100

This issue trackerhas been migrated toGitHub, and is currentlyread-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title:dataclasses and __slots__ - non-default argument (member_descriptor)
Type:behaviorStage:resolved
Components:Library (Lib)Versions:Python 3.8, Python 3.7
process
Status:closedResolution:fixed
Dependencies:Superseder:
Assigned To: eric.smithNosy List: eric.smith, stachel
Priority:normalKeywords:patch

Created on2018-03-19 03:30 bystachel, last changed2022-04-11 14:58 byadmin. This issue is nowclosed.

Pull Requests
URLStatusLinkedEdit
PR 6152mergederic.smith,2018-03-20 00:49
PR 6153mergedmiss-islington,2018-03-20 01:08
Messages (8)
msg314077 -(view)Author: Adrian Stachlewski (stachel)Date: 2018-03-19 03:30
I've tried to declare two classes@dataclassclass Base:    __slots__ = ('x',)    x: Any@dataclassclass Derived(Base):    x: int    y: intAs long as I correctly understoodPEP 557 (inheritance part), changing type of variable is possible. This code produce error:TypeError: non-default argument 'y' follows default argument'x' variable in Derived class has changed default from MISSING to member_descriptor and that's the reason of the exception.
msg314087 -(view)Author: Eric V. Smith (eric.smith)*(Python committer)Date: 2018-03-19 08:57
This is the same reason that this fails:class Base:    __slots__ = ('x',)    x = 3with:ValueError: 'x' in __slots__ conflicts with class variableIn the dataclasses case, the error needs to be improved, and moved to when the base class is being defined.
msg314090 -(view)Author: Adrian Stachlewski (stachel)Date: 2018-03-19 09:14
I don't really get your point. @dataclassclass Base:    __slots__ = ('x',)    x: AnyThis case is described inPEP 557 as correct, so I don't understand why you want to generate error. Also inheritance without defining slots is correct as stated in data model. In my opinion, member_descriptor should be treated same as MISSING and everything should work correctly.
msg314091 -(view)Author: Eric V. Smith (eric.smith)*(Python committer)Date: 2018-03-19 11:32
My point is that the problem is that after:@dataclassclass Base:    __slots__ = ('x',)    x: AnyBase.x has a value (it's the member_descriptor for x). That's what's causing the problem that when adding a field to the derived class, it thinks you're adding a field without a default value after one that has a default value.I agree that I could detect this specific case and allow it. My comment about the error message was inaccurate.
msg314121 -(view)Author: Adrian Stachlewski (stachel)Date: 2018-03-19 21:52
There's also another major problem. Because Base.x has value, __init__ is not prepared correctly (member_descriptor is passed as default). @dataclassclass Base:    __slots__ = ('x',)    x: AnyBase()  # No TypeError exceptionFixing this should be quite easy, if you want I can prepare PR.
msg314122 -(view)Author: Eric V. Smith (eric.smith)*(Python committer)Date: 2018-03-19 22:30
Thanks, but I'm already looking at this in the context of a different bug.
msg314124 -(view)Author: Eric V. Smith (eric.smith)*(Python committer)Date: 2018-03-20 01:07
New changeset7389fd935c95b4b6f094312294e703ee0de18719 by Eric V. Smith in branch 'master':bpo-33100: Dataclasses now handles __slots__ and default values correctly. (GH-6152)https://github.com/python/cpython/commit/7389fd935c95b4b6f094312294e703ee0de18719
msg314128 -(view)Author: Eric V. Smith (eric.smith)*(Python committer)Date: 2018-03-20 01:31
New changeset3d41f482594b6aab12a316202b3c06757262109a by Eric V. Smith (Miss Islington (bot)) in branch '3.7':bpo-33100: Dataclasses now handles __slots__ and default values correctly. (GH-6152) (GH-6153)https://github.com/python/cpython/commit/3d41f482594b6aab12a316202b3c06757262109a
History
DateUserActionArgs
2022-04-11 14:58:58adminsetgithub: 77281
2018-03-20 01:39:18eric.smithsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-03-20 01:31:25eric.smithsetmessages: +msg314128
2018-03-20 01:08:15miss-islingtonsetpull_requests: +pull_request5911
2018-03-20 01:07:55eric.smithsetmessages: +msg314124
2018-03-20 00:49:32eric.smithsetkeywords: +patch
stage: patch review
pull_requests: +pull_request5910
2018-03-19 22:30:01eric.smithsetmessages: +msg314122
2018-03-19 21:52:16stachelsetmessages: +msg314121
2018-03-19 11:32:31eric.smithsetmessages: +msg314091
2018-03-19 09:14:50stachelsetmessages: +msg314090
2018-03-19 08:57:06eric.smithsetmessages: +msg314087
2018-03-19 03:43:05rhettingersetassignee:eric.smith

nosy: +eric.smith
2018-03-19 03:30:15stachelcreate
Supported byThe Python Software Foundation,
Powered byRoundup
Copyright © 1990-2022,Python Software Foundation
Legal Statements

[8]ページ先頭

©2009-2026 Movatter.jp