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
Increased cache size to 50; start backporting changes to Python 2
  • Loading branch information
ilevkivskyi committed Sep 22, 2016
commit 4c91c3b86781cda840bb64a411e5b52c0db605a5
112 changes: 12 additions & 100 deletions python2/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,11 @@ def test_any_instance_type_error(self):
with self.assertRaises(TypeError):
isinstance(42, Any)

def test_any_subclass(self):
self.assertTrue(issubclass(Employee, Any))
self.assertTrue(issubclass(int, Any))
self.assertTrue(issubclass(type(None), Any))
self.assertTrue(issubclass(object, Any))

def test_others_any(self):
self.assertFalse(issubclass(Any, Employee))
self.assertFalse(issubclass(Any, int))
self.assertFalse(issubclass(Any, type(None)))
# However, Any is a subclass of object (this can't be helped).
self.assertTrue(issubclass(Any, object))
def test_any_subclass_type_error(self):
with self.assertRaises(TypeError):
issubclass(Employee, Any)
with self.assertRaises(TypeError):
issubclass(Any, Employee)

def test_repr(self):
self.assertEqual(repr(Any), 'typing.Any')
Expand All @@ -97,22 +90,6 @@ def test_cannot_subscript(self):
Any[int]

def test_any_is_subclass(self):
# Any should be considered a subclass of everything.
self.assertIsSubclass(Any, Any)
self.assertIsSubclass(Any, typing.List)
self.assertIsSubclass(Any, typing.List[int])
self.assertIsSubclass(Any, typing.List[T])
self.assertIsSubclass(Any, typing.Mapping)
self.assertIsSubclass(Any, typing.Mapping[str, int])
self.assertIsSubclass(Any, typing.Mapping[KT, VT])
self.assertIsSubclass(Any, Generic)
self.assertIsSubclass(Any, Generic[T])
self.assertIsSubclass(Any, Generic[KT, VT])
self.assertIsSubclass(Any, AnyStr)
self.assertIsSubclass(Any, Union)
self.assertIsSubclass(Any, Union[int, str])
self.assertIsSubclass(Any, typing.Match)
self.assertIsSubclass(Any, typing.Match[str])
# These expressions must simply not fail.
typing.Match[Any]
typing.Pattern[Any]
Expand All @@ -123,13 +100,8 @@ class TypeVarTests(BaseTestCase):

def test_basic_plain(self):
T = TypeVar('T')
# Every class is a subclass of T.
self.assertIsSubclass(int, T)
self.assertIsSubclass(str, T)
# T equals itself.
self.assertEqual(T, T)
# T is a subclass of itself.
self.assertIsSubclass(T, T)
# T is an instance of TypeVar
self.assertIsInstance(T, TypeVar)

Expand All @@ -138,16 +110,12 @@ def test_typevar_instance_type_error(self):
with self.assertRaises(TypeError):
isinstance(42, T)

def test_basic_constrained(self):
A = TypeVar('A', str, bytes)
# Only str and bytes are subclasses of A.
self.assertIsSubclass(str, A)
self.assertIsSubclass(bytes, A)
self.assertNotIsSubclass(int, A)
# A equals itself.
self.assertEqual(A, A)
# A is a subclass of itself.
self.assertIsSubclass(A, A)
def test_typevar_subclass_type_error(self):
T = TypeVar('T')
with self.assertRaises(TypeError):
issubclass(int, T)
with self.assertRaises(TypeError):
issubclass(T, int)

def test_constrained_error(self):
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -184,19 +152,6 @@ def test_no_redefinition(self):
self.assertNotEqual(TypeVar('T'), TypeVar('T'))
self.assertNotEqual(TypeVar('T', int, str), TypeVar('T', int, str))

def test_subclass_as_unions(self):
# None of these are true -- each type var is its own world.
self.assertFalse(issubclass(TypeVar('T', int, str),
TypeVar('T', int, str)))
self.assertFalse(issubclass(TypeVar('T', int, float),
TypeVar('T', int, float, str)))
self.assertFalse(issubclass(TypeVar('T', int, str),
TypeVar('T', str, int)))
A = TypeVar('A', int, str)
B = TypeVar('B', int, str, float)
self.assertFalse(issubclass(A, B))
self.assertFalse(issubclass(B, A))

def test_cannot_subclass_vars(self):
with self.assertRaises(TypeError):
class V(TypeVar('T')):
Expand All @@ -211,12 +166,6 @@ def test_cannot_instantiate_vars(self):
with self.assertRaises(TypeError):
TypeVar('A')()

def test_bound(self):
X = TypeVar('X', bound=Employee)
self.assertIsSubclass(Employee, X)
self.assertIsSubclass(Manager, X)
self.assertNotIsSubclass(int, X)

def test_bound_errors(self):
with self.assertRaises(TypeError):
TypeVar('X', bound=42)
Expand Down Expand Up @@ -354,32 +303,6 @@ def Elem(*args):
Union[Elem, str] # Nor should this


class TypeVarUnionTests(BaseTestCase):

def test_simpler(self):
A = TypeVar('A', int, str, float)
B = TypeVar('B', int, str)
self.assertIsSubclass(A, A)
self.assertIsSubclass(B, B)
self.assertNotIsSubclass(B, A)
self.assertIsSubclass(A, Union[int, str, float])
self.assertNotIsSubclass(Union[int, str, float], A)
self.assertNotIsSubclass(Union[int, str], B)
self.assertIsSubclass(B, Union[int, str])
self.assertNotIsSubclass(A, B)
self.assertNotIsSubclass(Union[int, str, float], B)
self.assertNotIsSubclass(A, Union[int, str])

def test_var_union_subclass(self):
self.assertTrue(issubclass(T, Union[int, T]))
self.assertTrue(issubclass(KT, Union[KT, VT]))

def test_var_union(self):
TU = TypeVar('TU', Union[int, float], None)
self.assertIsSubclass(int, TU)
self.assertIsSubclass(float, TU)


class TupleTests(BaseTestCase):

def test_basics(self):
Expand Down Expand Up @@ -1274,22 +1197,14 @@ def test_basics(self):
pat = re.compile('[a-z]+', re.I)
self.assertIsSubclass(pat.__class__, Pattern)
self.assertIsSubclass(type(pat), Pattern)
self.assertIsSubclass(type(pat), Pattern[str])

mat = pat.search('12345abcde.....')
self.assertIsSubclass(mat.__class__, Match)
self.assertIsSubclass(mat.__class__, Match[str])
self.assertIsSubclass(mat.__class__, Match[bytes]) # Sad but true.
self.assertIsSubclass(type(mat), Match)
self.assertIsSubclass(type(mat), Match[str])

# these should just work
p = Pattern[Union[str, bytes]]
self.assertIsSubclass(Pattern[str], Pattern)
self.assertIsSubclass(Pattern[str], p)

m = Match[Union[bytes, str]]
self.assertIsSubclass(Match[bytes], Match)
self.assertIsSubclass(Match[bytes], m)

def test_errors(self):
with self.assertRaises(TypeError):
Expand All @@ -1302,9 +1217,6 @@ def test_errors(self):
with self.assertRaises(TypeError):
# Too complicated?
m[str]
with self.assertRaises(TypeError):
# We don't support isinstance().
isinstance(42, Pattern)
with self.assertRaises(TypeError):
# We don't support isinstance().
isinstance(42, Pattern[str])
Expand Down
Loading