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
6a2dc1bd
Commit
6a2dc1bd
authored
Apr 05, 2016
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Plain Diff
Merge upstream typing.py changes from 3.5 branch.
parents
b15c3049
bd5b9a07
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
371 additions
and
179 deletions
+371
-179
Lib/test/test_typing.py
Lib/test/test_typing.py
+129
-27
Lib/typing.py
Lib/typing.py
+242
-152
No files found.
Lib/test/test_typing.py
View file @
6a2dc1bd
import
asyncio
import
pickle
import
pickle
import
re
import
re
import
sys
import
sys
from
unittest
import
TestCase
,
main
from
unittest
import
TestCase
,
main
,
skipUnless
from
typing
import
Any
from
typing
import
Any
from
typing
import
TypeVar
,
AnyStr
from
typing
import
TypeVar
,
AnyStr
...
@@ -133,6 +132,7 @@ class TypeVarTests(TestCase):
...
@@ -133,6 +132,7 @@ class TypeVarTests(TestCase):
def
test_constrained_error
(
self
):
def
test_constrained_error
(
self
):
with
self
.
assertRaises
(
TypeError
):
with
self
.
assertRaises
(
TypeError
):
X
=
TypeVar
(
'X'
,
int
)
X
=
TypeVar
(
'X'
,
int
)
X
def
test_union_unique
(
self
):
def
test_union_unique
(
self
):
X
=
TypeVar
(
'X'
)
X
=
TypeVar
(
'X'
)
...
@@ -317,6 +317,7 @@ class UnionTests(TestCase):
...
@@ -317,6 +317,7 @@ class UnionTests(TestCase):
def
test_union_str_pattern
(
self
):
def
test_union_str_pattern
(
self
):
# Shouldn't crash; see http://bugs.python.org/issue25390
# Shouldn't crash; see http://bugs.python.org/issue25390
A
=
Union
[
str
,
Pattern
]
A
=
Union
[
str
,
Pattern
]
A
class
TypeVarUnionTests
(
TestCase
):
class
TypeVarUnionTests
(
TestCase
):
...
@@ -487,7 +488,7 @@ class SimpleMapping(Generic[XK, XV]):
...
@@ -487,7 +488,7 @@ class SimpleMapping(Generic[XK, XV]):
...
...
class
MySimpleMapping
(
SimpleMapping
):
class
MySimpleMapping
(
SimpleMapping
[
XK
,
XV
]
):
def
__init__
(
self
):
def
__init__
(
self
):
self
.
store
=
{}
self
.
store
=
{}
...
@@ -541,6 +542,7 @@ class ProtocolTests(TestCase):
...
@@ -541,6 +542,7 @@ class ProtocolTests(TestCase):
assert
not
issubclass
(
str
,
typing
.
SupportsAbs
)
assert
not
issubclass
(
str
,
typing
.
SupportsAbs
)
def
test_supports_round
(
self
):
def
test_supports_round
(
self
):
issubclass
(
float
,
typing
.
SupportsRound
)
assert
issubclass
(
float
,
typing
.
SupportsRound
)
assert
issubclass
(
float
,
typing
.
SupportsRound
)
assert
issubclass
(
int
,
typing
.
SupportsRound
)
assert
issubclass
(
int
,
typing
.
SupportsRound
)
assert
not
issubclass
(
str
,
typing
.
SupportsRound
)
assert
not
issubclass
(
str
,
typing
.
SupportsRound
)
...
@@ -551,20 +553,23 @@ class ProtocolTests(TestCase):
...
@@ -551,20 +553,23 @@ class ProtocolTests(TestCase):
def
test_protocol_instance_type_error
(
self
):
def
test_protocol_instance_type_error
(
self
):
with
self
.
assertRaises
(
TypeError
):
with
self
.
assertRaises
(
TypeError
):
isinstance
(
[],
typing
.
Reversible
)
isinstance
(
0
,
typing
.
SupportsAbs
)
class
GenericTests
(
TestCase
):
class
GenericTests
(
TestCase
):
def
test_basics
(
self
):
def
test_basics
(
self
):
X
=
SimpleMapping
[
str
,
Any
]
X
=
SimpleMapping
[
str
,
Any
]
Y
=
SimpleMapping
[
XK
,
str
]
assert
X
.
__parameters__
==
()
X
[
str
,
str
]
Y
[
str
,
str
]
with
self
.
assertRaises
(
TypeError
):
with
self
.
assertRaises
(
TypeError
):
X
[
int
,
str
]
X
[
str
]
with
self
.
assertRaises
(
TypeError
):
X
[
str
,
str
]
Y
=
SimpleMapping
[
XK
,
str
]
assert
Y
.
__parameters__
==
(
XK
,)
Y
[
str
]
with
self
.
assertRaises
(
TypeError
):
with
self
.
assertRaises
(
TypeError
):
Y
[
str
,
bytes
]
Y
[
str
,
str
]
def
test_init
(
self
):
def
test_init
(
self
):
T
=
TypeVar
(
'T'
)
T
=
TypeVar
(
'T'
)
...
@@ -576,30 +581,61 @@ class GenericTests(TestCase):
...
@@ -576,30 +581,61 @@ class GenericTests(TestCase):
def
test_repr
(
self
):
def
test_repr
(
self
):
self
.
assertEqual
(
repr
(
SimpleMapping
),
self
.
assertEqual
(
repr
(
SimpleMapping
),
__name__
+
'.'
+
'SimpleMapping
[~XK, ~XV]
'
)
__name__
+
'.'
+
'SimpleMapping
<~XK, ~XV>
'
)
self
.
assertEqual
(
repr
(
MySimpleMapping
),
self
.
assertEqual
(
repr
(
MySimpleMapping
),
__name__
+
'.'
+
'MySimpleMapping[~XK, ~XV]'
)
__name__
+
'.'
+
'MySimpleMapping<~XK, ~XV>'
)
def
test_chain_repr
(
self
):
T
=
TypeVar
(
'T'
)
S
=
TypeVar
(
'S'
)
class
C
(
Generic
[
T
]):
pass
X
=
C
[
Tuple
[
S
,
T
]]
assert
X
==
C
[
Tuple
[
S
,
T
]]
assert
X
!=
C
[
Tuple
[
T
,
S
]]
Y
=
X
[
T
,
int
]
assert
Y
==
X
[
T
,
int
]
assert
Y
!=
X
[
S
,
int
]
assert
Y
!=
X
[
T
,
str
]
Z
=
Y
[
str
]
assert
Z
==
Y
[
str
]
assert
Z
!=
Y
[
int
]
assert
Z
!=
Y
[
T
]
assert
str
(
Z
).
endswith
(
'.C<~T>[typing.Tuple[~S, ~T]]<~S, ~T>[~T, int]<~T>[str]'
)
def
test_dict
(
self
):
def
test_dict
(
self
):
T
=
TypeVar
(
'T'
)
T
=
TypeVar
(
'T'
)
class
B
(
Generic
[
T
]):
class
B
(
Generic
[
T
]):
pass
pass
b
=
B
()
b
=
B
()
b
.
foo
=
42
b
.
foo
=
42
self
.
assertEqual
(
b
.
__dict__
,
{
'foo'
:
42
})
self
.
assertEqual
(
b
.
__dict__
,
{
'foo'
:
42
})
class
C
(
B
[
int
]):
class
C
(
B
[
int
]):
pass
pass
c
=
C
()
c
=
C
()
c
.
bar
=
'abc'
c
.
bar
=
'abc'
self
.
assertEqual
(
c
.
__dict__
,
{
'bar'
:
'abc'
})
self
.
assertEqual
(
c
.
__dict__
,
{
'bar'
:
'abc'
})
def
test_pickle
(
self
):
def
test_pickle
(
self
):
global
C
# pickle wants to reference the class by name
T
=
TypeVar
(
'T'
)
T
=
TypeVar
(
'T'
)
class
B
(
Generic
[
T
]):
class
B
(
Generic
[
T
]):
pass
pass
global
C
# pickle wants to reference the class by name
class
C
(
B
[
int
]):
class
C
(
B
[
int
]):
pass
pass
c
=
C
()
c
=
C
()
c
.
foo
=
42
c
.
foo
=
42
c
.
bar
=
'abc'
c
.
bar
=
'abc'
...
@@ -626,12 +662,12 @@ class GenericTests(TestCase):
...
@@ -626,12 +662,12 @@ class GenericTests(TestCase):
assert
C
.
__module__
==
__name__
assert
C
.
__module__
==
__name__
if
not
PY32
:
if
not
PY32
:
assert
C
.
__qualname__
==
'GenericTests.test_repr_2.<locals>.C'
assert
C
.
__qualname__
==
'GenericTests.test_repr_2.<locals>.C'
assert
repr
(
C
).
split
(
'.'
)[
-
1
]
==
'C
[~T]
'
assert
repr
(
C
).
split
(
'.'
)[
-
1
]
==
'C
<~T>
'
X
=
C
[
int
]
X
=
C
[
int
]
assert
X
.
__module__
==
__name__
assert
X
.
__module__
==
__name__
if
not
PY32
:
if
not
PY32
:
assert
X
.
__qualname__
==
'C'
assert
X
.
__qualname__
==
'C'
assert
repr
(
X
).
split
(
'.'
)[
-
1
]
==
'C[int]'
assert
repr
(
X
).
split
(
'.'
)[
-
1
]
==
'C
<~T>
[int]'
class
Y
(
C
[
int
]):
class
Y
(
C
[
int
]):
pass
pass
...
@@ -639,7 +675,7 @@ class GenericTests(TestCase):
...
@@ -639,7 +675,7 @@ class GenericTests(TestCase):
assert
Y
.
__module__
==
__name__
assert
Y
.
__module__
==
__name__
if
not
PY32
:
if
not
PY32
:
assert
Y
.
__qualname__
==
'GenericTests.test_repr_2.<locals>.Y'
assert
Y
.
__qualname__
==
'GenericTests.test_repr_2.<locals>.Y'
assert
repr
(
Y
).
split
(
'.'
)[
-
1
]
==
'Y
[int]
'
assert
repr
(
Y
).
split
(
'.'
)[
-
1
]
==
'Y'
def
test_eq_1
(
self
):
def
test_eq_1
(
self
):
assert
Generic
==
Generic
assert
Generic
==
Generic
...
@@ -667,15 +703,14 @@ class GenericTests(TestCase):
...
@@ -667,15 +703,14 @@ class GenericTests(TestCase):
class
B
(
Generic
[
KT
,
T
]):
class
B
(
Generic
[
KT
,
T
]):
pass
pass
class
C
(
A
,
Generic
[
KT
,
VT
],
B
):
class
C
(
A
[
T
,
VT
],
Generic
[
VT
,
T
,
KT
],
B
[
KT
,
T
]
):
pass
pass
assert
C
.
__parameters__
==
(
T
,
V
T
,
KT
)
assert
C
.
__parameters__
==
(
VT
,
T
,
KT
)
def
test_nested
(
self
):
def
test_nested
(
self
):
class
G
(
Generic
):
G
=
Generic
pass
class
Visitor
(
G
[
T
]):
class
Visitor
(
G
[
T
]):
...
@@ -721,9 +756,30 @@ class GenericTests(TestCase):
...
@@ -721,9 +756,30 @@ class GenericTests(TestCase):
assert
type
(
a
)
is
Node
assert
type
(
a
)
is
Node
assert
type
(
b
)
is
Node
assert
type
(
b
)
is
Node
assert
type
(
c
)
is
Node
assert
type
(
c
)
is
Node
assert
a
.
label
==
x
assert
b
.
label
==
x
assert
c
.
label
==
x
foo
(
42
)
foo
(
42
)
def
test_implicit_any
(
self
):
T
=
TypeVar
(
'T'
)
class
C
(
Generic
[
T
]):
pass
class
D
(
C
):
pass
assert
D
.
__parameters__
==
()
with
self
.
assertRaises
(
Exception
):
D
[
int
]
with
self
.
assertRaises
(
Exception
):
D
[
Any
]
with
self
.
assertRaises
(
Exception
):
D
[
T
]
class
VarianceTests
(
TestCase
):
class
VarianceTests
(
TestCase
):
...
@@ -956,13 +1012,32 @@ class OverloadTests(TestCase):
...
@@ -956,13 +1012,32 @@ class OverloadTests(TestCase):
from
typing
import
overload
from
typing
import
overload
with
self
.
assertRaises
(
RuntimeError
):
with
self
.
assertRaises
(
RuntimeError
):
@
overload
@
overload
def
blah
():
def
blah
():
pass
pass
blah
()
T_a
=
TypeVar
(
'T'
)
def
test_overload_succeeds
(
self
):
from
typing
import
overload
@
overload
def
blah
():
pass
def
blah
():
pass
blah
()
PY35
=
sys
.
version_info
[:
2
]
>=
(
3
,
5
)
PY35_TESTS
=
"""
import asyncio
T_a = TypeVar('T')
class AwaitableWrapper(typing.Awaitable[T_a]):
class AwaitableWrapper(typing.Awaitable[T_a]):
...
@@ -973,7 +1048,6 @@ class AwaitableWrapper(typing.Awaitable[T_a]):
...
@@ -973,7 +1048,6 @@ class AwaitableWrapper(typing.Awaitable[T_a]):
yield
yield
return self.value
return self.value
class AsyncIteratorWrapper(typing.AsyncIterator[T_a]):
class AsyncIteratorWrapper(typing.AsyncIterator[T_a]):
def __init__(self, value: typing.Iterable[T_a]):
def __init__(self, value: typing.Iterable[T_a]):
...
@@ -989,6 +1063,10 @@ class AsyncIteratorWrapper(typing.AsyncIterator[T_a]):
...
@@ -989,6 +1063,10 @@ class AsyncIteratorWrapper(typing.AsyncIterator[T_a]):
return data
return data
else:
else:
raise StopAsyncIteration
raise StopAsyncIteration
"""
if
PY35
:
exec
(
PY35_TESTS
)
class
CollectionsAbcTests
(
TestCase
):
class
CollectionsAbcTests
(
TestCase
):
...
@@ -1015,9 +1093,14 @@ class CollectionsAbcTests(TestCase):
...
@@ -1015,9 +1093,14 @@ class CollectionsAbcTests(TestCase):
assert
isinstance
(
it
,
typing
.
Iterator
[
int
])
assert
isinstance
(
it
,
typing
.
Iterator
[
int
])
assert
not
isinstance
(
42
,
typing
.
Iterator
)
assert
not
isinstance
(
42
,
typing
.
Iterator
)
@
skipUnless
(
PY35
,
'Python 3.5 required'
)
def
test_awaitable
(
self
):
def
test_awaitable
(
self
):
async
def
foo
()
->
typing
.
Awaitable
[
int
]:
ns
=
{}
return
await
AwaitableWrapper
(
42
)
exec
(
"async def foo() -> typing.Awaitable[int]:
\
n
"
" return await AwaitableWrapper(42)
\
n
"
,
globals
(),
ns
)
foo
=
ns
[
'foo'
]
g
=
foo
()
g
=
foo
()
assert
issubclass
(
type
(
g
),
typing
.
Awaitable
[
int
])
assert
issubclass
(
type
(
g
),
typing
.
Awaitable
[
int
])
assert
isinstance
(
g
,
typing
.
Awaitable
)
assert
isinstance
(
g
,
typing
.
Awaitable
)
...
@@ -1028,6 +1111,7 @@ class CollectionsAbcTests(TestCase):
...
@@ -1028,6 +1111,7 @@ class CollectionsAbcTests(TestCase):
typing
.
Awaitable
[
Manager
])
typing
.
Awaitable
[
Manager
])
g
.
send
(
None
)
# Run foo() till completion, to avoid warning.
g
.
send
(
None
)
# Run foo() till completion, to avoid warning.
@
skipUnless
(
PY35
,
'Python 3.5 required'
)
def
test_async_iterable
(
self
):
def
test_async_iterable
(
self
):
base_it
=
range
(
10
)
# type: Iterator[int]
base_it
=
range
(
10
)
# type: Iterator[int]
it
=
AsyncIteratorWrapper
(
base_it
)
it
=
AsyncIteratorWrapper
(
base_it
)
...
@@ -1037,6 +1121,7 @@ class CollectionsAbcTests(TestCase):
...
@@ -1037,6 +1121,7 @@ class CollectionsAbcTests(TestCase):
typing
.
AsyncIterable
[
Employee
])
typing
.
AsyncIterable
[
Employee
])
assert
not
isinstance
(
42
,
typing
.
AsyncIterable
)
assert
not
isinstance
(
42
,
typing
.
AsyncIterable
)
@
skipUnless
(
PY35
,
'Python 3.5 required'
)
def
test_async_iterator
(
self
):
def
test_async_iterator
(
self
):
base_it
=
range
(
10
)
# type: Iterator[int]
base_it
=
range
(
10
)
# type: Iterator[int]
it
=
AsyncIteratorWrapper
(
base_it
)
it
=
AsyncIteratorWrapper
(
base_it
)
...
@@ -1127,6 +1212,22 @@ class CollectionsAbcTests(TestCase):
...
@@ -1127,6 +1212,22 @@ class CollectionsAbcTests(TestCase):
d
=
MyDict
()
d
=
MyDict
()
assert
isinstance
(
d
,
MyDict
)
assert
isinstance
(
d
,
MyDict
)
def
test_no_defaultdict_instantiation
(
self
):
with
self
.
assertRaises
(
TypeError
):
typing
.
DefaultDict
()
with
self
.
assertRaises
(
TypeError
):
typing
.
DefaultDict
[
KT
,
VT
]()
with
self
.
assertRaises
(
TypeError
):
typing
.
DefaultDict
[
str
,
int
]()
def
test_defaultdict_subclass_instantiation
(
self
):
class
MyDefDict
(
typing
.
DefaultDict
[
str
,
int
]):
pass
dd
=
MyDefDict
()
assert
isinstance
(
dd
,
MyDefDict
)
def
test_no_set_instantiation
(
self
):
def
test_no_set_instantiation
(
self
):
with
self
.
assertRaises
(
TypeError
):
with
self
.
assertRaises
(
TypeError
):
typing
.
Set
()
typing
.
Set
()
...
@@ -1251,7 +1352,7 @@ class IOTests(TestCase):
...
@@ -1251,7 +1352,7 @@ class IOTests(TestCase):
return
a
.
readline
()
return
a
.
readline
()
a
=
stuff
.
__annotations__
[
'a'
]
a
=
stuff
.
__annotations__
[
'a'
]
assert
a
.
__parameters__
==
(
str
,
)
assert
a
.
__parameters__
==
()
def
test_binaryio
(
self
):
def
test_binaryio
(
self
):
...
@@ -1259,7 +1360,7 @@ class IOTests(TestCase):
...
@@ -1259,7 +1360,7 @@ class IOTests(TestCase):
return
a
.
readline
()
return
a
.
readline
()
a
=
stuff
.
__annotations__
[
'a'
]
a
=
stuff
.
__annotations__
[
'a'
]
assert
a
.
__parameters__
==
(
bytes
,
)
assert
a
.
__parameters__
==
()
def
test_io_submodule
(
self
):
def
test_io_submodule
(
self
):
from
typing.io
import
IO
,
TextIO
,
BinaryIO
,
__all__
,
__name__
from
typing.io
import
IO
,
TextIO
,
BinaryIO
,
__all__
,
__name__
...
@@ -1346,8 +1447,9 @@ class AllTests(TestCase):
...
@@ -1346,8 +1447,9 @@ class AllTests(TestCase):
assert
'ValuesView'
in
a
assert
'ValuesView'
in
a
assert
'cast'
in
a
assert
'cast'
in
a
assert
'overload'
in
a
assert
'overload'
in
a
assert
'io'
in
a
# Check that io and re are not exported.
assert
're'
in
a
assert
'io'
not
in
a
assert
're'
not
in
a
# Spot-check that stdlib modules aren't exported.
# Spot-check that stdlib modules aren't exported.
assert
'os'
not
in
a
assert
'os'
not
in
a
assert
'sys'
not
in
a
assert
'sys'
not
in
a
...
...
Lib/typing.py
View file @
6a2dc1bd
This diff is collapsed.
Click to expand it.
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