Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
75b1bdca
Commit
75b1bdca
authored
Apr 19, 2016
by
Zachary Ware
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replace assert statements with self.assertXxx() calls
Sync with upstream, see github.com/python/typing/pull/205
parent
def8072c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
304 additions
and
280 deletions
+304
-280
Lib/test/test_typing.py
Lib/test/test_typing.py
+304
-280
No files found.
Lib/test/test_typing.py
View file @
75b1bdca
...
...
@@ -20,6 +20,23 @@ from typing import Pattern, Match
import
typing
class
BaseTestCase
(
TestCase
):
def
assertIsSubclass
(
self
,
cls
,
class_or_tuple
,
msg
=
None
):
if
not
issubclass
(
cls
,
class_or_tuple
):
message
=
'%r is not a subclass of %r'
%
(
cls
,
class_or_tuple
)
if
msg
is
not
None
:
message
+=
' : %s'
%
msg
raise
self
.
failureException
(
message
)
def
assertNotIsSubclass
(
self
,
cls
,
class_or_tuple
,
msg
=
None
):
if
issubclass
(
cls
,
class_or_tuple
):
message
=
'%r is a subclass of %r'
%
(
cls
,
class_or_tuple
)
if
msg
is
not
None
:
message
+=
' : %s'
%
msg
raise
self
.
failureException
(
message
)
class
Employee
:
pass
...
...
@@ -36,7 +53,7 @@ class ManagingFounder(Manager, Founder):
pass
class
AnyTests
(
TestCase
):
class
AnyTests
(
Base
TestCase
):
def
test_any_instance_type_error
(
self
):
with
self
.
assertRaises
(
TypeError
):
...
...
@@ -79,40 +96,40 @@ class AnyTests(TestCase):
def
test_any_is_subclass
(
self
):
# Any should be considered a subclass of everything.
assert
iss
ubclass
(
Any
,
Any
)
assert
iss
ubclass
(
Any
,
typing
.
List
)
assert
iss
ubclass
(
Any
,
typing
.
List
[
int
])
assert
iss
ubclass
(
Any
,
typing
.
List
[
T
])
assert
iss
ubclass
(
Any
,
typing
.
Mapping
)
assert
iss
ubclass
(
Any
,
typing
.
Mapping
[
str
,
int
])
assert
iss
ubclass
(
Any
,
typing
.
Mapping
[
KT
,
VT
])
assert
iss
ubclass
(
Any
,
Generic
)
assert
iss
ubclass
(
Any
,
Generic
[
T
])
assert
iss
ubclass
(
Any
,
Generic
[
KT
,
VT
])
assert
iss
ubclass
(
Any
,
AnyStr
)
assert
iss
ubclass
(
Any
,
Union
)
assert
iss
ubclass
(
Any
,
Union
[
int
,
str
])
assert
iss
ubclass
(
Any
,
typing
.
Match
)
assert
iss
ubclass
(
Any
,
typing
.
Match
[
str
])
self
.
assertIsS
ubclass
(
Any
,
Any
)
self
.
assertIsS
ubclass
(
Any
,
typing
.
List
)
self
.
assertIsS
ubclass
(
Any
,
typing
.
List
[
int
])
self
.
assertIsS
ubclass
(
Any
,
typing
.
List
[
T
])
self
.
assertIsS
ubclass
(
Any
,
typing
.
Mapping
)
self
.
assertIsS
ubclass
(
Any
,
typing
.
Mapping
[
str
,
int
])
self
.
assertIsS
ubclass
(
Any
,
typing
.
Mapping
[
KT
,
VT
])
self
.
assertIsS
ubclass
(
Any
,
Generic
)
self
.
assertIsS
ubclass
(
Any
,
Generic
[
T
])
self
.
assertIsS
ubclass
(
Any
,
Generic
[
KT
,
VT
])
self
.
assertIsS
ubclass
(
Any
,
AnyStr
)
self
.
assertIsS
ubclass
(
Any
,
Union
)
self
.
assertIsS
ubclass
(
Any
,
Union
[
int
,
str
])
self
.
assertIsS
ubclass
(
Any
,
typing
.
Match
)
self
.
assertIsS
ubclass
(
Any
,
typing
.
Match
[
str
])
# These expressions must simply not fail.
typing
.
Match
[
Any
]
typing
.
Pattern
[
Any
]
typing
.
IO
[
Any
]
class
TypeVarTests
(
TestCase
):
class
TypeVarTests
(
Base
TestCase
):
def
test_basic_plain
(
self
):
T
=
TypeVar
(
'T'
)
# Every class is a subclass of T.
assert
iss
ubclass
(
int
,
T
)
assert
iss
ubclass
(
str
,
T
)
self
.
assertIsS
ubclass
(
int
,
T
)
self
.
assertIsS
ubclass
(
str
,
T
)
# T equals itself.
assert
T
==
T
self
.
assertEqual
(
T
,
T
)
# T is a subclass of itself.
assert
iss
ubclass
(
T
,
T
)
self
.
assertIsS
ubclass
(
T
,
T
)
# T is an instance of TypeVar
assert
isi
nstance
(
T
,
TypeVar
)
self
.
assertIsI
nstance
(
T
,
TypeVar
)
def
test_typevar_instance_type_error
(
self
):
T
=
TypeVar
(
'T'
)
...
...
@@ -122,13 +139,13 @@ class TypeVarTests(TestCase):
def
test_basic_constrained
(
self
):
A
=
TypeVar
(
'A'
,
str
,
bytes
)
# Only str and bytes are subclasses of A.
assert
iss
ubclass
(
str
,
A
)
assert
iss
ubclass
(
bytes
,
A
)
assert
not
iss
ubclass
(
int
,
A
)
self
.
assertIsS
ubclass
(
str
,
A
)
self
.
assertIsS
ubclass
(
bytes
,
A
)
self
.
assertNotIsS
ubclass
(
int
,
A
)
# A equals itself.
assert
A
==
A
self
.
assertEqual
(
A
,
A
)
# A is a subclass of itself.
assert
iss
ubclass
(
A
,
A
)
self
.
assertIsS
ubclass
(
A
,
A
)
def
test_constrained_error
(
self
):
with
self
.
assertRaises
(
TypeError
):
...
...
@@ -138,18 +155,18 @@ class TypeVarTests(TestCase):
def
test_union_unique
(
self
):
X
=
TypeVar
(
'X'
)
Y
=
TypeVar
(
'Y'
)
assert
X
!=
Y
assert
Union
[
X
]
==
X
assert
Union
[
X
]
!=
Union
[
X
,
Y
]
assert
Union
[
X
,
X
]
==
X
assert
Union
[
X
,
int
]
!=
Union
[
X
]
assert
Union
[
X
,
int
]
!=
Union
[
int
]
assert
Union
[
X
,
int
].
__union_params__
==
(
X
,
int
)
assert
Union
[
X
,
int
].
__union_set_params__
==
{
X
,
int
}
self
.
assertNotEqual
(
X
,
Y
)
self
.
assertEqual
(
Union
[
X
],
X
)
self
.
assertNotEqual
(
Union
[
X
],
Union
[
X
,
Y
])
self
.
assertEqual
(
Union
[
X
,
X
],
X
)
self
.
assertNotEqual
(
Union
[
X
,
int
],
Union
[
X
])
self
.
assertNotEqual
(
Union
[
X
,
int
],
Union
[
int
])
self
.
assertEqual
(
Union
[
X
,
int
].
__union_params__
,
(
X
,
int
)
)
self
.
assertEqual
(
Union
[
X
,
int
].
__union_set_params__
,
{
X
,
int
})
def
test_union_constrained
(
self
):
A
=
TypeVar
(
'A'
,
str
,
bytes
)
assert
Union
[
A
,
str
]
!=
Union
[
A
]
self
.
assertNotEqual
(
Union
[
A
,
str
],
Union
[
A
])
def
test_repr
(
self
):
self
.
assertEqual
(
repr
(
T
),
'~T'
)
...
...
@@ -194,9 +211,9 @@ class TypeVarTests(TestCase):
def
test_bound
(
self
):
X
=
TypeVar
(
'X'
,
bound
=
Employee
)
assert
iss
ubclass
(
Employee
,
X
)
assert
iss
ubclass
(
Manager
,
X
)
assert
not
iss
ubclass
(
int
,
X
)
self
.
assertIsS
ubclass
(
Employee
,
X
)
self
.
assertIsS
ubclass
(
Manager
,
X
)
self
.
assertNotIsS
ubclass
(
int
,
X
)
def
test_bound_errors
(
self
):
with
self
.
assertRaises
(
TypeError
):
...
...
@@ -205,7 +222,7 @@ class TypeVarTests(TestCase):
TypeVar
(
'X'
,
str
,
float
,
bound
=
Employee
)
class
UnionTests
(
TestCase
):
class
UnionTests
(
Base
TestCase
):
def
test_basics
(
self
):
u
=
Union
[
int
,
float
]
...
...
@@ -308,8 +325,8 @@ class UnionTests(TestCase):
Union
[()]
def
test_issubclass_union
(
self
):
assert
iss
ubclass
(
Union
[
int
,
str
],
Union
)
assert
not
iss
ubclass
(
int
,
Union
)
self
.
assertIsS
ubclass
(
Union
[
int
,
str
],
Union
)
self
.
assertNotIsS
ubclass
(
int
,
Union
)
def
test_union_instance_type_error
(
self
):
with
self
.
assertRaises
(
TypeError
):
...
...
@@ -321,21 +338,21 @@ class UnionTests(TestCase):
A
class
TypeVarUnionTests
(
TestCase
):
class
TypeVarUnionTests
(
Base
TestCase
):
def
test_simpler
(
self
):
A
=
TypeVar
(
'A'
,
int
,
str
,
float
)
B
=
TypeVar
(
'B'
,
int
,
str
)
assert
iss
ubclass
(
A
,
A
)
assert
iss
ubclass
(
B
,
B
)
assert
not
iss
ubclass
(
B
,
A
)
assert
iss
ubclass
(
A
,
Union
[
int
,
str
,
float
])
assert
not
iss
ubclass
(
Union
[
int
,
str
,
float
],
A
)
assert
not
iss
ubclass
(
Union
[
int
,
str
],
B
)
assert
iss
ubclass
(
B
,
Union
[
int
,
str
])
assert
not
iss
ubclass
(
A
,
B
)
assert
not
iss
ubclass
(
Union
[
int
,
str
,
float
],
B
)
assert
not
iss
ubclass
(
A
,
Union
[
int
,
str
])
self
.
assertIsS
ubclass
(
A
,
A
)
self
.
assertIsS
ubclass
(
B
,
B
)
self
.
assertNotIsS
ubclass
(
B
,
A
)
self
.
assertIsS
ubclass
(
A
,
Union
[
int
,
str
,
float
])
self
.
assertNotIsS
ubclass
(
Union
[
int
,
str
,
float
],
A
)
self
.
assertNotIsS
ubclass
(
Union
[
int
,
str
],
B
)
self
.
assertIsS
ubclass
(
B
,
Union
[
int
,
str
])
self
.
assertNotIsS
ubclass
(
A
,
B
)
self
.
assertNotIsS
ubclass
(
Union
[
int
,
str
,
float
],
B
)
self
.
assertNotIsS
ubclass
(
A
,
Union
[
int
,
str
])
def
test_var_union_subclass
(
self
):
self
.
assertTrue
(
issubclass
(
T
,
Union
[
int
,
T
]))
...
...
@@ -343,11 +360,11 @@ class TypeVarUnionTests(TestCase):
def
test_var_union
(
self
):
TU
=
TypeVar
(
'TU'
,
Union
[
int
,
float
],
None
)
assert
iss
ubclass
(
int
,
TU
)
assert
iss
ubclass
(
float
,
TU
)
self
.
assertIsS
ubclass
(
int
,
TU
)
self
.
assertIsS
ubclass
(
float
,
TU
)
class
TupleTests
(
TestCase
):
class
TupleTests
(
Base
TestCase
):
def
test_basics
(
self
):
self
.
assertTrue
(
issubclass
(
Tuple
[
int
,
str
],
Tuple
))
...
...
@@ -360,10 +377,10 @@ class TupleTests(TestCase):
self
.
assertFalse
(
issubclass
(
Tuple
,
tuple
))
# Can't have it both ways.
def
test_equality
(
self
):
assert
Tuple
[
int
]
==
Tuple
[
int
]
assert
Tuple
[
int
,
...]
==
Tuple
[
int
,
...]
assert
Tuple
[
int
]
!=
Tuple
[
int
,
int
]
assert
Tuple
[
int
]
!=
Tuple
[
int
,
...]
self
.
assertEqual
(
Tuple
[
int
],
Tuple
[
int
])
self
.
assertEqual
(
Tuple
[
int
,
...],
Tuple
[
int
,
...])
self
.
assertNotEqual
(
Tuple
[
int
],
Tuple
[
int
,
int
])
self
.
assertNotEqual
(
Tuple
[
int
],
Tuple
[
int
,
...])
def
test_tuple_subclass
(
self
):
class
MyTuple
(
tuple
):
...
...
@@ -384,10 +401,10 @@ class TupleTests(TestCase):
class
C
(
B
):
pass
assert
not
iss
ubclass
(
Tuple
[
B
],
Tuple
[
B
,
...])
assert
iss
ubclass
(
Tuple
[
C
,
...],
Tuple
[
B
,
...])
assert
not
iss
ubclass
(
Tuple
[
C
,
...],
Tuple
[
B
])
assert
not
iss
ubclass
(
Tuple
[
C
],
Tuple
[
B
,
...])
self
.
assertNotIsS
ubclass
(
Tuple
[
B
],
Tuple
[
B
,
...])
self
.
assertIsS
ubclass
(
Tuple
[
C
,
...],
Tuple
[
B
,
...])
self
.
assertNotIsS
ubclass
(
Tuple
[
C
,
...],
Tuple
[
B
])
self
.
assertNotIsS
ubclass
(
Tuple
[
C
],
Tuple
[
B
,
...])
def
test_repr
(
self
):
self
.
assertEqual
(
repr
(
Tuple
),
'typing.Tuple'
)
...
...
@@ -402,7 +419,7 @@ class TupleTests(TestCase):
issubclass
(
42
,
Tuple
[
int
])
class
CallableTests
(
TestCase
):
class
CallableTests
(
Base
TestCase
):
def
test_self_subclass
(
self
):
self
.
assertTrue
(
issubclass
(
Callable
[[
int
],
int
],
Callable
))
...
...
@@ -447,20 +464,20 @@ class CallableTests(TestCase):
def
test_callable_instance_works
(
self
):
def
f
():
pass
assert
isi
nstance
(
f
,
Callable
)
assert
not
isi
nstance
(
None
,
Callable
)
self
.
assertIsI
nstance
(
f
,
Callable
)
self
.
assertNotIsI
nstance
(
None
,
Callable
)
def
test_callable_instance_type_error
(
self
):
def
f
():
pass
with
self
.
assertRaises
(
TypeError
):
assert
isi
nstance
(
f
,
Callable
[[],
None
])
self
.
assertIsI
nstance
(
f
,
Callable
[[],
None
])
with
self
.
assertRaises
(
TypeError
):
assert
isi
nstance
(
f
,
Callable
[[],
Any
])
self
.
assertIsI
nstance
(
f
,
Callable
[[],
Any
])
with
self
.
assertRaises
(
TypeError
):
assert
not
isi
nstance
(
None
,
Callable
[[],
None
])
self
.
assertNotIsI
nstance
(
None
,
Callable
[[],
None
])
with
self
.
assertRaises
(
TypeError
):
assert
not
isi
nstance
(
None
,
Callable
[[],
Any
])
self
.
assertNotIsI
nstance
(
None
,
Callable
[[],
Any
])
def
test_repr
(
self
):
ct0
=
Callable
[[],
bool
]
...
...
@@ -513,15 +530,15 @@ class MySimpleMapping(SimpleMapping[XK, XV]):
return
default
class
ProtocolTests
(
TestCase
):
class
ProtocolTests
(
Base
TestCase
):
def
test_supports_int
(
self
):
assert
iss
ubclass
(
int
,
typing
.
SupportsInt
)
assert
not
iss
ubclass
(
str
,
typing
.
SupportsInt
)
self
.
assertIsS
ubclass
(
int
,
typing
.
SupportsInt
)
self
.
assertNotIsS
ubclass
(
str
,
typing
.
SupportsInt
)
def
test_supports_float
(
self
):
assert
iss
ubclass
(
float
,
typing
.
SupportsFloat
)
assert
not
iss
ubclass
(
str
,
typing
.
SupportsFloat
)
self
.
assertIsS
ubclass
(
float
,
typing
.
SupportsFloat
)
self
.
assertNotIsS
ubclass
(
str
,
typing
.
SupportsFloat
)
def
test_supports_complex
(
self
):
...
...
@@ -530,8 +547,8 @@ class ProtocolTests(TestCase):
def
__complex__
(
self
):
return
0j
assert
iss
ubclass
(
C
,
typing
.
SupportsComplex
)
assert
not
iss
ubclass
(
str
,
typing
.
SupportsComplex
)
self
.
assertIsS
ubclass
(
C
,
typing
.
SupportsComplex
)
self
.
assertNotIsS
ubclass
(
str
,
typing
.
SupportsComplex
)
def
test_supports_bytes
(
self
):
...
...
@@ -540,40 +557,40 @@ class ProtocolTests(TestCase):
def
__bytes__
(
self
):
return
b''
assert
iss
ubclass
(
B
,
typing
.
SupportsBytes
)
assert
not
iss
ubclass
(
str
,
typing
.
SupportsBytes
)
self
.
assertIsS
ubclass
(
B
,
typing
.
SupportsBytes
)
self
.
assertNotIsS
ubclass
(
str
,
typing
.
SupportsBytes
)
def
test_supports_abs
(
self
):
assert
iss
ubclass
(
float
,
typing
.
SupportsAbs
)
assert
iss
ubclass
(
int
,
typing
.
SupportsAbs
)
assert
not
iss
ubclass
(
str
,
typing
.
SupportsAbs
)
self
.
assertIsS
ubclass
(
float
,
typing
.
SupportsAbs
)
self
.
assertIsS
ubclass
(
int
,
typing
.
SupportsAbs
)
self
.
assertNotIsS
ubclass
(
str
,
typing
.
SupportsAbs
)
def
test_supports_round
(
self
):
issubclass
(
float
,
typing
.
SupportsRound
)
assert
iss
ubclass
(
float
,
typing
.
SupportsRound
)
assert
iss
ubclass
(
int
,
typing
.
SupportsRound
)
assert
not
iss
ubclass
(
str
,
typing
.
SupportsRound
)
self
.
assertIsS
ubclass
(
float
,
typing
.
SupportsRound
)
self
.
assertIsS
ubclass
(
int
,
typing
.
SupportsRound
)
self
.
assertNotIsS
ubclass
(
str
,
typing
.
SupportsRound
)
def
test_reversible
(
self
):
assert
iss
ubclass
(
list
,
typing
.
Reversible
)
assert
not
iss
ubclass
(
int
,
typing
.
Reversible
)
self
.
assertIsS
ubclass
(
list
,
typing
.
Reversible
)
self
.
assertNotIsS
ubclass
(
int
,
typing
.
Reversible
)
def
test_protocol_instance_type_error
(
self
):
with
self
.
assertRaises
(
TypeError
):
isinstance
(
0
,
typing
.
SupportsAbs
)
class
GenericTests
(
TestCase
):
class
GenericTests
(
Base
TestCase
):
def
test_basics
(
self
):
X
=
SimpleMapping
[
str
,
Any
]
assert
X
.
__parameters__
==
(
)
self
.
assertEqual
(
X
.
__parameters__
,
()
)
with
self
.
assertRaises
(
TypeError
):
X
[
str
]
with
self
.
assertRaises
(
TypeError
):
X
[
str
,
str
]
Y
=
SimpleMapping
[
XK
,
str
]
assert
Y
.
__parameters__
==
(
XK
,
)
self
.
assertEqual
(
Y
.
__parameters__
,
(
XK
,)
)
Y
[
str
]
with
self
.
assertRaises
(
TypeError
):
Y
[
str
,
str
]
...
...
@@ -600,21 +617,21 @@ class GenericTests(TestCase):
pass
X
=
C
[
Tuple
[
S
,
T
]]
assert
X
==
C
[
Tuple
[
S
,
T
]]
assert
X
!=
C
[
Tuple
[
T
,
S
]]
self
.
assertEqual
(
X
,
C
[
Tuple
[
S
,
T
]])
self
.
assertNotEqual
(
X
,
C
[
Tuple
[
T
,
S
]])
Y
=
X
[
T
,
int
]
assert
Y
==
X
[
T
,
int
]
assert
Y
!=
X
[
S
,
int
]
assert
Y
!=
X
[
T
,
str
]
self
.
assertEqual
(
Y
,
X
[
T
,
int
])
self
.
assertNotEqual
(
Y
,
X
[
S
,
int
])
self
.
assertNotEqual
(
Y
,
X
[
T
,
str
])
Z
=
Y
[
str
]
assert
Z
==
Y
[
str
]
assert
Z
!=
Y
[
int
]
assert
Z
!=
Y
[
T
]
self
.
assertEqual
(
Z
,
Y
[
str
])
self
.
assertNotEqual
(
Z
,
Y
[
int
])
self
.
assertNotEqual
(
Z
,
Y
[
T
])
assert
str
(
Z
).
endswith
(
'.C<~T>[typing.Tuple[~S, ~T]]<~S, ~T>[~T, int]<~T>[str]'
)
self
.
assertTrue
(
str
(
Z
).
endswith
(
'.C<~T>[typing.Tuple[~S, ~T]]<~S, ~T>[~T, int]<~T>[str]'
)
)
def
test_dict
(
self
):
T
=
TypeVar
(
'T'
)
...
...
@@ -666,28 +683,30 @@ class GenericTests(TestCase):
class
C
(
Generic
[
T
]):
pass
assert
C
.
__module__
==
__name__
self
.
assertEqual
(
C
.
__module__
,
__name__
)
if
not
PY32
:
assert
C
.
__qualname__
==
'GenericTests.test_repr_2.<locals>.C'
assert
repr
(
C
).
split
(
'.'
)[
-
1
]
==
'C<~T>'
self
.
assertEqual
(
C
.
__qualname__
,
'GenericTests.test_repr_2.<locals>.C'
)
self
.
assertEqual
(
repr
(
C
).
split
(
'.'
)[
-
1
],
'C<~T>'
)
X
=
C
[
int
]
assert
X
.
__module__
==
__name__
self
.
assertEqual
(
X
.
__module__
,
__name__
)
if
not
PY32
:
assert
X
.
__qualname__
==
'C'
assert
repr
(
X
).
split
(
'.'
)[
-
1
]
==
'C<~T>[int]'
self
.
assertEqual
(
X
.
__qualname__
,
'C'
)
self
.
assertEqual
(
repr
(
X
).
split
(
'.'
)[
-
1
],
'C<~T>[int]'
)
class
Y
(
C
[
int
]):
pass
assert
Y
.
__module__
==
__name__
self
.
assertEqual
(
Y
.
__module__
,
__name__
)
if
not
PY32
:
assert
Y
.
__qualname__
==
'GenericTests.test_repr_2.<locals>.Y'
assert
repr
(
Y
).
split
(
'.'
)[
-
1
]
==
'Y'
self
.
assertEqual
(
Y
.
__qualname__
,
'GenericTests.test_repr_2.<locals>.Y'
)
self
.
assertEqual
(
repr
(
Y
).
split
(
'.'
)[
-
1
],
'Y'
)
def
test_eq_1
(
self
):
assert
Generic
==
Generic
assert
Generic
[
T
]
==
Generic
[
T
]
assert
Generic
[
KT
]
!=
Generic
[
VT
]
self
.
assertEqual
(
Generic
,
Generic
)
self
.
assertEqual
(
Generic
[
T
],
Generic
[
T
])
self
.
assertNotEqual
(
Generic
[
KT
],
Generic
[
VT
])
def
test_eq_2
(
self
):
...
...
@@ -697,10 +716,10 @@ class GenericTests(TestCase):
class
B
(
Generic
[
T
]):
pass
assert
A
==
A
assert
A
!=
B
assert
A
[
T
]
==
A
[
T
]
assert
A
[
T
]
!=
B
[
T
]
self
.
assertEqual
(
A
,
A
)
self
.
assertNotEqual
(
A
,
B
)
self
.
assertEqual
(
A
[
T
],
A
[
T
])
self
.
assertNotEqual
(
A
[
T
],
B
[
T
])
def
test_multiple_inheritance
(
self
):
...
...
@@ -713,7 +732,7 @@ class GenericTests(TestCase):
class
C
(
A
[
T
,
VT
],
Generic
[
VT
,
T
,
KT
],
B
[
KT
,
T
]):
pass
assert
C
.
__parameters__
==
(
VT
,
T
,
KT
)
self
.
assertEqual
(
C
.
__parameters__
,
(
VT
,
T
,
KT
)
)
def
test_nested
(
self
):
...
...
@@ -743,7 +762,7 @@ class GenericTests(TestCase):
a
.
set
([])
a
.
append
(
1
)
a
.
append
(
42
)
assert
a
.
get
()
==
[
1
,
42
]
self
.
assertEqual
(
a
.
get
(),
[
1
,
42
])
def
test_type_erasure
(
self
):
T
=
TypeVar
(
'T'
)
...
...
@@ -760,12 +779,12 @@ class GenericTests(TestCase):
a
=
Node
(
x
)
b
=
Node
[
T
](
x
)
c
=
Node
[
Any
](
x
)
assert
type
(
a
)
is
Node
assert
type
(
b
)
is
Node
assert
type
(
c
)
is
Node
assert
a
.
label
==
x
assert
b
.
label
==
x
assert
c
.
label
==
x
self
.
assertIs
(
type
(
a
),
Node
)
self
.
assertIs
(
type
(
b
),
Node
)
self
.
assertIs
(
type
(
c
),
Node
)
self
.
assertEqual
(
a
.
label
,
x
)
self
.
assertEqual
(
b
.
label
,
x
)
self
.
assertEqual
(
c
.
label
,
x
)
foo
(
42
)
...
...
@@ -778,7 +797,7 @@ class GenericTests(TestCase):
class
D
(
C
):
pass
assert
D
.
__parameters__
==
(
)
self
.
assertEqual
(
D
.
__parameters__
,
()
)
with
self
.
assertRaises
(
Exception
):
D
[
int
]
...
...
@@ -788,61 +807,63 @@ class GenericTests(TestCase):
D
[
T
]
class
VarianceTests
(
TestCase
):
class
VarianceTests
(
Base
TestCase
):
def
test_invariance
(
self
):
# Because of invariance, List[subclass of X] is not a subclass
# of List[X], and ditto for MutableSequence.
assert
not
iss
ubclass
(
typing
.
List
[
Manager
],
typing
.
List
[
Employee
])
assert
not
iss
ubclass
(
typing
.
MutableSequence
[
Manager
],
self
.
assertNotIsS
ubclass
(
typing
.
List
[
Manager
],
typing
.
List
[
Employee
])
self
.
assertNotIsS
ubclass
(
typing
.
MutableSequence
[
Manager
],
typing
.
MutableSequence
[
Employee
])
# It's still reflexive.
assert
iss
ubclass
(
typing
.
List
[
Employee
],
typing
.
List
[
Employee
])
assert
iss
ubclass
(
typing
.
MutableSequence
[
Employee
],
self
.
assertIsS
ubclass
(
typing
.
List
[
Employee
],
typing
.
List
[
Employee
])
self
.
assertIsS
ubclass
(
typing
.
MutableSequence
[
Employee
],
typing
.
MutableSequence
[
Employee
])
def
test_covariance_tuple
(
self
):
# Check covariace for Tuple (which are really special cases).
assert
iss
ubclass
(
Tuple
[
Manager
],
Tuple
[
Employee
])
assert
not
iss
ubclass
(
Tuple
[
Employee
],
Tuple
[
Manager
])
self
.
assertIsS
ubclass
(
Tuple
[
Manager
],
Tuple
[
Employee
])
self
.
assertNotIsS
ubclass
(
Tuple
[
Employee
],
Tuple
[
Manager
])
# And pairwise.
assert
issubclass
(
Tuple
[
Manager
,
Manager
],
Tuple
[
Employee
,
Employee
])
assert
not
issubclass
(
Tuple
[
Employee
,
Employee
],
self
.
assertIsSubclass
(
Tuple
[
Manager
,
Manager
],
Tuple
[
Employee
,
Employee
])
self
.
assertNotIsSubclass
(
Tuple
[
Employee
,
Employee
],
Tuple
[
Manager
,
Employee
])
# And using ellipsis.
assert
iss
ubclass
(
Tuple
[
Manager
,
...],
Tuple
[
Employee
,
...])
assert
not
iss
ubclass
(
Tuple
[
Employee
,
...],
Tuple
[
Manager
,
...])
self
.
assertIsS
ubclass
(
Tuple
[
Manager
,
...],
Tuple
[
Employee
,
...])
self
.
assertNotIsS
ubclass
(
Tuple
[
Employee
,
...],
Tuple
[
Manager
,
...])
def
test_covariance_sequence
(
self
):
# Check covariance for Sequence (which is just a generic class
# for this purpose, but using a covariant type variable).
assert
issubclass
(
typing
.
Sequence
[
Manager
],
typing
.
Sequence
[
Employee
])
assert
not
issubclass
(
typing
.
Sequence
[
Employee
],
self
.
assertIsSubclass
(
typing
.
Sequence
[
Manager
],
typing
.
Sequence
[
Employee
])
self
.
assertNotIsSubclass
(
typing
.
Sequence
[
Employee
],
typing
.
Sequence
[
Manager
])
def
test_covariance_mapping
(
self
):
# Ditto for Mapping (covariant in the value, invariant in the key).
assert
iss
ubclass
(
typing
.
Mapping
[
Employee
,
Manager
],
self
.
assertIsS
ubclass
(
typing
.
Mapping
[
Employee
,
Manager
],
typing
.
Mapping
[
Employee
,
Employee
])
assert
not
iss
ubclass
(
typing
.
Mapping
[
Manager
,
Employee
],
self
.
assertNotIsS
ubclass
(
typing
.
Mapping
[
Manager
,
Employee
],
typing
.
Mapping
[
Employee
,
Employee
])
assert
not
iss
ubclass
(
typing
.
Mapping
[
Employee
,
Manager
],
self
.
assertNotIsS
ubclass
(
typing
.
Mapping
[
Employee
,
Manager
],
typing
.
Mapping
[
Manager
,
Manager
])
assert
not
iss
ubclass
(
typing
.
Mapping
[
Manager
,
Employee
],
self
.
assertNotIsS
ubclass
(
typing
.
Mapping
[
Manager
,
Employee
],
typing
.
Mapping
[
Manager
,
Manager
])
class
CastTests
(
TestCase
):
class
CastTests
(
Base
TestCase
):
def
test_basics
(
self
):
assert
cast
(
int
,
42
)
==
42
assert
cast
(
float
,
42
)
==
42
assert
type
(
cast
(
float
,
42
))
is
int
assert
cast
(
Any
,
42
)
==
42
assert
cast
(
list
,
42
)
==
42
assert
cast
(
Union
[
str
,
float
],
42
)
==
42
assert
cast
(
AnyStr
,
42
)
==
42
assert
cast
(
None
,
42
)
==
42
self
.
assertEqual
(
cast
(
int
,
42
),
42
)
self
.
assertEqual
(
cast
(
float
,
42
),
42
)
self
.
assertIs
(
type
(
cast
(
float
,
42
)),
int
)
self
.
assertEqual
(
cast
(
Any
,
42
),
42
)
self
.
assertEqual
(
cast
(
list
,
42
),
42
)
self
.
assertEqual
(
cast
(
Union
[
str
,
float
],
42
),
42
)
self
.
assertEqual
(
cast
(
AnyStr
,
42
),
42
)
self
.
assertEqual
(
cast
(
None
,
42
),
42
)
def
test_errors
(
self
):
# Bogus calls are not expected to fail.
...
...
@@ -850,7 +871,7 @@ class CastTests(TestCase):
cast
(
'hello'
,
42
)
class
ForwardRefTests
(
TestCase
):
class
ForwardRefTests
(
Base
TestCase
):
def
test_basics
(
self
):
...
...
@@ -876,15 +897,17 @@ class ForwardRefTests(TestCase):
t
=
Node
[
int
]
both_hints
=
get_type_hints
(
t
.
add_both
,
globals
(),
locals
())
assert
both_hints
[
'left'
]
==
both_hints
[
'right'
]
==
Optional
[
Node
[
T
]]
assert
both_hints
[
'stuff'
]
==
Optional
[
int
]
assert
'blah'
not
in
both_hints
self
.
assertEqual
(
both_hints
[
'left'
],
Optional
[
Node
[
T
]])
self
.
assertEqual
(
both_hints
[
'right'
],
Optional
[
Node
[
T
]])
self
.
assertEqual
(
both_hints
[
'left'
],
both_hints
[
'right'
])
self
.
assertEqual
(
both_hints
[
'stuff'
],
Optional
[
int
])
self
.
assertNotIn
(
'blah'
,
both_hints
)
left_hints
=
get_type_hints
(
t
.
add_left
,
globals
(),
locals
())
assert
left_hints
[
'node'
]
==
Optional
[
Node
[
T
]]
self
.
assertEqual
(
left_hints
[
'node'
],
Optional
[
Node
[
T
]])
right_hints
=
get_type_hints
(
t
.
add_right
,
globals
(),
locals
())
assert
right_hints
[
'node'
]
==
Optional
[
Node
[
T
]]
self
.
assertEqual
(
right_hints
[
'node'
],
Optional
[
Node
[
T
]])
def
test_forwardref_instance_type_error
(
self
):
fr
=
typing
.
_ForwardRef
(
'int'
)
...
...
@@ -1007,10 +1030,10 @@ class ForwardRefTests(TestCase):
ns
=
{}
exec
(
code
,
ns
)
hints
=
get_type_hints
(
ns
[
'C'
].
foo
)
assert
hints
==
{
'a'
:
ns
[
'C'
],
'return'
:
ns
[
'D'
]}
self
.
assertEqual
(
hints
,
{
'a'
:
ns
[
'C'
],
'return'
:
ns
[
'D'
]})
class
OverloadTests
(
TestCase
):
class
OverloadTests
(
Base
TestCase
):
def
test_overload_exists
(
self
):
from
typing
import
overload
...
...
@@ -1076,29 +1099,29 @@ if PY35:
exec
(
PY35_TESTS
)
class
CollectionsAbcTests
(
TestCase
):
class
CollectionsAbcTests
(
Base
TestCase
):
def
test_hashable
(
self
):
assert
isi
nstance
(
42
,
typing
.
Hashable
)
assert
not
isi
nstance
([],
typing
.
Hashable
)
self
.
assertIsI
nstance
(
42
,
typing
.
Hashable
)
self
.
assertNotIsI
nstance
([],
typing
.
Hashable
)
def
test_iterable
(
self
):
assert
isi
nstance
([],
typing
.
Iterable
)
self
.
assertIsI
nstance
([],
typing
.
Iterable
)
# Due to ABC caching, the second time takes a separate code
# path and could fail. So call this a few times.
assert
isi
nstance
([],
typing
.
Iterable
)
assert
isi
nstance
([],
typing
.
Iterable
)
assert
isi
nstance
([],
typing
.
Iterable
[
int
])
assert
not
isi
nstance
(
42
,
typing
.
Iterable
)
self
.
assertIsI
nstance
([],
typing
.
Iterable
)
self
.
assertIsI
nstance
([],
typing
.
Iterable
)
self
.
assertIsI
nstance
([],
typing
.
Iterable
[
int
])
self
.
assertNotIsI
nstance
(
42
,
typing
.
Iterable
)
# Just in case, also test issubclass() a few times.
assert
iss
ubclass
(
list
,
typing
.
Iterable
)
assert
iss
ubclass
(
list
,
typing
.
Iterable
)
self
.
assertIsS
ubclass
(
list
,
typing
.
Iterable
)
self
.
assertIsS
ubclass
(
list
,
typing
.
Iterable
)
def
test_iterator
(
self
):
it
=
iter
([])
assert
isi
nstance
(
it
,
typing
.
Iterator
)
assert
isi
nstance
(
it
,
typing
.
Iterator
[
int
])
assert
not
isi
nstance
(
42
,
typing
.
Iterator
)
self
.
assertIsI
nstance
(
it
,
typing
.
Iterator
)
self
.
assertIsI
nstance
(
it
,
typing
.
Iterator
[
int
])
self
.
assertNotIsI
nstance
(
42
,
typing
.
Iterator
)
@
skipUnless
(
PY35
,
'Python 3.5 required'
)
def
test_awaitable
(
self
):
...
...
@@ -1109,12 +1132,12 @@ class CollectionsAbcTests(TestCase):
globals
(),
ns
)
foo
=
ns
[
'foo'
]
g
=
foo
()
assert
iss
ubclass
(
type
(
g
),
typing
.
Awaitable
[
int
])
assert
isi
nstance
(
g
,
typing
.
Awaitable
)
assert
not
isi
nstance
(
foo
,
typing
.
Awaitable
)
assert
iss
ubclass
(
typing
.
Awaitable
[
Manager
],
self
.
assertIsS
ubclass
(
type
(
g
),
typing
.
Awaitable
[
int
])
self
.
assertIsI
nstance
(
g
,
typing
.
Awaitable
)
self
.
assertNotIsI
nstance
(
foo
,
typing
.
Awaitable
)
self
.
assertIsS
ubclass
(
typing
.
Awaitable
[
Manager
],
typing
.
Awaitable
[
Employee
])
assert
not
iss
ubclass
(
typing
.
Awaitable
[
Employee
],
self
.
assertNotIsS
ubclass
(
typing
.
Awaitable
[
Employee
],
typing
.
Awaitable
[
Manager
])
g
.
send
(
None
)
# Run foo() till completion, to avoid warning.
...
...
@@ -1122,70 +1145,70 @@ class CollectionsAbcTests(TestCase):
def
test_async_iterable
(
self
):
base_it
=
range
(
10
)
# type: Iterator[int]
it
=
AsyncIteratorWrapper
(
base_it
)
assert
isi
nstance
(
it
,
typing
.
AsyncIterable
)
assert
isi
nstance
(
it
,
typing
.
AsyncIterable
)
assert
iss
ubclass
(
typing
.
AsyncIterable
[
Manager
],
self
.
assertIsI
nstance
(
it
,
typing
.
AsyncIterable
)
self
.
assertIsI
nstance
(
it
,
typing
.
AsyncIterable
)
self
.
assertIsS
ubclass
(
typing
.
AsyncIterable
[
Manager
],
typing
.
AsyncIterable
[
Employee
])
assert
not
isi
nstance
(
42
,
typing
.
AsyncIterable
)
self
.
assertNotIsI
nstance
(
42
,
typing
.
AsyncIterable
)
@
skipUnless
(
PY35
,
'Python 3.5 required'
)
def
test_async_iterator
(
self
):
base_it
=
range
(
10
)
# type: Iterator[int]
it
=
AsyncIteratorWrapper
(
base_it
)
assert
isi
nstance
(
it
,
typing
.
AsyncIterator
)
assert
iss
ubclass
(
typing
.
AsyncIterator
[
Manager
],
self
.
assertIsI
nstance
(
it
,
typing
.
AsyncIterator
)
self
.
assertIsS
ubclass
(
typing
.
AsyncIterator
[
Manager
],
typing
.
AsyncIterator
[
Employee
])
assert
not
isi
nstance
(
42
,
typing
.
AsyncIterator
)
self
.
assertNotIsI
nstance
(
42
,
typing
.
AsyncIterator
)
def
test_sized
(
self
):
assert
isi
nstance
([],
typing
.
Sized
)
assert
not
isi
nstance
(
42
,
typing
.
Sized
)
self
.
assertIsI
nstance
([],
typing
.
Sized
)
self
.
assertNotIsI
nstance
(
42
,
typing
.
Sized
)
def
test_container
(
self
):
assert
isi
nstance
([],
typing
.
Container
)
assert
not
isi
nstance
(
42
,
typing
.
Container
)
self
.
assertIsI
nstance
([],
typing
.
Container
)
self
.
assertNotIsI
nstance
(
42
,
typing
.
Container
)
def
test_abstractset
(
self
):
assert
isi
nstance
(
set
(),
typing
.
AbstractSet
)
assert
not
isi
nstance
(
42
,
typing
.
AbstractSet
)
self
.
assertIsI
nstance
(
set
(),
typing
.
AbstractSet
)
self
.
assertNotIsI
nstance
(
42
,
typing
.
AbstractSet
)
def
test_mutableset
(
self
):
assert
isi
nstance
(
set
(),
typing
.
MutableSet
)
assert
not
isi
nstance
(
frozenset
(),
typing
.
MutableSet
)
self
.
assertIsI
nstance
(
set
(),
typing
.
MutableSet
)
self
.
assertNotIsI
nstance
(
frozenset
(),
typing
.
MutableSet
)
def
test_mapping
(
self
):
assert
isi
nstance
({},
typing
.
Mapping
)
assert
not
isi
nstance
(
42
,
typing
.
Mapping
)
self
.
assertIsI
nstance
({},
typing
.
Mapping
)
self
.
assertNotIsI
nstance
(
42
,
typing
.
Mapping
)
def
test_mutablemapping
(
self
):
assert
isi
nstance
({},
typing
.
MutableMapping
)
assert
not
isi
nstance
(
42
,
typing
.
MutableMapping
)
self
.
assertIsI
nstance
({},
typing
.
MutableMapping
)
self
.
assertNotIsI
nstance
(
42
,
typing
.
MutableMapping
)
def
test_sequence
(
self
):
assert
isi
nstance
([],
typing
.
Sequence
)
assert
not
isi
nstance
(
42
,
typing
.
Sequence
)
self
.
assertIsI
nstance
([],
typing
.
Sequence
)
self
.
assertNotIsI
nstance
(
42
,
typing
.
Sequence
)
def
test_mutablesequence
(
self
):
assert
isi
nstance
([],
typing
.
MutableSequence
)
assert
not
isi
nstance
((),
typing
.
MutableSequence
)
self
.
assertIsI
nstance
([],
typing
.
MutableSequence
)
self
.
assertNotIsI
nstance
((),
typing
.
MutableSequence
)
def
test_bytestring
(
self
):
assert
isi
nstance
(
b''
,
typing
.
ByteString
)
assert
isi
nstance
(
bytearray
(
b''
),
typing
.
ByteString
)
self
.
assertIsI
nstance
(
b''
,
typing
.
ByteString
)
self
.
assertIsI
nstance
(
bytearray
(
b''
),
typing
.
ByteString
)
def
test_list
(
self
):
assert
iss
ubclass
(
list
,
typing
.
List
)
self
.
assertIsS
ubclass
(
list
,
typing
.
List
)
def
test_set
(
self
):
assert
iss
ubclass
(
set
,
typing
.
Set
)
assert
not
iss
ubclass
(
frozenset
,
typing
.
Set
)
self
.
assertIsS
ubclass
(
set
,
typing
.
Set
)
self
.
assertNotIsS
ubclass
(
frozenset
,
typing
.
Set
)
def
test_frozenset
(
self
):
assert
iss
ubclass
(
frozenset
,
typing
.
FrozenSet
)
assert
not
iss
ubclass
(
set
,
typing
.
FrozenSet
)
self
.
assertIsS
ubclass
(
frozenset
,
typing
.
FrozenSet
)
self
.
assertNotIsS
ubclass
(
set
,
typing
.
FrozenSet
)
def
test_dict
(
self
):
assert
iss
ubclass
(
dict
,
typing
.
Dict
)
self
.
assertIsS
ubclass
(
dict
,
typing
.
Dict
)
def
test_no_list_instantiation
(
self
):
with
self
.
assertRaises
(
TypeError
):
...
...
@@ -1201,7 +1224,7 @@ class CollectionsAbcTests(TestCase):
pass
a
=
MyList
()
assert
isi
nstance
(
a
,
MyList
)
self
.
assertIsI
nstance
(
a
,
MyList
)
def
test_no_dict_instantiation
(
self
):
with
self
.
assertRaises
(
TypeError
):
...
...
@@ -1217,7 +1240,7 @@ class CollectionsAbcTests(TestCase):
pass
d
=
MyDict
()
assert
isi
nstance
(
d
,
MyDict
)
self
.
assertIsI
nstance
(
d
,
MyDict
)
def
test_no_defaultdict_instantiation
(
self
):
with
self
.
assertRaises
(
TypeError
):
...
...
@@ -1233,7 +1256,7 @@ class CollectionsAbcTests(TestCase):
pass
dd
=
MyDefDict
()
assert
isi
nstance
(
dd
,
MyDefDict
)
self
.
assertIsI
nstance
(
dd
,
MyDefDict
)
def
test_no_set_instantiation
(
self
):
with
self
.
assertRaises
(
TypeError
):
...
...
@@ -1249,7 +1272,7 @@ class CollectionsAbcTests(TestCase):
pass
d
=
MySet
()
assert
isi
nstance
(
d
,
MySet
)
self
.
assertIsI
nstance
(
d
,
MySet
)
def
test_no_frozenset_instantiation
(
self
):
with
self
.
assertRaises
(
TypeError
):
...
...
@@ -1265,7 +1288,7 @@ class CollectionsAbcTests(TestCase):
pass
d
=
MyFrozenSet
()
assert
isi
nstance
(
d
,
MyFrozenSet
)
self
.
assertIsI
nstance
(
d
,
MyFrozenSet
)
def
test_no_tuple_instantiation
(
self
):
with
self
.
assertRaises
(
TypeError
):
...
...
@@ -1279,10 +1302,10 @@ class CollectionsAbcTests(TestCase):
def
foo
():
yield
42
g
=
foo
()
assert
iss
ubclass
(
type
(
g
),
typing
.
Generator
)
assert
iss
ubclass
(
typing
.
Generator
[
Manager
,
Employee
,
Manager
],
self
.
assertIsS
ubclass
(
type
(
g
),
typing
.
Generator
)
self
.
assertIsS
ubclass
(
typing
.
Generator
[
Manager
,
Employee
,
Manager
],
typing
.
Generator
[
Employee
,
Manager
,
Employee
])
assert
not
iss
ubclass
(
typing
.
Generator
[
Manager
,
Manager
,
Manager
],
self
.
assertNotIsS
ubclass
(
typing
.
Generator
[
Manager
,
Manager
,
Manager
],
typing
.
Generator
[
Employee
,
Employee
,
Employee
])
def
test_no_generator_instantiation
(
self
):
...
...
@@ -1305,18 +1328,18 @@ class CollectionsAbcTests(TestCase):
def
__len__
(
self
):
return
0
assert
len
(
MMC
())
==
0
self
.
assertEqual
(
len
(
MMC
()),
0
)
class
MMB
(
typing
.
MutableMapping
[
KT
,
VT
]):
def
__len__
(
self
):
return
0
assert
len
(
MMB
())
==
0
assert
len
(
MMB
[
str
,
str
]())
==
0
assert
len
(
MMB
[
KT
,
VT
]())
==
0
self
.
assertEqual
(
len
(
MMB
()),
0
)
self
.
assertEqual
(
len
(
MMB
[
str
,
str
]()),
0
)
self
.
assertEqual
(
len
(
MMB
[
KT
,
VT
]()),
0
)
class
OtherABCTests
(
TestCase
):
class
OtherABCTests
(
Base
TestCase
):
@
skipUnless
(
hasattr
(
typing
,
'ContextManager'
),
'requires typing.ContextManager'
)
...
...
@@ -1326,27 +1349,27 @@ class OtherABCTests(TestCase):
yield
42
cm
=
manager
()
assert
isi
nstance
(
cm
,
typing
.
ContextManager
)
assert
isi
nstance
(
cm
,
typing
.
ContextManager
[
int
])
assert
not
isi
nstance
(
42
,
typing
.
ContextManager
)
self
.
assertIsI
nstance
(
cm
,
typing
.
ContextManager
)
self
.
assertIsI
nstance
(
cm
,
typing
.
ContextManager
[
int
])
self
.
assertNotIsI
nstance
(
42
,
typing
.
ContextManager
)
class
NamedTupleTests
(
TestCase
):
class
NamedTupleTests
(
Base
TestCase
):
def
test_basics
(
self
):
Emp
=
NamedTuple
(
'Emp'
,
[(
'name'
,
str
),
(
'id'
,
int
)])
assert
iss
ubclass
(
Emp
,
tuple
)
self
.
assertIsS
ubclass
(
Emp
,
tuple
)
joe
=
Emp
(
'Joe'
,
42
)
jim
=
Emp
(
name
=
'Jim'
,
id
=
1
)
assert
isi
nstance
(
joe
,
Emp
)
assert
isi
nstance
(
joe
,
tuple
)
assert
joe
.
name
==
'Joe'
assert
joe
.
id
==
42
assert
jim
.
name
==
'Jim'
assert
jim
.
id
==
1
assert
Emp
.
__name__
==
'Emp'
assert
Emp
.
_fields
==
(
'name'
,
'id'
)
assert
Emp
.
_field_types
==
dict
(
name
=
str
,
id
=
int
)
self
.
assertIsI
nstance
(
joe
,
Emp
)
self
.
assertIsI
nstance
(
joe
,
tuple
)
self
.
assertEqual
(
joe
.
name
,
'Joe'
)
self
.
assertEqual
(
joe
.
id
,
42
)
self
.
assertEqual
(
jim
.
name
,
'Jim'
)
self
.
assertEqual
(
jim
.
id
,
1
)
self
.
assertEqual
(
Emp
.
__name__
,
'Emp'
)
self
.
assertEqual
(
Emp
.
_fields
,
(
'name'
,
'id'
)
)
self
.
assertEqual
(
Emp
.
_field_types
,
dict
(
name
=
str
,
id
=
int
)
)
def
test_pickle
(
self
):
global
Emp
# pickle wants to reference the class by name
...
...
@@ -1358,7 +1381,7 @@ class NamedTupleTests(TestCase):
self
.
assertEqual
(
jane2
,
jane
)
class
IOTests
(
TestCase
):
class
IOTests
(
Base
TestCase
):
def
test_io
(
self
):
...
...
@@ -1366,7 +1389,7 @@ class IOTests(TestCase):
return
a
.
readline
()
a
=
stuff
.
__annotations__
[
'a'
]
assert
a
.
__parameters__
==
(
AnyStr
,
)
self
.
assertEqual
(
a
.
__parameters__
,
(
AnyStr
,)
)
def
test_textio
(
self
):
...
...
@@ -1374,7 +1397,7 @@ class IOTests(TestCase):
return
a
.
readline
()
a
=
stuff
.
__annotations__
[
'a'
]
assert
a
.
__parameters__
==
(
)
self
.
assertEqual
(
a
.
__parameters__
,
()
)
def
test_binaryio
(
self
):
...
...
@@ -1382,40 +1405,40 @@ class IOTests(TestCase):
return
a
.
readline
()
a
=
stuff
.
__annotations__
[
'a'
]
assert
a
.
__parameters__
==
(
)
self
.
assertEqual
(
a
.
__parameters__
,
()
)
def
test_io_submodule
(
self
):
from
typing.io
import
IO
,
TextIO
,
BinaryIO
,
__all__
,
__name__
assert
IO
is
typing
.
IO
assert
TextIO
is
typing
.
TextIO
assert
BinaryIO
is
typing
.
BinaryIO
assert
set
(
__all__
)
==
set
([
'IO'
,
'TextIO'
,
'BinaryIO'
]
)
assert
__name__
==
'typing.io'
self
.
assertIs
(
IO
,
typing
.
IO
)
self
.
assertIs
(
TextIO
,
typing
.
TextIO
)
self
.
assertIs
(
BinaryIO
,
typing
.
BinaryIO
)
self
.
assertEqual
(
set
(
__all__
),
set
([
'IO'
,
'TextIO'
,
'BinaryIO'
])
)
self
.
assertEqual
(
__name__
,
'typing.io'
)
class
RETests
(
TestCase
):
class
RETests
(
Base
TestCase
):
# Much of this is really testing _TypeAlias.
def
test_basics
(
self
):
pat
=
re
.
compile
(
'[a-z]+'
,
re
.
I
)
assert
iss
ubclass
(
pat
.
__class__
,
Pattern
)
assert
iss
ubclass
(
type
(
pat
),
Pattern
)
assert
iss
ubclass
(
type
(
pat
),
Pattern
[
str
])
self
.
assertIsS
ubclass
(
pat
.
__class__
,
Pattern
)
self
.
assertIsS
ubclass
(
type
(
pat
),
Pattern
)
self
.
assertIsS
ubclass
(
type
(
pat
),
Pattern
[
str
])
mat
=
pat
.
search
(
'12345abcde.....'
)
assert
iss
ubclass
(
mat
.
__class__
,
Match
)
assert
iss
ubclass
(
mat
.
__class__
,
Match
[
str
])
assert
iss
ubclass
(
mat
.
__class__
,
Match
[
bytes
])
# Sad but true.
assert
iss
ubclass
(
type
(
mat
),
Match
)
assert
iss
ubclass
(
type
(
mat
),
Match
[
str
])
self
.
assertIsS
ubclass
(
mat
.
__class__
,
Match
)
self
.
assertIsS
ubclass
(
mat
.
__class__
,
Match
[
str
])
self
.
assertIsS
ubclass
(
mat
.
__class__
,
Match
[
bytes
])
# Sad but true.
self
.
assertIsS
ubclass
(
type
(
mat
),
Match
)
self
.
assertIsS
ubclass
(
type
(
mat
),
Match
[
str
])
p
=
Pattern
[
Union
[
str
,
bytes
]]
assert
iss
ubclass
(
Pattern
[
str
],
Pattern
)
assert
iss
ubclass
(
Pattern
[
str
],
p
)
self
.
assertIsS
ubclass
(
Pattern
[
str
],
Pattern
)
self
.
assertIsS
ubclass
(
Pattern
[
str
],
p
)
m
=
Match
[
Union
[
bytes
,
str
]]
assert
iss
ubclass
(
Match
[
bytes
],
Match
)
assert
iss
ubclass
(
Match
[
bytes
],
m
)
self
.
assertIsS
ubclass
(
Match
[
bytes
],
Match
)
self
.
assertIsS
ubclass
(
Match
[
bytes
],
m
)
def
test_errors
(
self
):
with
self
.
assertRaises
(
TypeError
):
...
...
@@ -1436,19 +1459,19 @@ class RETests(TestCase):
isinstance
(
42
,
Pattern
[
str
])
def
test_repr
(
self
):
assert
repr
(
Pattern
)
==
'Pattern[~AnyStr]'
assert
repr
(
Pattern
[
str
])
==
'Pattern[str]'
assert
repr
(
Pattern
[
bytes
])
==
'Pattern[bytes]'
assert
repr
(
Match
)
==
'Match[~AnyStr]'
assert
repr
(
Match
[
str
])
==
'Match[str]'
assert
repr
(
Match
[
bytes
])
==
'Match[bytes]'
self
.
assertEqual
(
repr
(
Pattern
),
'Pattern[~AnyStr]'
)
self
.
assertEqual
(
repr
(
Pattern
[
str
]),
'Pattern[str]'
)
self
.
assertEqual
(
repr
(
Pattern
[
bytes
]),
'Pattern[bytes]'
)
self
.
assertEqual
(
repr
(
Match
),
'Match[~AnyStr]'
)
self
.
assertEqual
(
repr
(
Match
[
str
]),
'Match[str]'
)
self
.
assertEqual
(
repr
(
Match
[
bytes
]),
'Match[bytes]'
)
def
test_re_submodule
(
self
):
from
typing.re
import
Match
,
Pattern
,
__all__
,
__name__
assert
Match
is
typing
.
Match
assert
Pattern
is
typing
.
Pattern
assert
set
(
__all__
)
==
set
([
'Match'
,
'Pattern'
]
)
assert
__name__
==
'typing.re'
self
.
assertIs
(
Match
,
typing
.
Match
)
self
.
assertIs
(
Pattern
,
typing
.
Pattern
)
self
.
assertEqual
(
set
(
__all__
),
set
([
'Match'
,
'Pattern'
])
)
self
.
assertEqual
(
__name__
,
'typing.re'
)
def
test_cannot_subclass
(
self
):
with
self
.
assertRaises
(
TypeError
)
as
ex
:
...
...
@@ -1456,29 +1479,30 @@ class RETests(TestCase):
class
A
(
typing
.
Match
):
pass
assert
str
(
ex
.
exception
)
==
"A type alias cannot be subclassed"
self
.
assertEqual
(
str
(
ex
.
exception
),
"A type alias cannot be subclassed"
)
class
AllTests
(
TestCase
):
class
AllTests
(
Base
TestCase
):
"""Tests for __all__."""
def
test_all
(
self
):
from
typing
import
__all__
as
a
# Just spot-check the first and last of every category.
assert
'AbstractSet'
in
a
assert
'ValuesView'
in
a
assert
'cast'
in
a
assert
'overload'
in
a
self
.
assertIn
(
'AbstractSet'
,
a
)
self
.
assertIn
(
'ValuesView'
,
a
)
self
.
assertIn
(
'cast'
,
a
)
self
.
assertIn
(
'overload'
,
a
)
if
hasattr
(
contextlib
,
'AbstractContextManager'
):
assert
'ContextManager'
in
a
self
.
assertIn
(
'ContextManager'
,
a
)
# Check that io and re are not exported.
assert
'io'
not
in
a
assert
're'
not
in
a
self
.
assertNotIn
(
'io'
,
a
)
self
.
assertNotIn
(
're'
,
a
)
# Spot-check that stdlib modules aren't exported.
assert
'os'
not
in
a
assert
'sys'
not
in
a
self
.
assertNotIn
(
'os'
,
a
)
self
.
assertNotIn
(
'sys'
,
a
)
# Check that Text is defined.
assert
'Text'
in
a
self
.
assertIn
(
'Text'
,
a
)
if
__name__
==
'__main__'
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment