Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -216,7 +216,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 All @@ -240,7 +240,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 @@ -112,12 +110,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