You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pythongh-132494 made typing.py eagerly import annotationlib again because
typing contains several protocols. Avoid this by determining annotations
lazily. This should also make protocol creation faster:
Unpatched:
$ ./python.exe -m timeit -s 'from typing import Protocol, runtime_checkable' '''@runtime_checkable
class MyProtocol(Protocol):
def meth(self): pass
'''
50000 loops, best of 5: 9.28 usec per loop
$ ./python.exe -m timeit -s 'from typing import Protocol, runtime_checkable' '''class MyProtocol(Protocol):
def meth(self): pass
'''
50000 loops, best of 5: 9.05 usec per loop
Patched:
$ ./python.exe -m timeit -s 'from typing import Protocol, runtime_checkable' '''@runtime_checkable
class MyProtocol(Protocol):
def meth(self): pass
'''
50000 loops, best of 5: 7.69 usec per loop
$ ./python.exe -m timeit -s 'from typing import Protocol, runtime_checkable' '''class MyProtocol(Protocol):
def meth(self): pass
'''
50000 loops, best of 5: 7.78 usec per loop
This was on a debug build though and I haven't compared it with versions where Protocol just accessed
`.__annotations__` directly, and it's not a huge difference, so I don't think it's worth calling out the
optimization too much.
A downside of this change is that any errors that happen during the determination of attributes now
happen only the first time isinstance() is called. This seems OK since these errors happen only in
fairly exotic circumstances.
Another downside is that any attributes added after class initialization now get picked up as protocol
members. This came up in the typing test suite due to `@final`, but may cause issues elsewhere too.
0 commit comments