Fix issue 589 (#590)

* from parsyfiles development (https://github.com/smarie/python-simple-file-collection-parsing-framework):
* added new 'chain' validator to allow users to chain their custom validators with, for example, 'instance_of'
* added utility methods to get the declared attribute type (by inspecting its validators) and to check if it is optional

* from parsyfiles development (https://github.com/smarie/python-simple-file-collection-parsing-framework):
* added tests
* fixed bug with optional validator to pass tests :)

* from parsyfiles development (https://github.com/smarie/python-simple-file-collection-parsing-framework):
* completed doc api.rst
* small mod in tests

* Fixed issue with empty cell in closure when slots are used. Fixed #589

* Removed api.rst mods erroneously injected from another branch.

* Removed api.rst mods erroneously injected from another branch.

* noqa so that flake8 is happy

* Fixed lint error

* Added towncrier changelog entry

* Attempt to fix linter error

* Update tests/test_slots.py

* Last lint fix

* Update changelog.d/590.change.rst

Co-Authored-By: Hynek Schlawack <hs@ox.cx>

* Added extensive doc in the new test

* Update tests/test_slots.py

* last lint error

Co-authored-by: Sylvain MARIE <sylvain.marie@se.com>
Co-authored-by: Hynek Schlawack <hs@ox.cx>
This commit is contained in:
smarie 2020-03-13 06:06:14 +01:00 committed by GitHub
parent 4bd6827091
commit 07810aec7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 2 deletions

View File

@ -0,0 +1 @@
Fixed a ``ValueError: Cell is empty`` bug that could happen in some rare edge cases.

View File

@ -641,8 +641,13 @@ class _ClassBuilder(object):
if not closure_cells: # Catch None or the empty list.
continue
for cell in closure_cells:
if cell.cell_contents is self._cls:
set_closure_cell(cell, cls)
try:
match = cell.cell_contents is self._cls
except ValueError: # ValueError: Cell is empty
pass
else:
if match:
set_closure_cell(cell, cls)
return cls

View File

@ -543,3 +543,28 @@ def tests_weakref_does_not_add_with_weakref_attribute():
w = weakref.ref(c)
assert c is w()
def test_slots_empty_cell():
"""
Tests that no `ValueError: Cell is empty` exception is raised when
closure cells are present with no contents in a `slots=True` class.
(issue https://github.com/python-attrs/attrs/issues/589)
On Python 3, if a method mentions `__class__` or uses the no-arg `super()`,
the compiler will bake a reference to the class in the method itself as
`method.__closure__`. Since `attrs` replaces the class with a clone,
`_ClassBuilder._create_slots_class(self)` will rewrite these references so
it keeps working. This method was not properly covering the edge case where
the closure cell was empty, we fixed it and this is the non-regression
test.
"""
@attr.s(slots=True)
class C(object):
field = attr.ib()
def f(self, a):
super(C, self).__init__()
C(field=1)