summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2016-06-08 18:19:11 (GMT)
committerGuido van Rossum <guido@python.org>2016-06-08 18:19:11 (GMT)
commit91185fe0284a04162e0b3425b53be49bdbfad67d (patch)
treed7ce0fa5970b79c29850d61a0e7b79479c39e874 /Lib/test
parent07a9fcdc86b740ac5c5108d10418e7668a06e3df (diff)
downloadcpython-91185fe0284a04162e0b3425b53be49bdbfad67d.zip
cpython-91185fe0284a04162e0b3425b53be49bdbfad67d.tar.gz
cpython-91185fe0284a04162e0b3425b53be49bdbfad67d.tar.bz2
Sync typing.py with upstream.
(Upstream is https://github.com/python/typing) - Add TYPE_CHECKING (false at runtime, true in type checkers) (upstream #230). - Avoid error on Union[xml.etree.cElementTree.Element, str] (upstream #229). - Repr of Tuple[()] should be 'Tuple[()]' (upstream #231). - Add NewType() (upstream #189).
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_typing.py38
1 files changed, 36 insertions, 2 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index ade8a35..a7f8dd5 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -3,7 +3,7 @@ import collections
import pickle
import re
import sys
-from unittest import TestCase, main, skipUnless
+from unittest import TestCase, main, skipUnless, SkipTest
from typing import Any
from typing import TypeVar, AnyStr
@@ -16,6 +16,7 @@ from typing import cast
from typing import get_type_hints
from typing import no_type_check, no_type_check_decorator
from typing import Type
+from typing import NewType
from typing import NamedTuple
from typing import IO, TextIO, BinaryIO
from typing import Pattern, Match
@@ -339,6 +340,20 @@ class UnionTests(BaseTestCase):
A = Union[str, Pattern]
A
+ def test_etree(self):
+ # See https://github.com/python/typing/issues/229
+ # (Only relevant for Python 2.)
+ try:
+ from xml.etree.cElementTree import Element
+ except ImportError:
+ raise SkipTest("cElementTree not found")
+ Union[Element, str] # Shouldn't crash
+
+ def Elem(*args):
+ return Element(*args)
+
+ Union[Elem, str] # Nor should this
+
class TypeVarUnionTests(BaseTestCase):
@@ -410,7 +425,7 @@ class TupleTests(BaseTestCase):
def test_repr(self):
self.assertEqual(repr(Tuple), 'typing.Tuple')
- self.assertEqual(repr(Tuple[()]), 'typing.Tuple[]')
+ self.assertEqual(repr(Tuple[()]), 'typing.Tuple[()]')
self.assertEqual(repr(Tuple[int, float]), 'typing.Tuple[int, float]')
self.assertEqual(repr(Tuple[int, ...]), 'typing.Tuple[int, ...]')
@@ -1401,6 +1416,25 @@ class TypeTests(BaseTestCase):
joe = new_user(BasicUser)
+class NewTypeTests(BaseTestCase):
+
+ def test_basic(self):
+ UserId = NewType('UserId', int)
+ UserName = NewType('UserName', str)
+ self.assertIsInstance(UserId(5), int)
+ self.assertIsInstance(UserName('Joe'), str)
+ self.assertEqual(UserId(5) + 1, 6)
+
+ def test_errors(self):
+ UserId = NewType('UserId', int)
+ UserName = NewType('UserName', str)
+ with self.assertRaises(TypeError):
+ issubclass(UserId, int)
+ with self.assertRaises(TypeError):
+ class D(UserName):
+ pass
+
+
class NamedTupleTests(BaseTestCase):
def test_basics(self):