Skip to content

Add NoReturn type #397

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

Merged
merged 2 commits into from
Mar 27, 2017
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
36 changes: 35 additions & 1 deletion python2/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from unittest import TestCase, main, SkipTest
from copy import copy, deepcopy

from typing import Any
from typing import Any, NoReturn
from typing import TypeVar, AnyStr
from typing import T, KT, VT # Not in __all__.
from typing import Union, Optional
Expand Down Expand Up @@ -110,6 +110,40 @@ def test_any_is_subclass(self):
typing.IO[Any]


class NoReturnTests(BaseTestCase):

def test_noreturn_instance_type_error(self):
with self.assertRaises(TypeError):
isinstance(42, NoReturn)

def test_noreturn_subclass_type_error(self):
with self.assertRaises(TypeError):
issubclass(Employee, NoReturn)
with self.assertRaises(TypeError):
issubclass(NoReturn, Employee)

def test_repr(self):
self.assertEqual(repr(NoReturn), 'typing.NoReturn')

def test_not_generic(self):
with self.assertRaises(TypeError):
NoReturn[int]

def test_cannot_subclass(self):
with self.assertRaises(TypeError):
class A(NoReturn):
pass
with self.assertRaises(TypeError):
class A(type(NoReturn)):
pass

def test_cannot_instantiate(self):
with self.assertRaises(TypeError):
NoReturn()
with self.assertRaises(TypeError):
type(NoReturn)()


class TypeVarTests(BaseTestCase):

def test_basic_plain(self):
Expand Down
34 changes: 34 additions & 0 deletions python2/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,40 @@ def __subclasscheck__(self, cls):
Any = _Any(_root=True)


class NoReturnMeta(TypingMeta):
"""Metaclass for NoReturn."""

def __new__(cls, name, bases, namespace):
cls.assert_no_subclassing(bases)
self = super(NoReturnMeta, cls).__new__(cls, name, bases, namespace)
return self


class _NoReturn(_FinalTypingBase):
"""Special type indicating functions that never return.
Example::

from typing import NoReturn

def stop() -> NoReturn:
raise Exception('no way')

This type is invalid in other positions, e.g., ``List[NoReturn]``
will fail in static type checkers.
"""
__metaclass__ = NoReturnMeta
__slots__ = ()

def __instancecheck__(self, obj):
raise TypeError("NoReturn cannot be used with isinstance().")

def __subclasscheck__(self, cls):
raise TypeError("NoReturn cannot be used with issubclass().")


NoReturn = _NoReturn(_root=True)


class TypeVarMeta(TypingMeta):
def __new__(cls, name, bases, namespace):
cls.assert_no_subclassing(bases)
Expand Down
36 changes: 35 additions & 1 deletion src/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from unittest import TestCase, main, skipUnless, SkipTest
from copy import copy, deepcopy

from typing import Any
from typing import Any, NoReturn
from typing import TypeVar, AnyStr
from typing import T, KT, VT # Not in __all__.
from typing import Union, Optional
Expand Down Expand Up @@ -113,6 +113,40 @@ def test_any_works_with_alias(self):
typing.IO[Any]


class NoReturnTests(BaseTestCase):

def test_noreturn_instance_type_error(self):
with self.assertRaises(TypeError):
isinstance(42, NoReturn)

def test_noreturn_subclass_type_error(self):
with self.assertRaises(TypeError):
issubclass(Employee, NoReturn)
with self.assertRaises(TypeError):
issubclass(NoReturn, Employee)

def test_repr(self):
self.assertEqual(repr(NoReturn), 'typing.NoReturn')

def test_not_generic(self):
with self.assertRaises(TypeError):
NoReturn[int]

def test_cannot_subclass(self):
with self.assertRaises(TypeError):
class A(NoReturn):
pass
with self.assertRaises(TypeError):
class A(type(NoReturn)):
pass

def test_cannot_instantiate(self):
with self.assertRaises(TypeError):
NoReturn()
with self.assertRaises(TypeError):
type(NoReturn)()


class TypeVarTests(BaseTestCase):

def test_basic_plain(self):
Expand Down
25 changes: 25 additions & 0 deletions src/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,31 @@ def __subclasscheck__(self, cls):
Any = _Any(_root=True)


class _NoReturn(_FinalTypingBase, _root=True):
"""Special type indicating functions that never return.
Example::

from typing import NoReturn

def stop() -> NoReturn:
raise Exception('no way')

This type is invalid in other positions, e.g., ``List[NoReturn]``
will fail in static type checkers.
"""

__slots__ = ()

def __instancecheck__(self, obj):
raise TypeError("NoReturn cannot be used with isinstance().")

def __subclasscheck__(self, cls):
raise TypeError("NoReturn cannot be used with issubclass().")


NoReturn = _NoReturn(_root=True)


class TypeVar(_TypingBase, _root=True):
"""Type variable.

Expand Down