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
efa798d1
Commit
efa798d1
authored
Aug 23, 2016
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
A new version of typing.py from
https://github.com/python/typing
.
parent
83f5a384
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
26 deletions
+61
-26
Lib/test/test_typing.py
Lib/test/test_typing.py
+12
-1
Lib/typing.py
Lib/typing.py
+43
-25
Misc/NEWS
Misc/NEWS
+6
-0
No files found.
Lib/test/test_typing.py
View file @
efa798d1
...
@@ -512,6 +512,10 @@ class CallableTests(BaseTestCase):
...
@@ -512,6 +512,10 @@ class CallableTests(BaseTestCase):
self
.
assertEqual
(
get_type_hints
(
foo
,
globals
(),
locals
()),
self
.
assertEqual
(
get_type_hints
(
foo
,
globals
(),
locals
()),
{
'a'
:
Callable
[...,
T
]})
{
'a'
:
Callable
[...,
T
]})
def
test_ellipsis_in_generic
(
self
):
# Shouldn't crash; see https://github.com/python/typing/issues/259
typing
.
List
[
Callable
[...,
str
]]
XK
=
TypeVar
(
'XK'
,
str
,
bytes
)
XK
=
TypeVar
(
'XK'
,
str
,
bytes
)
XV
=
TypeVar
(
'XV'
)
XV
=
TypeVar
(
'XV'
)
...
@@ -852,7 +856,7 @@ class VarianceTests(BaseTestCase):
...
@@ -852,7 +856,7 @@ class VarianceTests(BaseTestCase):
def
test_covariance_sequence
(
self
):
def
test_covariance_sequence
(
self
):
# Check covariance for Sequence (which is just a generic class
# Check covariance for Sequence (which is just a generic class
# for this purpose, but using a
covariant type variabl
e).
# for this purpose, but using a
type variable with covariant=Tru
e).
self
.
assertIsSubclass
(
typing
.
Sequence
[
Manager
],
self
.
assertIsSubclass
(
typing
.
Sequence
[
Manager
],
typing
.
Sequence
[
Employee
])
typing
.
Sequence
[
Employee
])
self
.
assertNotIsSubclass
(
typing
.
Sequence
[
Employee
],
self
.
assertNotIsSubclass
(
typing
.
Sequence
[
Employee
],
...
@@ -1185,6 +1189,13 @@ class CollectionsAbcTests(BaseTestCase):
...
@@ -1185,6 +1189,13 @@ class CollectionsAbcTests(BaseTestCase):
self
.
assertIsInstance
([],
typing
.
Container
)
self
.
assertIsInstance
([],
typing
.
Container
)
self
.
assertNotIsInstance
(
42
,
typing
.
Container
)
self
.
assertNotIsInstance
(
42
,
typing
.
Container
)
def
test_collection
(
self
):
if
hasattr
(
typing
,
'Collection'
):
self
.
assertIsInstance
(
tuple
(),
typing
.
Collection
)
self
.
assertIsInstance
(
frozenset
(),
typing
.
Collection
)
self
.
assertIsSubclass
(
dict
,
typing
.
Collection
)
self
.
assertNotIsInstance
(
42
,
typing
.
Collection
)
def
test_abstractset
(
self
):
def
test_abstractset
(
self
):
self
.
assertIsInstance
(
set
(),
typing
.
AbstractSet
)
self
.
assertIsInstance
(
set
(),
typing
.
AbstractSet
)
self
.
assertNotIsInstance
(
42
,
typing
.
AbstractSet
)
self
.
assertNotIsInstance
(
42
,
typing
.
AbstractSet
)
...
...
Lib/typing.py
View file @
efa798d1
...
@@ -57,6 +57,7 @@ __all__ = [
...
@@ -57,6 +57,7 @@ __all__ = [
'DefaultDict'
,
'DefaultDict'
,
'List'
,
'List'
,
'Set'
,
'Set'
,
'FrozenSet'
,
'NamedTuple'
,
# Not really a type.
'NamedTuple'
,
# Not really a type.
'Generator'
,
'Generator'
,
...
@@ -160,12 +161,6 @@ class _ForwardRef(TypingMeta):
...
@@ -160,12 +161,6 @@ class _ForwardRef(TypingMeta):
return
self
return
self
def
_eval_type
(
self
,
globalns
,
localns
):
def
_eval_type
(
self
,
globalns
,
localns
):
if
not
isinstance
(
localns
,
dict
):
raise
TypeError
(
'ForwardRef localns must be a dict -- got %r'
%
(
localns
,))
if
not
isinstance
(
globalns
,
dict
):
raise
TypeError
(
'ForwardRef globalns must be a dict -- got %r'
%
(
globalns
,))
if
not
self
.
__forward_evaluated__
:
if
not
self
.
__forward_evaluated__
:
if
globalns
is
None
and
localns
is
None
:
if
globalns
is
None
and
localns
is
None
:
globalns
=
localns
=
{}
globalns
=
localns
=
{}
...
@@ -388,9 +383,10 @@ class TypeVar(TypingMeta, metaclass=TypingMeta, _root=True):
...
@@ -388,9 +383,10 @@ class TypeVar(TypingMeta, metaclass=TypingMeta, _root=True):
and issubclass(bytes, A) are true, and issubclass(int, A) is
and issubclass(bytes, A) are true, and issubclass(int, A) is
false. (TODO: Why is this needed? This may change. See #136.)
false. (TODO: Why is this needed? This may change. See #136.)
Type variables may be marked covariant or contravariant by passing
Type variables defined with covariant=True or contravariant=True
covariant=True or contravariant=True. See PEP 484 for more
can be used do declare covariant or contravariant generic types.
details. By default type variables are invariant.
See PEP 484 for more details. By default generic types are invariant
in all type variables.
Type variables can be introspected. e.g.:
Type variables can be introspected. e.g.:
...
@@ -405,7 +401,7 @@ class TypeVar(TypingMeta, metaclass=TypingMeta, _root=True):
...
@@ -405,7 +401,7 @@ class TypeVar(TypingMeta, metaclass=TypingMeta, _root=True):
covariant
=
False
,
contravariant
=
False
):
covariant
=
False
,
contravariant
=
False
):
self
=
super
().
__new__
(
cls
,
name
,
(
Final
,),
{},
_root
=
True
)
self
=
super
().
__new__
(
cls
,
name
,
(
Final
,),
{},
_root
=
True
)
if
covariant
and
contravariant
:
if
covariant
and
contravariant
:
raise
ValueError
(
"Bivariant type
variable
s are not supported."
)
raise
ValueError
(
"Bivariant types are not supported."
)
self
.
__covariant__
=
bool
(
covariant
)
self
.
__covariant__
=
bool
(
covariant
)
self
.
__contravariant__
=
bool
(
contravariant
)
self
.
__contravariant__
=
bool
(
contravariant
)
if
constraints
and
bound
is
not
None
:
if
constraints
and
bound
is
not
None
:
...
@@ -782,7 +778,7 @@ class CallableMeta(TypingMeta):
...
@@ -782,7 +778,7 @@ class CallableMeta(TypingMeta):
return
self
return
self
def
_get_type_vars
(
self
,
tvars
):
def
_get_type_vars
(
self
,
tvars
):
if
self
.
__args__
:
if
self
.
__args__
and
self
.
__args__
is
not
Ellipsis
:
_get_type_vars
(
self
.
__args__
,
tvars
)
_get_type_vars
(
self
.
__args__
,
tvars
)
def
_eval_type
(
self
,
globalns
,
localns
):
def
_eval_type
(
self
,
globalns
,
localns
):
...
@@ -1044,7 +1040,7 @@ class GenericMeta(TypingMeta, abc.ABCMeta):
...
@@ -1044,7 +1040,7 @@ class GenericMeta(TypingMeta, abc.ABCMeta):
if
cls
is
Any
:
if
cls
is
Any
:
return
True
return
True
if
isinstance
(
cls
,
GenericMeta
):
if
isinstance
(
cls
,
GenericMeta
):
# For a c
lass C(Generic[T]) where T is co-variant
,
# For a c
ovariant class C(Generic[T])
,
# C[X] is a subclass of C[Y] iff X is a subclass of Y.
# C[X] is a subclass of C[Y] iff X is a subclass of Y.
origin
=
self
.
__origin__
origin
=
self
.
__origin__
if
origin
is
not
None
and
origin
is
cls
.
__origin__
:
if
origin
is
not
None
and
origin
is
cls
.
__origin__
:
...
@@ -1434,31 +1430,53 @@ class Container(Generic[T_co], extra=collections_abc.Container):
...
@@ -1434,31 +1430,53 @@ class Container(Generic[T_co], extra=collections_abc.Container):
__slots__
=
()
__slots__
=
()
# Callable was defined earlier.
if
hasattr
(
collections_abc
,
'Collection'
):
class
Collection
(
Sized
,
Iterable
[
T_co
],
Container
[
T_co
],
extra
=
collections_abc
.
Collection
):
__slots__
=
()
__all__
.
append
(
'Collection'
)
class
AbstractSet
(
Sized
,
Iterable
[
T_co
],
Container
[
T_co
],
extra
=
collections_abc
.
Set
):
# Callable was defined earlier.
pass
if
hasattr
(
collections_abc
,
'Collection'
):
class
AbstractSet
(
Collection
[
T_co
],
extra
=
collections_abc
.
Set
):
pass
else
:
class
AbstractSet
(
Sized
,
Iterable
[
T_co
],
Container
[
T_co
],
extra
=
collections_abc
.
Set
):
pass
class
MutableSet
(
AbstractSet
[
T
],
extra
=
collections_abc
.
MutableSet
):
class
MutableSet
(
AbstractSet
[
T
],
extra
=
collections_abc
.
MutableSet
):
pass
pass
# NOTE: Only the value type is covariant.
# NOTE: It is only covariant in the value type.
class
Mapping
(
Sized
,
Iterable
[
KT
],
Container
[
KT
],
Generic
[
KT
,
VT_co
],
if
hasattr
(
collections_abc
,
'Collection'
):
extra
=
collections_abc
.
Mapping
):
class
Mapping
(
Collection
[
KT
],
Generic
[
KT
,
VT_co
],
pass
extra
=
collections_abc
.
Mapping
):
pass
else
:
class
Mapping
(
Sized
,
Iterable
[
KT
],
Container
[
KT
],
Generic
[
KT
,
VT_co
],
extra
=
collections_abc
.
Mapping
):
pass
class
MutableMapping
(
Mapping
[
KT
,
VT
],
extra
=
collections_abc
.
MutableMapping
):
class
MutableMapping
(
Mapping
[
KT
,
VT
],
extra
=
collections_abc
.
MutableMapping
):
pass
pass
if
hasattr
(
collections_abc
,
'Reversible'
):
if
hasattr
(
collections_abc
,
'Reversible'
):
class
Sequence
(
Sized
,
Reversible
[
T_co
],
Container
[
T_co
],
if
hasattr
(
collections_abc
,
'Collection'
):
extra
=
collections_abc
.
Sequence
):
class
Sequence
(
Reversible
[
T_co
],
Collection
[
T_co
],
pass
extra
=
collections_abc
.
Sequence
):
pass
else
:
class
Sequence
(
Sized
,
Reversible
[
T_co
],
Container
[
T_co
],
extra
=
collections_abc
.
Sequence
):
pass
else
:
else
:
class
Sequence
(
Sized
,
Iterable
[
T_co
],
Container
[
T_co
],
class
Sequence
(
Sized
,
Iterable
[
T_co
],
Container
[
T_co
],
extra
=
collections_abc
.
Sequence
):
extra
=
collections_abc
.
Sequence
):
...
@@ -1583,11 +1601,11 @@ class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
...
@@ -1583,11 +1601,11 @@ class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
# Internal type variable used for Type[].
# Internal type variable used for Type[].
CT
=
TypeVar
(
'CT
'
,
covariant
=
True
,
bound
=
type
)
CT
_co
=
TypeVar
(
'CT_co
'
,
covariant
=
True
,
bound
=
type
)
# This is not a real generic class. Don't use outside annotations.
# This is not a real generic class. Don't use outside annotations.
class
Type
(
type
,
Generic
[
CT
],
extra
=
type
):
class
Type
(
type
,
Generic
[
CT
_co
],
extra
=
type
):
"""A special construct usable to annotate class objects.
"""A special construct usable to annotate class objects.
For example, suppose we have the following classes::
For example, suppose we have the following classes::
...
...
Misc/NEWS
View file @
efa798d1
...
@@ -50,6 +50,12 @@ Core and Builtins
...
@@ -50,6 +50,12 @@ Core and Builtins
Library
Library
-------
-------
-
A
new
version
of
typing
.
py
from
https
://
github
.
com
/
python
/
typing
:
-
Collection
(
only
for
3.6
)
(
Issue
#
27598
)
-
Add
FrozenSet
to
__all__
(
upstream
#
261
)
-
fix
crash
in
_get_type_vars
()
(
upstream
#
259
)
-
Remove
the
dict
constraint
in
ForwardRef
.
_eval_type
(
upstream
#
252
)
-
Issue
#
27539
:
Fix
unnormalised
``
Fraction
.
__pow__
``
result
in
the
case
-
Issue
#
27539
:
Fix
unnormalised
``
Fraction
.
__pow__
``
result
in
the
case
of
negative
exponent
and
negative
base
.
of
negative
exponent
and
negative
base
.
...
...
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