Skip to content

Commit faa9967

Browse files
improve robustness of WithDecoratedMethods
1 parent 8e30d8f commit faa9967

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Changelog
2+
## Pedantic 2.3.1
3+
- improve robustness of `WithDecoratedMethods`
4+
25
## Pedantic 2.3.0
36
- added support for Python 3.14
47
- updated dependencies

pedantic/mixins/with_decorated_methods.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ def get_decorated_functions(self) -> dict[DecoratorTypeVar, dict[C, T]]:
8888
if attribute_name.startswith('__') or attribute_name in ['type_var', 'type_vars']:
8989
continue
9090

91-
attribute = getattr(self, attribute_name)
91+
try:
92+
attribute = getattr(self, attribute_name)
93+
except BaseException:
94+
continue # ignore bad attributes
9295

9396
for decorator_type in decorator_types: # type: ignore
9497
if hasattr(attribute, decorator_type):

pedantic/tests/test_with_decorated_methods.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22
from functools import wraps
3-
from typing import TypeVar, Generic
3+
from typing import TypeVar, Generic, NoReturn
44

55
from pedantic import DecoratorType, create_decorator, WithDecoratedMethods
66

@@ -16,6 +16,22 @@ class Decorators(DecoratorType):
1616

1717

1818
class TestWithDecoratedMethods(unittest.TestCase):
19+
def test_no_decorated_methods(self):
20+
class MyClass(WithDecoratedMethods[Decorators]):
21+
pass
22+
23+
instance = MyClass()
24+
assert instance.get_decorated_functions() == {Decorators.FOO: {}, Decorators.BAR: {}}
25+
26+
def test_class_with_bad_property(self):
27+
class MyClass(WithDecoratedMethods[Decorators]):
28+
@property
29+
def bad(self) -> NoReturn:
30+
raise RuntimeError('bad man')
31+
32+
instance = MyClass()
33+
assert instance.get_decorated_functions() == {Decorators.FOO: {}, Decorators.BAR: {}}
34+
1935
def test_with_decorated_methods_sync(self):
2036
class MyClass(WithDecoratedMethods[Decorators]):
2137
@foo(42)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "pedantic"
7-
version = "2.3.0"
7+
version = "2.3.1"
88
description = "Some useful Python decorators for cleaner software development."
99
readme = "README.md"
1010
requires-python = ">=3.11"

0 commit comments

Comments
 (0)