Skip to content

Commit

Permalink
Allow InstancePropertyHelper to accept properties with names on Pytho…
Browse files Browse the repository at this point in the history
…n 3.13+

Fixes Pylons#3761
  • Loading branch information
hroncok committed Jun 17, 2024
1 parent ef0f686 commit 1079613
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/pyramid/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def make_property(cls, callable, name=None, reify=False):
if name is None:
if not hasattr(callable, '__name__'):
raise ValueError(
'missing __name__, must specify "name" for property'
'missing __name__, must specify "name" for property '
'on Python < 3.13'
)
name = callable.__name__
name = get_callable_name(name)
Expand Down
28 changes: 22 additions & 6 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,21 @@ def worker(obj):

def test_property_without_name(self):
def worker(obj): # pragma: no cover
pass
return obj.bar

foo = Dummy()
helper = self._getTargetClass()
self.assertRaises(
ValueError, helper.set_property, foo, property(worker)
)
if sys.version_info < (3, 13):
self.assertRaises(
ValueError, helper.set_property, foo, property(worker)
)
else:
# Since Python 3.13, the name of the property is automatic
helper.set_property(foo, property(worker))
foo.bar = 1
self.assertEqual(1, foo.worker)
foo.bar = 2
self.assertEqual(2, foo.worker)

def test_property_with_name(self):
def worker(obj):
Expand Down Expand Up @@ -272,10 +280,18 @@ def worker(obj):

def test_property_without_name(self):
def worker(obj): # pragma: no cover
pass
return obj.bar

foo = self._makeOne()
self.assertRaises(ValueError, foo.set_property, property(worker))
if sys.version_info < (3, 13):
self.assertRaises(ValueError, foo.set_property, property(worker))
else:
# Since Python 3.13, the name of the property is automatic
foo.set_property(property(worker))
foo.bar = 1
self.assertEqual(1, foo.worker)
foo.bar = 2
self.assertEqual(2, foo.worker)

def test_property_with_name(self):
def worker(obj):
Expand Down

0 comments on commit 1079613

Please sign in to comment.