Kill __subclasscheck__#283
Conversation
This reverts commit fa4a681.
|
@gvanrossum @JukkaL I added this to the description. |
Sorry, something went wrong.
|
I did a quick pass on the diff and tried it with mypy, and I like it. The changes seem to speed up a no-op mypy invocation ( The generic type object cache hit rate when running |
Sorry, something went wrong.
gvanrossum
left a comment
There was a problem hiding this comment.
This is great! Here are some nits. My main concern is over __slots__ but even that's minor. I haven't reviewed everything (in fact I only reviewed src/typing.py) but I think we should just go ahead and release this with 3.6b2 and hope for the best. Maybe we'll get some real-world feedback from that and we can tweak things before 3.5.3 is finalized (sorry, there's no date in PEP 478, but we'll get ample warning, and I expect it to be around the same time as 3.6.0 goes out, mid December according to PEP 494).
Sorry, something went wrong.
|
@gvanrossum
|
Sorry, something went wrong.
|
@JukkaL |
Sorry, something went wrong.
gvanrossum
left a comment
There was a problem hiding this comment.
I am going to merge this as-is. While there are a few things left to do, it's easier to do them in a follow-up PR -- this one is just too big to review already.
Sorry, something went wrong.
|
Feel free to iterate more on this! Especially the Union[Any, int] issue. But I figured we might as well take a checkpoint, so this is now merged (also into CPython 3.5, 3.6 and 3.7==default). I closed the three CPython bugs you mentioned. |
Sorry, something went wrong.
|
@gvanrossum This is a good catch, I didn't notice this while editing PEP 483. The point is that type is characterized by two sets: set of values and set of functions (methods), as mentioned in PEP 483. With such definition neither Actually, now I think that x: Union[object, Any]
x.lower() # this should be rejected
if not isinstance(x, object):
x.lower() # this should be allowed, but it is not reachableWhat do you think? (If it will help, the relation between |
Sorry, something went wrong.
|
Good reasoning! I like talking about "has more methods" and "has more
values".
|
Sorry, something went wrong.
This PR:
isinstancepart, and multiple inheritance, stilltyping.Somethingis not a drop-in replacement forcollections.abc.Somethingin terms of implementation).issubclasstests fromtest_typingand main changes totypingare__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 bytyping).Using
isinstance()orissubclass()risesTypeErrorfor almost everything. There are exceptions:issubclass({}, typing.Mapping). This is done to (a) not break existing code by addition of type information; (b) to allow usingtypingclasses as a replacement forcollections.abcin class and instance checks. Finally, there is an agreement that a generic without parameters assumesAny, andAnymeans fallback to dynamic typing.isinstance(lambda x: x, typing.Callable)is also OK. AlthoughCallableis not a generic class, when unsubscribed, it could be also used as a replacement forcollections.abc.Callable.isinstance([], typing.List)possible, for consistency I also allowedisinstance((), 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 offunctools.lru_cachethat allows fallback to non-cached version for unhashable arguments.This is still WIP, since I did this only for Python 3.
@gvanrossum @JukkaL Please take a look.