-
Notifications
You must be signed in to change notification settings - Fork 263
Follow up for #283 (response to comments) including change in Union[X, Any] #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,7 +149,7 @@ def __new__(cls, *args, **kwds): | |
isinstance(args[1], tuple)): | ||
# Close enough. | ||
raise TypeError("Cannot subclass %r" % cls) | ||
return object.__new__(cls) | ||
return super(_TypingBase, cls).__new__(cls) | ||
|
||
# Things that are not classes also need these. | ||
def _eval_type(self, globalns, localns): | ||
|
@@ -168,7 +168,11 @@ def __call__(self, *args, **kwds): | |
|
||
|
||
class _FinalTypingBase(_TypingBase): | ||
"""Mix-in class to prevent instantiation.""" | ||
"""Mix-in class to prevent instantiation. | ||
|
||
Prevents instantiation unless _root=True is given in class call. | ||
It is used to create pseudo-singleton instances Any, Union, Tuple, etc. | ||
""" | ||
|
||
__slots__ = () | ||
|
||
|
@@ -256,7 +260,7 @@ def __init__(self, name, type_var, impl_type, type_checker): | |
assert isinstance(name, basestring), repr(name) | ||
assert isinstance(impl_type, type), repr(impl_type) | ||
assert not isinstance(impl_type, TypingMeta), repr(impl_type) | ||
assert isinstance(type_var, (type, _TypingBase)) | ||
assert isinstance(type_var, (type, _TypingBase)), repr(type_var) | ||
self.name = name | ||
self.type_var = type_var | ||
self.impl_type = impl_type | ||
|
@@ -427,9 +431,13 @@ def __new__(cls, name, bases, namespace): | |
class _Any(_FinalTypingBase): | ||
"""Special type indicating an unconstrained type. | ||
|
||
- Any object is an instance of Any. | ||
- Any class is a subclass of Any. | ||
- As a special case, Any and object are subclasses of each other. | ||
- Any is compatible with every type. | ||
- Any assumed to have all methods. | ||
- All values assumed to be instances of Any. | ||
|
||
Note that all the above statements are true from the point of view of | ||
static type checkers. At runtime, Any should not be used with instance | ||
or class checks. | ||
""" | ||
__metaclass__ = AnyMeta | ||
__slots__ = () | ||
|
@@ -598,16 +606,10 @@ class Manager(Employee): pass | |
Union[Manager, int, Employee] == Union[int, Employee] | ||
Union[Employee, Manager] == Employee | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd keep this even though it's not a special case -- it's a nice edge case that confirms the rule. |
||
|
||
- Corollary: if Any is present it is the sole survivor, e.g.:: | ||
|
||
Union[int, Any] == Any | ||
|
||
- Similar for object:: | ||
|
||
Union[int, object] == object | ||
|
||
- To cut a tie: Union[object, Any] == Union[Any, object] == Any. | ||
|
||
- You cannot subclass or instantiate a union. | ||
|
||
- You cannot write Union[X][Y] (what would it mean?). | ||
|
@@ -646,14 +648,11 @@ def __new__(cls, parameters=None, *args, **kwds): | |
assert not all_params, all_params | ||
# Weed out subclasses. | ||
# E.g. Union[int, Employee, Manager] == Union[int, Employee]. | ||
# If Any or object is present it will be the sole survivor. | ||
# If both Any and object are present, Any wins. | ||
# Never discard type variables, except against Any. | ||
# 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 t1 is Any: | ||
return Any | ||
if not isinstance(t1, type): | ||
continue | ||
if any(isinstance(t2, type) and issubclass(t1, t2) | ||
|
@@ -726,7 +725,7 @@ def __new__(cls, name, bases, namespace): | |
class _Optional(_FinalTypingBase): | ||
"""Optional type. | ||
|
||
Optional[X] is equivalent to Union[X, type(None)]. | ||
Optional[X] is equivalent to Union[X, None]. | ||
""" | ||
|
||
__metaclass__ = OptionalMeta | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not keep the test but change the asserts to test for the new behavior?