Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7d73596
Started #136
ilevkivskyi Sep 18, 2016
20de824
Started #136
ilevkivskyi Sep 18, 2016
8e6f0a6
More work on #136
ilevkivskyi Sep 18, 2016
bcde4c6
Finish first round of #136
ilevkivskyi Sep 18, 2016
9987bb2
Shorter error messages
ilevkivskyi Sep 18, 2016
90d72da
Added lru_cache for Generic[]
ilevkivskyi Sep 18, 2016
603ff6d
Flexible cache for Generic[]
ilevkivskyi Sep 18, 2016
fa4a681
Skip GenericMeta in FrozenSetMeta MRO
ilevkivskyi Sep 18, 2016
cc99972
Revert "Skip GenericMeta in FrozenSetMeta MRO"
ilevkivskyi Sep 18, 2016
b587dc0
Remove redundant metaclass for frozenset
ilevkivskyi Sep 18, 2016
e25b303
Some formatting
ilevkivskyi Sep 18, 2016
3ccb7a9
Fix async/await tests
ilevkivskyi Sep 18, 2016
0606059
Fixed ContextManger test
ilevkivskyi Sep 18, 2016
8b909d6
Be consistent about _TypeAlias
ilevkivskyi Sep 18, 2016
554f2ed
Removed debugging print()
ilevkivskyi Sep 18, 2016
11189b0
Simplified code
ilevkivskyi Sep 19, 2016
4c91c3b
Increased cache size to 50; start backporting changes to Python 2
ilevkivskyi Sep 22, 2016
1920009
Backport Union and Optional to Python 2
ilevkivskyi Sep 23, 2016
f003eca
Finished backport to Python 2; some polishing is needed
ilevkivskyi Sep 23, 2016
e130c77
Added many unit tests for bugs fixed by PR
ilevkivskyi Sep 24, 2016
fe2d3b7
Updated docstrings and other minor things
ilevkivskyi Sep 24, 2016
e4cebff
Optimized caching + first part of response to comments
ilevkivskyi Sep 25, 2016
9b443cb
Second part of response to comments. Need to port all comments etc. t…
ilevkivskyi Sep 26, 2016
bd160a1
Start backporting comments to Python2; improve caching
ilevkivskyi Sep 26, 2016
80ce513
Get rid of frozensetmeta, it causes random failures
ilevkivskyi Sep 26, 2016
c4f11e8
Backported everything to Python 2
Sep 26, 2016
230a500
Do not duplicate real errors while caching
ilevkivskyi Sep 26, 2016
e8c85b2
Merge remote-tracking branch 'origin/master'
ilevkivskyi Sep 26, 2016
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
Prev Previous commit
Next Next commit
Added many unit tests for bugs fixed by PR
  • Loading branch information
ilevkivskyi committed Sep 24, 2016
commit e130c770d7fd3bb3a908f259e7587eb8a600700e
70 changes: 69 additions & 1 deletion python2/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import TypeVar, AnyStr
from typing import T, KT, VT # Not in __all__.
from typing import Union, Optional
from typing import Tuple, List
from typing import Tuple, List, MutableMapping
from typing import Callable
from typing import Generic, ClassVar
from typing import cast
Expand All @@ -20,6 +20,10 @@
from typing import IO, TextIO, BinaryIO
from typing import Pattern, Match
import typing
try:
import collections.abc as collections_abc
except ImportError:
import collections as collections_abc # Fallback for PY3.2.


class BaseTestCase(TestCase):
Expand Down Expand Up @@ -80,10 +84,15 @@ def test_cannot_subclass(self):
with self.assertRaises(TypeError):
class A(Any):
pass
with self.assertRaises(TypeError):
class A(type(Any)):
pass

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

def test_cannot_subscript(self):
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -244,6 +253,9 @@ def test_cannot_subclass(self):
with self.assertRaises(TypeError):
class C(Union):
pass
with self.assertRaises(TypeError):
class C(type(Union)):
pass
with self.assertRaises(TypeError):
class C(Union[int, str]):
pass
Expand All @@ -254,6 +266,13 @@ def test_cannot_instantiate(self):
u = Union[int, float]
with self.assertRaises(TypeError):
u()
with self.assertRaises(TypeError):
type(u)()

