-
-
Notifications
You must be signed in to change notification settings - Fork 374
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
T325 weakref with slots #420
Conversation
Hmm, I should probably add a test to confirm that no extra fields are added. Kind of a given I think, but seeing as the workaround involved creating one and I don't think we want that, it seems worth a couple lines of test code I show and verify that desire. I should also probably change my added tests to use hypothesis... :| |
Hmmm, IIUC, you’re only setting It’s even questionable whether it should be an option at all, but if it is, it might even be renamed to |
I'd be fine with switching on the |
@hynek, yes, the intent was to only add TypeError: __weakref__ slot disallowed: either we already got one, or __itemsize__ != 0 Same thing happens with an attribute named |
If the option is kept and renamed, perhaps Would we want an exception in case of |
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 think to me it’s more important that we do what’s right/expected so I’m gonna say: make it an option but make it True by default. People like Tin who care about 8 bytes can always set it to False.
- Yes, it should be renamed.
weakref_slot
sounds fine. - Yes, exception would be good.
changelog.d/420.change.rst
Outdated
@@ -0,0 +1 @@ | |||
Add weakref parameter to attr.s() allowing for weak referenceable slots classes. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/attr/_make.py
Outdated
@@ -719,6 +730,8 @@ def attrs( | |||
``object.__setattr__(self, "attribute_name", value)``. | |||
|
|||
.. _slots: https://docs.python.org/3/reference/datamodel.html#slots | |||
:param bool weakref: Make instancess weak-referenceable. This has no |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Once we switch to I'm coding towards |
Changing the default broke some tests so I stashed that and went ahead and added parametrization to them for |
It does make sense to me that |
FWIF, I can live with not having an exception. I’d like to point out tho, that it’s specifically
Otherwise I think everything we settled before is still true? True by default so Tin can save his precious bytes, …? |
I couldn't reason to a sensible exception with the name and default Anyways, I'm not waiting on anyone but myself to get back to this and figure out the failures. |
Nah it’s fine, as I said, you can drop the exception. Default True, the docs explain that it’s a NOP on dict classes. |
Do we have outstanding actions on this at this point? Should any of the tests I added use Hypothesis? Or others that haven't been added but should be? |
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 think it’s mostly fine now, it just needs a bunch of updates where is still refers to weakref
instead of weakref_slot
.
Also please add an example to tests/typing_example.py
to ensure the stubs work (we’ve almost missed the wrong name!).
changelog.d/420.change.rst
Outdated
@@ -0,0 +1 @@ | |||
Add weakref parameter to attr.s() allowing for weak referenceable slotted classes. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
docs/glossary.rst
Outdated
@@ -43,21 +43,11 @@ Glossary | |||
Those methods are created for frozen slotted classes because they won't pickle otherwise. | |||
`Think twice <https://www.youtube.com/watch?v=7KnfGDajDQw>`_ before using :mod:`pickle` though. | |||
|
|||
- As always with slotted classes, you must specify a ``__weakref__`` slot if you wish for the class to be weak-referenceable. | |||
Here's how it looks using ``attrs``: | |||
- Slotted classes are weak-referenceable by default. This can be disabled in CPython by passing ``weakref_slot=False`` to ``@attr.s`` [#pypyweakref]_. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/attr/__init__.pyi
Outdated
@@ -164,6 +164,7 @@ def attrs( | |||
init: bool = ..., | |||
slots: bool = ..., | |||
frozen: bool = ..., | |||
weakref: bool = ..., |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/attr/__init__.pyi
Outdated
@@ -180,6 +181,7 @@ def attrs( | |||
init: bool = ..., | |||
slots: bool = ..., | |||
frozen: bool = ..., | |||
weakref: bool = ..., |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/attr/__init__.pyi
Outdated
@@ -207,6 +209,7 @@ def make_class( | |||
init: bool = ..., | |||
slots: bool = ..., | |||
frozen: bool = ..., | |||
weakref: bool = ..., |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
src/attr/_make.py
Outdated
@@ -759,6 +785,8 @@ def attrs( | |||
``object.__setattr__(self, "attribute_name", value)``. | |||
|
|||
.. _slots: https://docs.python.org/3/reference/datamodel.html#slots | |||
:param bool weakref: Make instances weak-referenceable. This has no |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
tests/strategies.py
Outdated
If `slots=True` is passed in, only slots classes will be generated, and | ||
if `slots=False` is passed in, no slot classes will be generated. The same | ||
applies to `frozen`. | ||
By default, all combinations of slots, frozen, and weakref classes will be |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
tests/strategies.py
Outdated
By default, all combinations of slots, frozen, and weakref classes will be | ||
generated. If `slots=True` is passed in, only slots classes will be | ||
generated, and if `slots=False` is passed in, no slot classes will be | ||
generated. The same applies to `frozen` and `weakref`. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@hynek, sorry... i thought i had done all that a few days ago. |
Thanks! |
#325