Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error messages from Final and NoPublicConstructor #1497

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to do {__module__}.{__qualname__} for maximum comprehensibility?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Done.

)
)
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