Skip to content

Commit

Permalink
Merge pull request #1497 from njsmith/better-metaclass-error-messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pquentin authored May 8, 2020
2 parents a9919d3 + 4807db7 commit 091919b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 29 deletions.
26 changes: 6 additions & 20 deletions trio/_core/tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2155,11 +2155,7 @@ async def inner():


def test_Nursery_init():
check_Nursery_error = pytest.raises(
TypeError, match='no public constructor available'
)

with check_Nursery_error:
with pytest.raises(TypeError):
_core._run.Nursery(None, None)


Expand All @@ -2170,23 +2166,17 @@ async def test_Nursery_private_init():


def test_Nursery_subclass():
with pytest.raises(
TypeError, match='`Nursery` does not support subclassing'
):
with pytest.raises(TypeError):

class Subclass(_core._run.Nursery):
pass


def test_Cancelled_init():
check_Cancelled_error = pytest.raises(
TypeError, match='no public constructor available'
)

with check_Cancelled_error:
with pytest.raises(TypeError):
raise _core.Cancelled

with check_Cancelled_error:
with pytest.raises(TypeError):
_core.Cancelled()

# private constructor should not raise
Expand All @@ -2199,18 +2189,14 @@ def test_Cancelled_str():


def test_Cancelled_subclass():
with pytest.raises(
TypeError, match='`Cancelled` does not support subclassing'
):
with pytest.raises(TypeError):

class Subclass(_core.Cancelled):
pass


def test_CancelScope_subclass():
with pytest.raises(
TypeError, match='`CancelScope` does not support subclassing'
):
with pytest.raises(TypeError):

class Subclass(_core.CancelScope):
pass
Expand Down
6 changes: 4 additions & 2 deletions trio/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def __new__(cls, name, bases, cls_namespace):
for base in bases:
if isinstance(base, Final):
raise TypeError(
"`%s` does not support subclassing" % base.__name__
f"{base.__module__}.{base.__qualname__} does not support subclassing"
)
return super().__new__(cls, name, bases, cls_namespace)

Expand Down Expand Up @@ -256,7 +256,9 @@ class SomeClass(metaclass=NoPublicConstructor):
- TypeError if a sub class or an instance is created.
"""
def __call__(self, *args, **kwargs):
raise TypeError("no public constructor available")
raise TypeError(
f"{self.__module__}.{self.__qualname__} has no public constructor"
)

def _create(self, *args, **kwargs):
return super().__call__(*args, **kwargs)
10 changes: 3 additions & 7 deletions trio/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ def test_final_metaclass():
class FinalClass(metaclass=Final):
pass

with pytest.raises(
TypeError, match="`FinalClass` does not support subclassing"
):
with pytest.raises(TypeError):

class SubClass(FinalClass):
pass
Expand All @@ -122,12 +120,10 @@ def test_no_public_constructor_metaclass():
class SpecialClass(metaclass=NoPublicConstructor):
pass

with pytest.raises(TypeError, match="no public constructor available"):
with pytest.raises(TypeError):
SpecialClass()

with pytest.raises(
TypeError, match="`SpecialClass` does not support subclassing"
):
with pytest.raises(TypeError):

class SubClass(SpecialClass):
pass
Expand Down

0 comments on commit 091919b

Please sign in to comment.