-
Notifications
You must be signed in to change notification settings - Fork 258
Bare Union, Callable and Tuple should not be classes #133
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
Comments
This was referenced May 22, 2015
Closed
Closed
I've been using Callable like that. What's wrong with doing so? Should I import Callable from |
Hm, that's a good point. Pretty much anything you can do with collections.abc.X should be possible with typing.X. (This does not apply to things that aren't defined in collection.abc though, so this is not the case for Union, Tuple or List etc.) |
Merged
gvanrossum
pushed a commit
that referenced
this issue
Sep 27, 2016
This PR: - Fixes #136 - Fixes #133 - Partially addresses #203 (fixes the isinstance part, and multiple inheritance, still typing.Something is not a drop-in replacement for collections.abc.Something in terms of implementation). - Also fixes http://bugs.python.org/issue26075, http://bugs.python.org/issue25830, and http://bugs.python.org/issue26477 - Makes almost everything up to 10x faster. - Is aimed to be a minimalistic change. I only removed issubclass tests from test_typing and main changes to typing are __new__ and __getitem__. The idea is to make most things not classes. Now _ForwardRef(), TypeVar(), Union[], Tuple[], Callable[] are not classes (i.e. new class objects are almost never created by typing). Using isinstance() or issubclass() rises TypeError for almost everything. There are exceptions: Unsubscripted generics are still OK, e.g. issubclass({}, typing.Mapping). This is done to (a) not break existing code by addition of type information; (b) to allow using typing classes as a replacement for collections.abc in class and instance checks. Finally, there is an agreement that a generic without parameters assumes Any, and Any means fallback to dynamic typing. isinstance(lambda x: x, typing.Callable) is also OK. Although Callable is not a generic class, when unsubscribed, it could be also used as a replacement for collections.abc.Callable. The first rule for generics makes isinstance([], typing.List) possible, for consistency I also allowed isinstance((), typing.Tuple). Finally, generics should be classes, to allow subclassing, but now the outcome of __getitem__ on classes is cached. I use an extended version of functools.lru_cache that allows fallback to non-cached version for unhashable arguments.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is no reason for "bare" Union, Callable or Tuple (i.e. without [...]) to be a class. You shouldn't ever write
isinstance(x, Union)
for example, and in their bare form these are not acceptable as annotations either.The text was updated successfully, but these errors were encountered: