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
f65e31fe
Commit
f65e31fe
authored
May 18, 2018
by
Ivan Levkivskyi
Committed by
GitHub
May 18, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-28556: Don't simplify unions at runtime (GH-6841)
parent
5634331a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
21 additions
and
45 deletions
+21
-45
Doc/library/typing.rst
Doc/library/typing.rst
+3
-4
Lib/test/test_typing.py
Lib/test/test_typing.py
+12
-12
Lib/typing.py
Lib/typing.py
+3
-29
Misc/NEWS.d/next/Library/2018-05-17-22-53-08.bpo-28556.C6Hnd1.rst
...S.d/next/Library/2018-05-17-22-53-08.bpo-28556.C6Hnd1.rst
+3
-0
No files found.
Doc/library/typing.rst
View file @
f65e31fe
...
...
@@ -961,16 +961,15 @@ The module defines the following classes, functions and decorators:
Union[int, str] == Union[str, int]
* When a class and its subclass are present, the latter is skipped, e.g.::
Union[int, object] == object
* You cannot subclass or instantiate a union.
* You cannot write ``Union[X][Y]``.
* You can use ``Optional[X]`` as a shorthand for ``Union[X, None]``.
.. versionchanged:: 3.7
Don't remove explicit subclasses from unions at runtime.
.. data:: Optional
Optional type.
...
...
Lib/test/test_typing.py
View file @
f65e31fe
...
...
@@ -253,10 +253,11 @@ class UnionTests(BaseTestCase):
def
test_union_object
(
self
):
u
=
Union
[
object
]
self
.
assertEqual
(
u
,
object
)
u
=
Union
[
int
,
object
]
self
.
assertEqual
(
u
,
object
)
u
=
Union
[
object
,
int
]
self
.
assertEqual
(
u
,
object
)
u1
=
Union
[
int
,
object
]
u2
=
Union
[
object
,
int
]
self
.
assertEqual
(
u1
,
u2
)
self
.
assertNotEqual
(
u1
,
object
)
self
.
assertNotEqual
(
u2
,
object
)
def
test_unordered
(
self
):
u1
=
Union
[
int
,
float
]
...
...
@@ -267,13 +268,11 @@ class UnionTests(BaseTestCase):
t
=
Union
[
Employee
]
self
.
assertIs
(
t
,
Employee
)
def
test_base_class_disappears
(
self
):
u
=
Union
[
Employee
,
Manager
,
int
]
self
.
assertEqual
(
u
,
Union
[
int
,
Employee
])
u
=
Union
[
Manager
,
int
,
Employee
]
self
.
assertEqual
(
u
,
Union
[
int
,
Employee
])
def
test_base_class_kept
(
self
):
u
=
Union
[
Employee
,
Manager
]
self
.
assertIs
(
u
,
Employee
)
self
.
assertNotEqual
(
u
,
Employee
)
self
.
assertIn
(
Employee
,
u
.
__args__
)
self
.
assertIn
(
Manager
,
u
.
__args__
)
def
test_union_union
(
self
):
u
=
Union
[
int
,
float
]
...
...
@@ -317,7 +316,8 @@ class UnionTests(BaseTestCase):
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
)
self
.
assertIn
(
str
,
Union
[
str
,
typing
.
Iterable
[
int
]].
__args__
)
self
.
assertIn
(
typing
.
Iterable
[
int
],
Union
[
str
,
typing
.
Iterable
[
int
]].
__args__
)
def
test_union_compare_other
(
self
):
self
.
assertNotEqual
(
Union
,
object
)
...
...
@@ -917,7 +917,7 @@ class GenericTests(BaseTestCase):
self
.
assertEqual
(
Union
[
T
,
U
][
int
,
Union
[
int
,
str
]],
Union
[
int
,
str
])
class
Base
:
...
class
Derived
(
Base
):
...
self
.
assertEqual
(
Union
[
T
,
Base
][
Derived
],
Base
)
self
.
assertEqual
(
Union
[
T
,
Base
][
Union
[
Base
,
Derived
]],
Union
[
Base
,
Derived
]
)
with
self
.
assertRaises
(
TypeError
):
Union
[
T
,
int
][
1
]
...
...
Lib/typing.py
View file @
f65e31fe
...
...
@@ -206,8 +206,8 @@ def _check_generic(cls, parameters):
def
_remove_dups_flatten
(
parameters
):
"""An internal helper for Union creation and substitution: flatten Union
'
s
among parameters, then remove duplicates
and strict subclasses
.
"""An internal helper for Union creation and substitution: flatten Unions
among parameters, then remove duplicates.
"""
# Flatten out Union[Union[...], ...].
params
=
[]
...
...
@@ -228,20 +228,7 @@ def _remove_dups_flatten(parameters):
all_params
.
remove
(
t
)
params
=
new_params
assert
not
all_params
,
all_params
# Weed out subclasses.
# E.g. Union[int, Employee, Manager] == Union[int, Employee].
# If object is present it will be sole survivor among proper classes.
# Never discard type variables.
# (In particular, Union[str, AnyStr] != AnyStr.)
all_params
=
set
(
params
)
for
t1
in
params
:
if
not
isinstance
(
t1
,
type
):
continue
if
any
((
isinstance
(
t2
,
type
)
or
isinstance
(
t2
,
_GenericAlias
)
and
t2
.
_special
)
and
issubclass
(
t1
,
t2
)
for
t2
in
all_params
-
{
t1
}):
all_params
.
remove
(
t1
)
return
tuple
(
t
for
t
in
params
if
t
in
all_params
)
return
tuple
(
params
)
_cleanups
=
[]
...
...
@@ -440,19 +427,6 @@ Union = _SpecialForm('Union', doc=
Union[int, str] == Union[str, int]
- When two arguments have a subclass relationship, the least
derived argument is kept, e.g.::
class Employee: pass
class Manager(Employee): pass
Union[int, Employee, Manager] == Union[int, Employee]
Union[Manager, int, Employee] == Union[int, Employee]
Union[Employee, Manager] == Employee
- Similar for object::
Union[int, object] == object
- You cannot subclass or instantiate a union.
- You can use Optional[X] as a shorthand for Union[X, None].
"""
)
...
...
Misc/NEWS.d/next/Library/2018-05-17-22-53-08.bpo-28556.C6Hnd1.rst
0 → 100644
View file @
f65e31fe
Do not simplify arguments to `typing.Union`. Now `Union[Manager, Employee]`
is not simplified to `Employee` at runtime. Such simplification previously
caused several bugs and limited possibilities for introspection.
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