Skip to content

Commit

Permalink
Handle strange exceptions raised on attempting to access __set_name__
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Nov 17, 2023
1 parent f2a57b2 commit 1909fd5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7577,6 +7577,22 @@ class NamedTupleClass(NamedTuple):
normal_exception.__notes__[0].replace("NormalClass", "NamedTupleClass")
)

def test_exception_raised_if_accessing_set_name_causes_strange_error(self):
class Meta(type):
def __getattribute__(self, attr):
if attr == "__set_name__":
raise TypeError("NO")

class VeryAnnoying(metaclass=Meta): pass

# Sanity check to make sure the test is setup correctly:
with self.assertRaises(TypeError):
VeryAnnoying.__set_name__

# The real test here is just that this class creation succeeds:
class Foo(NamedTuple):
attr = VeryAnnoying()


class TypedDictTests(BaseTestCase):
def test_basics_functional_syntax(self):
Expand Down
2 changes: 1 addition & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2734,7 +2734,7 @@ def __new__(cls, typename, bases, ns):
setattr(nm_tpl, key, val)
try:
set_name = type(val).__set_name__
except AttributeError:
except Exception:
pass
else:
try:
Expand Down

0 comments on commit 1909fd5

Please sign in to comment.