def test_union_generalization(self):
self.assertFalse(Union[str, typing.Iterable[int]] == str)
self.assertFalse(Union[str, typing.Iterable[int]] == typing.Iterable[int])
self.assertTrue(Union[str, typing.Iterable] == typing.Iterable)

def test_optional(self):
o = Optional[int]
Expand Down Expand Up @@ -353,6 +372,11 @@ def test_cannot_subclass(self):
class C(Callable):
pass

with self.assertRaises(TypeError):

class C(type(Callable)):
pass

with self.assertRaises(TypeError):

class C(Callable[[int], int]):
Expand All @@ -361,9 +385,13 @@ class C(Callable[[int], int]):
def test_cannot_instantiate(self):
with self.assertRaises(TypeError):
Callable()
with self.assertRaises(TypeError):
type(Callable)()
c = Callable[[int], str]
with self.assertRaises(TypeError):
c()
with self.assertRaises(TypeError):
type(c)()

def test_callable_instance_works(self):
def f():
Expand Down Expand Up @@ -540,6 +568,42 @@ class C(B[int]):
c.bar = 'abc'
self.assertEqual(c.__dict__, {'bar': 'abc'})

def test_false_subclasses(self):
class MyMapping(MutableMapping[str, str]): pass
self.assertNotIsInstance({}, MyMapping)
self.assertNotIsSubclass(dict, MyMapping)

def test_multiple_abc_bases(self):
class MM1(MutableMapping[str, str], collections_abc.MutableMapping):
def __getitem__(self, k):
return None
def __setitem__(self, k, v):
pass
def __delitem__(self, k):
pass
def __iter__(self):
return iter(())
def __len__(self):
return 0
class MM2(collections_abc.MutableMapping, MutableMapping[str, str]):
def __getitem__(self, k):
return None
def __setitem__(self, k, v):
pass
def __delitem__(self, k):
pass
def __iter__(self):
return iter(())
def __len__(self):
return 0
# these two should just work
MM1().update()
MM2().update()
self.assertIsInstance(MM1(), collections_abc.MutableMapping)
self.assertIsInstance(MM1(), MutableMapping)
self.assertIsInstance(MM2(), collections_abc.MutableMapping)
self.assertIsInstance(MM2(), MutableMapping)

def test_pickle(self):
global C # pickle wants to reference the class by name
T = TypeVar('T')
Expand Down Expand Up @@ -722,6 +786,8 @@ class C(type(ClassVar[int])):
pass

def test_cannot_init(self):
with self.assertRaises(TypeError):
ClassVar()
with self.assertRaises(TypeError):
type(ClassVar)()
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -1116,10 +1182,12 @@ def test_basics(self):
pat = re.compile('[a-z]+', re.I)
self.assertIsSubclass(pat.__class__, Pattern)
self.assertIsSubclass(type(pat), Pattern)
self.assertIsInstance(pat, Pattern)

mat = pat.search('12345abcde.....')
self.assertIsSubclass(mat.__class__, Match)
self.assertIsSubclass(type(mat), Match)
self.assertIsInstance(mat, Match)

# these should just work
p = Pattern[Union[str, bytes]]
Expand Down
72 changes: 71 additions & 1 deletion src/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import TypeVar, AnyStr
from typing import T, KT, VT # Not in __all__.
from typing import Union, Optional
from typing import Tuple, List
from typing import Tuple, List, MutableMapping
from typing import Callable
from typing import Generic, ClassVar
from typing import cast
Expand All @@ -21,6 +21,10 @@
from typing import IO, TextIO, BinaryIO
from typing import Pattern, Match
import typing
try:
import collections.abc as collections_abc
except ImportError:
import collections as collections_abc # Fallback for PY3.2.


