Issue 32505: dataclasses: make field() with no annotation an error (original) (raw)

Created on 2018-01-06 22:21 by eric.smith, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6192 merged eric.smith,2018-03-22 20:08
PR 6193 merged miss-islington,2018-03-22 20:29
Messages (9)
msg309586 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-01-06 22:21
This is an attractive nuisance, especially when coming from attrs. Make it an error, since it's using field() with no annotation: @dataclass class C: x = field()
msg314226 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-03-21 21:53
I'm not sure this is worth preventing. I can certainly make it an error, but since dataclasses ignores anything without a type annotation, I don't think it's a big deal. Basically I'd have to look through all class attributes that are of type Field but aren't in the class's __annotations__. I cannot find the original conversation that caused me to open this issue. If anyone feels strongly that this case should be an error, let me know.
msg314227 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2018-03-21 22:22
Fine to close as wontfix.
msg314228 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2018-03-21 23:43
Some part of the discussion is in https://bugs.python.org/issue32428 I still think it is important to flag things like this as an error: @dataclass class C: x = field() This is a big bug magnet. Especially taking into account that attrs behave in the opposite way in this case.
msg314231 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-03-22 01:47
But surely when you instantiate this: c = C(1) you'll notice that's an error?
msg314259 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2018-03-22 14:14
> you'll notice that's an error? Yes, but there are other scenarios, like using `init=False` or updating existing class definition and forgetting to update call sites (which will still work), etc. What would we lose by not flagging this as an error? I think there are no legitimate use cases for such code, and implementation is straightforward. If you don't have time I can do this myself.
msg314263 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-03-22 14:48
Thanks for the offer. I've already got the code written, I just need to write some tests. I'll get it done real soon now.
msg314290 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-03-22 20:28
New changeset 56970b8ce9d23269d20a76f13c80e670c856ba7f by Eric V. Smith in branch 'master': bpo-32505: dataclasses: raise TypeError if a member variable is of type Field, but doesn't have a type annotation. (GH-6192) https://github.com/python/cpython/commit/56970b8ce9d23269d20a76f13c80e670c856ba7f
msg314293 - (view) Author: miss-islington (miss-islington) Date: 2018-03-22 20:59
New changeset 3b4c6b16c597aa2356f5658dd67da7dcd4434038 by Miss Islington (bot) in branch '3.7': bpo-32505: dataclasses: raise TypeError if a member variable is of type Field, but doesn't have a type annotation. (GH-6192) https://github.com/python/cpython/commit/3b4c6b16c597aa2356f5658dd67da7dcd4434038
History
Date User Action Args
2022-04-11 14:58:56 admin set github: 76686
2018-03-22 21:00:25 eric.smith set status: open -> closedresolution: fixedstage: patch review -> resolved
2018-03-22 20:59:08 miss-islington set nosy: + miss-islingtonmessages: +
2018-03-22 20:29:16 miss-islington set pull_requests: + <pull%5Frequest5940>
2018-03-22 20:28:51 eric.smith set messages: +
2018-03-22 20:08:06 eric.smith set keywords: + patchstage: patch reviewpull_requests: + <pull%5Frequest5939>
2018-03-22 14:48:04 eric.smith set messages: + components: + Library (Lib)versions: + Python 3.8
2018-03-22 14:14:26 levkivskyi set messages: +
2018-03-22 01:47:32 eric.smith set messages: +
2018-03-21 23:43:42 levkivskyi set messages: +
2018-03-21 22:22:43 gvanrossum set messages: +
2018-03-21 21:53:39 eric.smith set messages: +
2018-01-06 22:21:25 eric.smith set versions: + Python 3.7
2018-01-06 22:21:04 eric.smith create