classification
Title: dataclasses: make field() with no annotation an error
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: eric.smith, gvanrossum, levkivskyi, miss-islington
Priority: normal Keywords: patch

Created on 2018-01-06 22:21 by eric.smith, last changed 2018-03-22 21:00 by eric.smith. 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
2018-03-22 21:00:25eric.smithsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-03-22 20:59:08miss-islingtonsetnosy: + miss-islington
messages: + msg314293
2018-03-22 20:29:16miss-islingtonsetpull_requests: + pull_request5940
2018-03-22 20:28:51eric.smithsetmessages: + msg314290
2018-03-22 20:08:06eric.smithsetkeywords: + patch
stage: patch review
pull_requests: + pull_request5939
2018-03-22 14:48:04eric.smithsetmessages: + msg314263
components: + Library (Lib)
versions: + Python 3.8
2018-03-22 14:14:26levkivskyisetmessages: + msg314259
2018-03-22 01:47:32eric.smithsetmessages: + msg314231
2018-03-21 23:43:42levkivskyisetmessages: + msg314228
2018-03-21 22:22:43gvanrossumsetmessages: + msg314227
2018-03-21 21:53:39eric.smithsetmessages: + msg314226
2018-01-06 22:21:25eric.smithsetversions: + Python 3.7
2018-01-06 22:21:04eric.smithcreate