class BaseTestCase(TestCase):
Expand Down Expand Up @@ -81,10 +85,15 @@ def test_cannot_subclass(self):
with self.assertRaises(TypeError):
class A(Any):
pass
with self.assertRaises(TypeError):
class A(type(Any)):
pass

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

def test_cannot_subscript(self):
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -245,16 +254,28 @@ def test_cannot_subclass(self):
with self.assertRaises(TypeError):
class C(Union):
pass
with self.assertRaises(TypeError):
class C(type(Union)):
pass
with self.assertRaises(TypeError):
class C(Union[int, str]):
pass

def test_cannot_instantiate(self):
with self.assertRaises(TypeError):
Union()
with self.assertRaises(TypeError):
type(Union)()
u = Union[int, float]
with self.assertRaises(TypeError):
u()
with self.assertRaises(TypeError):
type(u)()

def test_union_generalization(self):
self.assertFalse(Union[str, typing.Iterable[int]] == str)
self.assertFalse(Union[str, typing.Iterable[int]] == typing.Iterable[int])
self.assertTrue(Union[str, typing.Iterable] == typing.Iterable)

def test_optional(self):
o = Optional[int]
Expand Down Expand Up @@ -354,6 +375,11 @@ def test_cannot_subclass(self):
class C(Callable):
pass

with self.assertRaises(TypeError):

class C(type(Callable)):
pass

with self.assertRaises(TypeError):

class C(Callable[[int], int]):
Expand All @@ -362,9 +388,13 @@ class C(Callable[[int], int]):
def test_cannot_instantiate(self):
with self.assertRaises(TypeError):
Callable()
with self.assertRaises(TypeError):
type(Callable)()
c = Callable[[int], str]
with self.assertRaises(TypeError):
c()
with self.assertRaises(TypeError):
type(c)()

def test_callable_instance_works(self):
def f():
Expand Down Expand Up @@ -565,6 +595,42 @@ class C(B[int]):
c.bar = 'abc'
self.assertEqual(c.__dict__, {'bar': 'abc'})

def test_false_subclasses(self):
class MyMapping(MutableMapping[str, str]): pass
self.assertNotIsInstance({}, MyMapping)
self.assertNotIsSubclass(dict, MyMapping)

def test_multiple_abc_bases(self):
class MM1(MutableMapping[str, str], collections_abc.MutableMapping):
def __getitem__(self, k):
return None
def __setitem__(self, k, v):
pass
def __delitem__(self, k):
pass
def __iter__(self):
return iter(())
def __len__(self):
return 0
class MM2(collections_abc.MutableMapping, MutableMapping[str, str]):
def __getitem__(self, k):
return None
def __setitem__(self, k, v):
pass
def __delitem__(self, k):
pass
def __iter__(self):
return iter(())
def __len__(self):
return 0
# these two should just work
MM1().update()
MM2().update()
self.assertIsInstance(MM1(), collections_abc.MutableMapping)
self.assertIsInstance(MM1(), MutableMapping)
self.assertIsInstance(MM2(), collections_abc.MutableMapping)
self.assertIsInstance(MM2(), MutableMapping)

def test_pickle(self):
global C # pickle wants to reference the class by name
T = TypeVar('T')
Expand Down Expand Up @@ -747,6 +813,8 @@ class C(type(ClassVar[int])):
pass

def test_cannot_init(self):
with self.assertRaises(TypeError):
ClassVar()
with self.assertRaises(TypeError):
type(ClassVar)()
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -1485,10 +1553,12 @@ def test_basics(self):
pat = re.compile('[a-z]+', re.I)
self.assertIsSubclass(pat.__class__, Pattern)
self.assertIsSubclass(type(pat), Pattern)
self.assertIsInstance(pat, Pattern)

mat = pat.search('12345abcde.....')
self.assertIsSubclass(mat.__class__, Match)
self.assertIsSubclass(type(mat), Match)
self.assertIsInstance(mat, Match)

# these should just work
p = Pattern[Union[str, bytes]]
Expand Down