Skip to content

Commit

Permalink
Better error messages from Final and NoPublicConstructor
Browse files Browse the repository at this point in the history
This is a pretty trivial change, but I noticed it while working on
python-triogh-1496 so I figured I'd just fix it.
  • Loading branch information
njsmith committed May 6, 2020
1 parent f07c4fb commit b94e808
Show file tree
Hide file tree
Showing 3 changed files with 15 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
8 changes: 6 additions & 2 deletions trio/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ def __new__(cls, name, bases, cls_namespace):
for base in bases:
if isinstance(base, Final):
raise TypeError(
"`%s` does not support subclassing" % base.__name__
"the {!r} class does not support subclassing".format(
base.__name__
)
)
return super().__new__(cls, name, bases, cls_namespace)

Expand All @@ -240,7 +242,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(
"the {!r} class has no public constructor".format(self.__name__)
)

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

0 comments on commit b94e808

Please sign in to comment.