-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Use attrs library for data classes #2690
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2690 +/- ##
==========================================
+ Coverage 98.01% 98.01% +<.01%
==========================================
Files 39 39
Lines 7245 7270 +25
Branches 1258 1264 +6
==========================================
+ Hits 7101 7126 +25
Misses 44 44
Partials 100 100
Continue to review full report at Codecov.
|
@@ -39,12 +39,18 @@ | |||
__all__ = ('ClientRequest', 'ClientResponse', 'RequestInfo', 'Fingerprint') | |||
|
|||
|
|||
ContentDisposition = collections.namedtuple( | |||
'ContentDisposition', ('type', 'parameters', 'filename')) | |||
@attr.s(frozen=True) |
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 have my doubts about the usage of frozen=True
taking into account that immutability depends on the type of fields that you have in the data class. In the worst case scenario having a mutable attribute, the hash
will fail and you won't prevent modify these attributes.
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.
Sorry, I don't follow your point.
ConetentDisposition
was immutable named tuple, I've just converted the class into immutable attrs
representation. All type
, parameters
and filename
attributes are immutable too.
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.
... class Foo:
... a = attr.ib(type=MappingProxyType)
...
>>> d = {}
>>> m = MappingProxyType(d)
>>> f = Foo(m)
>>> hash(f)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<attrs generated hash a57df858a086a8e0acf97d938d787d39b6f33d51>", line 4, in __hash__
TypeError: unhashable type: 'mappingproxy'```
type = attr.ib(type=str) | ||
subtype = attr.ib(type=str) | ||
suffix = attr.ib(type=str) | ||
parameters = attr.ib(type=MultiDict) |
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.
MultiDict
is mutable frozen=True
is IMHO inconsistent in that case.
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.
Agree, but afaik that mimics old behavior. May be worth to replace with frozen/proxy version?
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.
Few more notes, but nice improvement!
aiohttp/web_request.py
Outdated
FileField = collections.namedtuple( | ||
'Field', 'name filename file content_type headers') | ||
|
||
@attr.s(frozen=True, slots=True) |
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.
Rest namedtuple replaces comes without slots. Is that intentional?
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.
Actually not . I forgot adding new param and was experimenting with different usage.
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.
Let's not use slots in attrs, they are not bring new value but slightly slower.
type = attr.ib(type=str) | ||
subtype = attr.ib(type=str) | ||
suffix = attr.ib(type=str) | ||
parameters = attr.ib(type=MultiDict) |
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.
Agree, but afaik that mimics old behavior. May be worth to replace with frozen/proxy version?
Btw, what do you think about dataclasses? Would we migrate to them eventually or will stay with attrs? |
I want keeping |
By the way, |
@kxepal we can return to discussion about switching to native python dataclasses after dropping Python 3.6 support. In several years. |
@webknjaz I know about dataclass support embedded into attrs, but we cannot use it until dropping Python 3.5 support. |
@asvetlov |
https://pypi.python.org/pypi/dataclasses requires Python 3.6, we are limited to 3.5.3 |
Yeah. Ok, I got your point. Thanks! |
What if there was a backport? |
@webknjaz don't follow |
@asvetlov I don't see the point of usage by default |
|
@asvetlov I mean, shouldn't it be possible to backport dataclasses implementation to support 3.5? |
this is clear, Im just saying that the usage of frozen can become error
prone, this feature do not prevent manipulate mutable data, perhaps
foo.headers.pop('header name').
IMHO this flag can give you a false sensation of protection.
El 26/01/2018 13:19, "Andrew Svetlov" <[email protected]> escribió:
… frozen prevents inplace modification:
In [1]: import attr
In [2]: @attr.s
...: class A:
...: a = attr.ib()
...:
In [3]: a = A(1)
In [4]: a.a
Out[4]: 1
In [5]: a.a = 2
In [6]: a.a
Out[6]: 2
In [7]: @attr.s(frozen=True)
...: class B:
...: b = attr.ib()
...:
In [8]: b = B(1)
In [9]: b.b
Out[9]: 1
In [10]: b.b = 2
---------------------------------------------------------------------------
FrozenInstanceError Traceback (most recent call last)
<ipython-input-10-dc6c31b33bcf> in <module>()
----> 1 b.b = 2
~/.virtualenvs/aiohttp/lib/python3.6/site-packages/attr/_make.py in _frozen_setattrs(self, name, value)
349 Attached to frozen classes as __setattr__.
350 """
--> 351 raise FrozenInstanceError()
352
353
FrozenInstanceError:
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#2690 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABK1iRPmY12oZP3ijVaFuR2xx_Ut5vOTks5tOcLCgaJpZM4RtmSA>
.
|
Fair enough |
@pfreixes I understand that |
Well, at least the whole replacement is safer for all variables, and must be the developer who has the last call to expose a variable mutable or not mutanle. A triki situatuion. Anyway, lets move forward. LGTM |
|
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs. |
No description provided.