Skip to content
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

Performance of typing._ProtocolMeta._get_protocol_attrs and isinstance #74690

Closed
orenbenkiki mannequin opened this issue May 29, 2017 · 22 comments · Fixed by #103348
Closed

Performance of typing._ProtocolMeta._get_protocol_attrs and isinstance #74690

orenbenkiki mannequin opened this issue May 29, 2017 · 22 comments · Fixed by #103348
Assignees
Labels
3.12 bugs and security fixes performance Performance or resource usage stdlib Python modules in the Lib dir topic-typing

Comments

@orenbenkiki
Copy link
Mannequin

orenbenkiki mannequin commented May 29, 2017

BPO 30505
Nosy @ilevkivskyi, @orenbenkiki

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2017-05-29.09:16:49.884>
labels = ['library', 'performance']
title = 'Performance of typing._ProtocolMeta._get_protocol_attrs and isinstance'
updated_at = <Date 2017-06-02.17:44:59.311>
user = 'https://github.com/orenbenkiki'

bugs.python.org fields:

activity = <Date 2017-06-02.17:44:59.311>
actor = 'levkivskyi'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2017-05-29.09:16:49.884>
creator = 'orenbenkiki'
dependencies = []
files = []
hgrepos = []
issue_num = 30505
keywords = []
message_count = 2.0
messages = ['294686', '295044']
nosy_count = 2.0
nosy_names = ['levkivskyi', 'orenbenkiki']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'performance'
url = 'https://bugs.python.org/issue30505'
versions = ['Python 3.6']

Linked PRs

@orenbenkiki
Copy link
Mannequin Author

orenbenkiki mannequin commented May 29, 2017

In 3.6.0, invocation of isinstance calls typing._ProtocolMeta._get_protocol_attrs.
This creates a set of all attributes in all base classes, loops on these attributes to check they exist, and discards the set. It is very slow.

My program uses isinstance to allow for flexibility in parameter types in certain key functions. I realize that using isinstance is frowned upon, but it seems to make sense in my case.

As a result, >95% of its run-time is inside typing._ProtocolMeta._get_protocol_attrs (!).

I have created a simple wrapper around isinstance which caches its result with a Dict[Tuple[type, type], bool]. This solved the performance problem, but introduced a different problem - type checking.

I use mypy and type annotations, and my code cleanly type-checks (with the occasional # type: ignore). If I switch to using my own isinstance function, then mypy's type inference no longer treats it as special, so it starts complaining about all uses of values protected by if isinstance(value, SomeType): ...

I propose that either the private typing._ProtocolMeta.__subclasscheck__ (which invokes _get_protocol_attrs), or the public isinstance, would be modified to cache their results.

I can create a PR for either approach, if this is acceptable.

@orenbenkiki orenbenkiki mannequin added stdlib Python modules in the Lib dir performance Performance or resource usage labels May 29, 2017
@ilevkivskyi
Copy link
Member

Thanks for reporting!

The runtime implementation of protocol classes will be thoroughly reworked as a part of PEP-544, see also python/typing#417 for a proof of concept runtime implementation.

Also, there is another ongoing discussion python/typing#432 about a global refactoring of typing module that will significantly improve performance.

Therefore, I would wait with any large PRs until these two stories are settled. If you still want to propose a small PR, you can do this at the upstream typing repo https://github.com/python/typing

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@iritkatriel
Copy link
Member

@orenbenkiki @ilevkivskyi What is the status of this issue, five years on?

@ilevkivskyi
Copy link
Member

TBH I am not sure this issue is still relevant (at least I didn't hear any recent complaints about Protocol performance). @orenbenkiki is performance still bad for your application?

@AlexWaygood
Copy link
Member

I've seen recent complaints about the performance of Protocol from users of the beartype third-party library, and by @posita here. (I'm not sure I've ever seen a proper write-up of the performance problems in a CPython issue, however.)

@ilevkivskyi
Copy link
Member

Hm, actually the short-circuiting proposed in that comment may be a good idea (unless I forgot something) for a simple perf optimization (btw FWIW this is what mypy does internally, it always checks nominal subtypig first).

@AlexWaygood
Copy link
Member

@posita appears to have implemented optimised versions of ProtocolMeta here and here.

(I haven't studied the code for either link, but may be worth looking at!)

@iritkatriel iritkatriel added the 3.12 bugs and security fixes label Sep 7, 2022
@posita
Copy link
Contributor

posita commented Sep 7, 2022

@posita appears to have implemented optimised versions of ProtocolMeta here and here.

(I haven't studied the code for either link, but may be worth looking at!)

For color, the current implementation lives in Beartype and is divided among two classes:

The meat is in _CachingProtocolMeta whereas the Protocol is basically a hack to work around some standard library implementation details and could likely be eliminated. At a high level, _CachingProtocolMeta overrides __instancecheck__ to perform an examination of whether an object "satisfies" a Protocol definition similar to typing._ProtocolMeta.__instancecheck__ and then caches the result by that object's type. Note the asymmetry: the examination is performed on the object, but it is assumed that all objects of the first-examined object's type are consistent. This makes the implementation ill-suited for runtime-composed "types" or monkey-patched objects.

Happy to discuss further, if desired.

@ilevkivskyi
Copy link
Member

Caching may need a more careful consideration (especially w.r.t. monkey-patching classes). I was specifically referring to this idea

class _ProtocolMeta(ABCMeta):
    # …
    def __instancecheck__(cls, instance):
        if super().__instancecheck__(instance):
            # Short circuit for direct inheritors
            return True
        else:
            # … existing implementation that checks method names, etc. …
            return False

@posita Do you have any good code base where you can benchmark this w.r.t. current implementation?

@posita
Copy link
Contributor

posita commented Oct 9, 2022

I can probably do something crude with numerary's and dyce's respective batteries of unit tests. Those are what motivated beartype.typing._typingpep544._CachingProtocolMeta in the first place. Give me a few days and I can probably throw something together.

@posita
Copy link
Contributor

posita commented Oct 11, 2022

Okay, this was a bit more involved than I thought it would be, but I have some rough numbers. My approach was to create a metaclass deriving from typing._ProtocolMeta but implementing its own __instancecheck__ method:

from typing import _ProtocolMeta as ProtocolMeta

class ProtocolMetaShortCircuit(ProtocolMeta):
    def __instancecheck__(cls, instance):
        return super(ProtocolMeta, cls).__instancecheck__(instance) or super(
            ProtocolMetaShortCircuit, cls
        ).__instancecheck__(instance)

This is used to create a special version of the big, scary (non-caching) SupportsLotsOfNumberStuff protocol borrowed from the numerary docs.

@runtime_checkable
class SupportsLotsOfNumberStuffShortCircuit(
    SupportsLotsOfNumberStuff,
    Protocol,
    metaclass=ProtocolMetaShortCircuit,
): pass

# ABC registration
assert isinstance(Fraction(), SupportsLotsOfNumberStuffShortCircuit)
SupportsLotsOfNumberStuffShortCircuit.register(Fraction)

# Direct derivation
class FractionShortCircuit(Fraction, SupportsLotsOfNumberStuffShortCircuit): pass

Initial results:

====
%timeit isinstance(1, SupportsLotsOfNumberStuff)
50 µs ± 616 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
%timeit isinstance(1, SupportsLotsOfNumberStuffShortCircuit)
54.4 µs ± 785 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
====
%timeit isinstance(2.0, SupportsLotsOfNumberStuff)
48.6 µs ± 464 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
%timeit isinstance(2.0, SupportsLotsOfNumberStuffShortCircuit)
51.3 µs ± 508 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
====
%timeit isinstance(Decimal('3'), SupportsLotsOfNumberStuff)
48.3 µs ± 288 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
%timeit isinstance(Decimal('3'), SupportsLotsOfNumberStuffShortCircuit)
52.1 µs ± 247 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
====
%timeit isinstance(Fraction(4, 1), SupportsLotsOfNumberStuff)
52 µs ± 318 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
%timeit isinstance(Fraction(4, 1), SupportsLotsOfNumberStuffShortCircuit)
26.1 µs ± 271 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
====
%timeit isinstance(FractionShortCircuit(5, 1), SupportsLotsOfNumberStuff)
52.7 µs ± 346 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
%timeit isinstance(FractionShortCircuit(5, 1), SupportsLotsOfNumberStuffShortCircuit)
262 ns ± 3.02 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

For built-in types, the short-circuit implementation imposes a small overhead of ~300 ns on my machine (which could likely be reduced), and is about twice as fast¹ for ABCMeta.registered types, but (as anticipated) is way faster for types that explicitly derive from the short-circuiting protocol.

You can play around with the notebook in Binder: Binder 👈 [might take awhile to load]

I tinkered with using this metaclass in my battery of unit tests for numerary and dyce. Anecdotally, I didn't notice much of a difference. I think this is largely because those tests rarely hit cases where this approach is beneficial (even fewer than I had anticipated).


¹ The registry check is likely O(1) due to caching (if I'm reading that implementation right).

AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Mar 30, 2023
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Mar 30, 2023
…` twice in `_ProtocolMeta.__instancecheck__`
AlexWaygood added a commit that referenced this issue Mar 31, 2023
…e in `_ProtocolMeta.__instancecheck__` (#103141)

Speed up `isinstance()` calls against runtime-checkable protocols
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Mar 31, 2023
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Mar 31, 2023
AlexWaygood added a commit that referenced this issue Mar 31, 2023
Improve performance of `isinstance()` checks against runtime-checkable protocols
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Mar 31, 2023
warsaw pushed a commit to warsaw/cpython that referenced this issue Apr 11, 2023
…in 3.12 (python#103348)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
AlexWaygood added a commit that referenced this issue May 18, 2023
…asses of protocols (#104622)

Don't set special protocol attributes on non-protocol subclasses of protocols
carljm added a commit to carljm/cpython that referenced this issue May 18, 2023
* main:
  pythongh-74690: Don't set special protocol attributes on non-protocol subclasses of protocols (python#104622)
  pythongh-104623: Update Windows installer to use SQLite 3.42.0 (python#104625)
  pythongh-104050: Add more type annotations to Argument Clinic (python#104628)
  pythongh-104629: Don't skip test_clinic if _testclinic is missing (python#104630)
  pythongh-104549: Set __module__ on TypeAliasType (python#104550)
  pythongh-104050: Improve some typing around `default`s and sentinel values (python#104626)
  pythongh-104146: Remove unused vars from Argument Clinic (python#104627)
  pythongh-104615: don't make unsafe swaps in apply_static_swaps (python#104620)
  pythonGH-104484: Add case_sensitive argument to `pathlib.PurePath.match()` (pythonGH-104565)
  pythonGH-96803: Document and test new unstable internal frame API functions (pythonGH-104211)
  pythonGH-104580: Don't cache eval breaker in interpreter (pythonGH-104581)
kraj pushed a commit to YoeDistro/poky that referenced this issue Jun 1, 2023
Changelog:
============
-  Fix use of @deprecated on classes with __new__ but no __init__.
-  Fix regression in version 4.6.1 where comparing a generic class against a
   runtime-checkable protocol using isinstance() would cause AttributeError to
   be raised if using Python 3.7.
-  Change deprecated @runtime to formal API @runtime_checkable in the error message.
-  Fix regression in 4.6.0 where attempting to define a Protocol that was generic
   over a ParamSpec or a TypeVarTuple would cause TypeError to be raised.
-  typing_extensions is now documented at https://typing-extensions.readthedocs.io/en/latest/.
-  Add typing_extensions.Buffer, a marker class for buffer types, as proposed
   by PEP 688. Equivalent to collections.abc.Buffer in Python 3.12.
-  Backport two CPython PRs fixing various issues with typing.Literal:
   python/cpython#23294 and python/cpython#23383. Both CPython PRs were originally,
   and both were backported to Python >=3.9.1, but no earlier.
-  A side effect of one of the changes is that equality comparisons of Literal
   objects will now raise a TypeError if one of the Literal objects being compared
   has a mutable parameter. (Using mutable parameters with Literal is not
   supported by PEP 586 or by any major static type checkers.)
-  Literal is now reimplemented on all Python versions <= 3.10.0.
-  Backport CPython PR 26067, ensuring that isinstance() calls on protocols raise
   TypeError when the protocol is not decorated with @runtime_checkable.
-  Backport several significant performance improvements to runtime-checkable protocols
   that have been made in Python 3.12 (see python/cpython#74690 for details).
-  A side effect of one of the performance improvements is that the members of a
   runtime-checkable protocol are now considered "frozen" at runtime as soon as the
   class has been created. Monkey-patching attributes onto a runtime-checkable
   protocol will still work, but will have no impact on isinstance() checks comparing
   objects to the protocol. See "What's New in Python 3.12" for more details.
-  isinstance() checks against runtime-checkable protocols now use inspect.getattr_static()
   rather than hasattr() to lookup whether attributes exist (backporting python/cpython#103034).
-  Backport the ability to define __init__ methods on Protocol classes, a change
   made in Python 3.11 (originally implemented in python/cpython#31628
-  Speedup isinstance(3, typing_extensions.SupportsIndex) by >10x on Python <3.12.
-  Add typing_extensions versions of SupportsInt, SupportsFloat, SupportsComplex,
   SupportsBytes, SupportsAbs and SupportsRound. These have the same semantics as
   the versions from the typing module, but isinstance() checks against the
   typing_extensions versions are >10x faster at runtime on Python <3.12.
-  Add __orig_bases__ to non-generic TypedDicts, call-based TypedDicts, and call-based NamedTuples.
-  Add typing_extensions.get_original_bases, a backport of types.get_original_bases,
   introduced in Python 3.12 (CPython PR python/cpython#101827, originally
-  This function should always produce correct results when called on classes
   constructed using features from typing_extensions.
-  Constructing a call-based TypedDict using keyword arguments for the fields
   now causes a DeprecationWarning to be emitted. This matches the behaviour
   of typing.TypedDict on 3.11 and 3.12.
-  Backport the implementation of NewType from 3.10 (where it is implemented as
   a class rather than a function). This allows user-defined NewTypes to be pickled.
-  Fix tests and import on Python 3.12, where typing.TypeVar can no longer be subclassed.
-  Add typing_extensions.TypeAliasType, a backport of typing.TypeAliasType from PEP 695.
-  Backport changes to the repr of typing.Unpack that were made in order
   to implement PEP 692 (backport of python/cpython#104048).

(From OE-Core rev: 2b1d07c7deb4f0247765bc737fb11a1747143edf)

Signed-off-by: Wang Mingyu <wangmy@fujitsu.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
rpurdie pushed a commit to yoctoproject/poky that referenced this issue Jun 2, 2023
Changelog:
============
-  Fix use of @deprecated on classes with __new__ but no __init__.
-  Fix regression in version 4.6.1 where comparing a generic class against a
   runtime-checkable protocol using isinstance() would cause AttributeError to
   be raised if using Python 3.7.
-  Change deprecated @runtime to formal API @runtime_checkable in the error message.
-  Fix regression in 4.6.0 where attempting to define a Protocol that was generic
   over a ParamSpec or a TypeVarTuple would cause TypeError to be raised.
-  typing_extensions is now documented at https://typing-extensions.readthedocs.io/en/latest/.
-  Add typing_extensions.Buffer, a marker class for buffer types, as proposed
   by PEP 688. Equivalent to collections.abc.Buffer in Python 3.12.
-  Backport two CPython PRs fixing various issues with typing.Literal:
   python/cpython#23294 and python/cpython#23383. Both CPython PRs were originally,
   and both were backported to Python >=3.9.1, but no earlier.
-  A side effect of one of the changes is that equality comparisons of Literal
   objects will now raise a TypeError if one of the Literal objects being compared
   has a mutable parameter. (Using mutable parameters with Literal is not
   supported by PEP 586 or by any major static type checkers.)
-  Literal is now reimplemented on all Python versions <= 3.10.0.
-  Backport CPython PR 26067, ensuring that isinstance() calls on protocols raise
   TypeError when the protocol is not decorated with @runtime_checkable.
-  Backport several significant performance improvements to runtime-checkable protocols
   that have been made in Python 3.12 (see python/cpython#74690 for details).
-  A side effect of one of the performance improvements is that the members of a
   runtime-checkable protocol are now considered "frozen" at runtime as soon as the
   class has been created. Monkey-patching attributes onto a runtime-checkable
   protocol will still work, but will have no impact on isinstance() checks comparing
   objects to the protocol. See "What's New in Python 3.12" for more details.
-  isinstance() checks against runtime-checkable protocols now use inspect.getattr_static()
   rather than hasattr() to lookup whether attributes exist (backporting python/cpython#103034).
-  Backport the ability to define __init__ methods on Protocol classes, a change
   made in Python 3.11 (originally implemented in python/cpython#31628
-  Speedup isinstance(3, typing_extensions.SupportsIndex) by >10x on Python <3.12.
-  Add typing_extensions versions of SupportsInt, SupportsFloat, SupportsComplex,
   SupportsBytes, SupportsAbs and SupportsRound. These have the same semantics as
   the versions from the typing module, but isinstance() checks against the
   typing_extensions versions are >10x faster at runtime on Python <3.12.
-  Add __orig_bases__ to non-generic TypedDicts, call-based TypedDicts, and call-based NamedTuples.
-  Add typing_extensions.get_original_bases, a backport of types.get_original_bases,
   introduced in Python 3.12 (CPython PR python/cpython#101827, originally
-  This function should always produce correct results when called on classes
   constructed using features from typing_extensions.
-  Constructing a call-based TypedDict using keyword arguments for the fields
   now causes a DeprecationWarning to be emitted. This matches the behaviour
   of typing.TypedDict on 3.11 and 3.12.
-  Backport the implementation of NewType from 3.10 (where it is implemented as
   a class rather than a function). This allows user-defined NewTypes to be pickled.
-  Fix tests and import on Python 3.12, where typing.TypeVar can no longer be subclassed.
-  Add typing_extensions.TypeAliasType, a backport of typing.TypeAliasType from PEP 695.
-  Backport changes to the repr of typing.Unpack that were made in order
   to implement PEP 692 (backport of python/cpython#104048).

(From OE-Core rev: a37154b9166323d05cca970ebb37bee0d5250893)

Signed-off-by: Wang Mingyu <wangmy@fujitsu.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
halstead pushed a commit to openembedded/openembedded-core that referenced this issue Jun 2, 2023
Changelog:
============
-  Fix use of @deprecated on classes with __new__ but no __init__.
-  Fix regression in version 4.6.1 where comparing a generic class against a
   runtime-checkable protocol using isinstance() would cause AttributeError to
   be raised if using Python 3.7.
-  Change deprecated @runtime to formal API @runtime_checkable in the error message.
-  Fix regression in 4.6.0 where attempting to define a Protocol that was generic
   over a ParamSpec or a TypeVarTuple would cause TypeError to be raised.
-  typing_extensions is now documented at https://typing-extensions.readthedocs.io/en/latest/.
-  Add typing_extensions.Buffer, a marker class for buffer types, as proposed
   by PEP 688. Equivalent to collections.abc.Buffer in Python 3.12.
-  Backport two CPython PRs fixing various issues with typing.Literal:
   python/cpython#23294 and python/cpython#23383. Both CPython PRs were originally,
   and both were backported to Python >=3.9.1, but no earlier.
-  A side effect of one of the changes is that equality comparisons of Literal
   objects will now raise a TypeError if one of the Literal objects being compared
   has a mutable parameter. (Using mutable parameters with Literal is not
   supported by PEP 586 or by any major static type checkers.)
-  Literal is now reimplemented on all Python versions <= 3.10.0.
-  Backport CPython PR 26067, ensuring that isinstance() calls on protocols raise
   TypeError when the protocol is not decorated with @runtime_checkable.
-  Backport several significant performance improvements to runtime-checkable protocols
   that have been made in Python 3.12 (see python/cpython#74690 for details).
-  A side effect of one of the performance improvements is that the members of a
   runtime-checkable protocol are now considered "frozen" at runtime as soon as the
   class has been created. Monkey-patching attributes onto a runtime-checkable
   protocol will still work, but will have no impact on isinstance() checks comparing
   objects to the protocol. See "What's New in Python 3.12" for more details.
-  isinstance() checks against runtime-checkable protocols now use inspect.getattr_static()
   rather than hasattr() to lookup whether attributes exist (backporting python/cpython#103034).
-  Backport the ability to define __init__ methods on Protocol classes, a change
   made in Python 3.11 (originally implemented in python/cpython#31628
-  Speedup isinstance(3, typing_extensions.SupportsIndex) by >10x on Python <3.12.
-  Add typing_extensions versions of SupportsInt, SupportsFloat, SupportsComplex,
   SupportsBytes, SupportsAbs and SupportsRound. These have the same semantics as
   the versions from the typing module, but isinstance() checks against the
   typing_extensions versions are >10x faster at runtime on Python <3.12.
-  Add __orig_bases__ to non-generic TypedDicts, call-based TypedDicts, and call-based NamedTuples.
-  Add typing_extensions.get_original_bases, a backport of types.get_original_bases,
   introduced in Python 3.12 (CPython PR python/cpython#101827, originally
-  This function should always produce correct results when called on classes
   constructed using features from typing_extensions.
-  Constructing a call-based TypedDict using keyword arguments for the fields
   now causes a DeprecationWarning to be emitted. This matches the behaviour
   of typing.TypedDict on 3.11 and 3.12.
-  Backport the implementation of NewType from 3.10 (where it is implemented as
   a class rather than a function). This allows user-defined NewTypes to be pickled.
-  Fix tests and import on Python 3.12, where typing.TypeVar can no longer be subclassed.
-  Add typing_extensions.TypeAliasType, a backport of typing.TypeAliasType from PEP 695.
-  Backport changes to the repr of typing.Unpack that were made in order
   to implement PEP 692 (backport of python/cpython#104048).

Signed-off-by: Wang Mingyu <wangmy@fujitsu.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
renovate bot referenced this issue in allenporter/flux-local Jun 3, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [typing-extensions](https://github.com/python/typing_extensions)
([changelog](https://github.com/python/typing_extensions/blob/main/CHANGELOG.md))
| `==4.5.0` -> `==4.6.3` |
[![age](https://badges.renovateapi.com/packages/pypi/typing-extensions/4.6.3/age-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://badges.renovateapi.com/packages/pypi/typing-extensions/4.6.3/adoption-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://badges.renovateapi.com/packages/pypi/typing-extensions/4.6.3/compatibility-slim/4.5.0)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://badges.renovateapi.com/packages/pypi/typing-extensions/4.6.3/confidence-slim/4.5.0)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>python/typing_extensions</summary>

###
[`v4.6.3`](https://github.com/python/typing_extensions/blob/HEAD/CHANGELOG.md#Release-463-June-1-2023)

[Compare
Source](https://github.com/python/typing_extensions/compare/4.6.2...4.6.3)

-   Fix a regression introduced in v4.6.0 in the implementation of
    runtime-checkable protocols. The regression meant
that doing `class Foo(X, typing_extensions.Protocol)`, where `X` was a
class that
    had `abc.ABCMeta` as its metaclass, would then cause subsequent
    `isinstance(1, X)` calls to erroneously raise `TypeError`. Patch by
Alex Waygood (backporting the
CPython[https://github.com/python/cpython/pull/105152](https://github.com/python/cpython/pull/105152)l/105152).
-   Sync the repository's LICENSE file with that of CPython.
    `typing_extensions` is distributed under the same license as
    CPython itself.
- Skip a problematic test on Python 3.12.0b1. The test fails on 3.12.0b1
due to
    a bug in CPython, which will be fixed in 3.12.0b2. The
    `typing_extensions` test suite now passes on 3.12.0b1.

###
[`v4.6.2`](https://github.com/python/typing_extensions/blob/HEAD/CHANGELOG.md#Release-462-May-25-2023)

[Compare
Source](https://github.com/python/typing_extensions/compare/4.6.1...4.6.2)

- Fix use of `@deprecated` on classes with `__new__` but no `__init__`.
    Patch by Jelle Zijlstra.
- Fix regression in version 4.6.1 where comparing a generic class
against a
runtime-checkable protocol using `isinstance()` would cause
`AttributeError`
    to be raised if using Python 3.7.

###
[`v4.6.1`](https://github.com/python/typing_extensions/blob/HEAD/CHANGELOG.md#Release-461-May-23-2023)

[Compare
Source](https://github.com/python/typing_extensions/compare/4.6.0...4.6.1)

- Change deprecated `@runtime` to formal API `@runtime_checkable` in the
error
    message. Patch by Xuehai Pan.
- Fix regression in 4.6.0 where attempting to define a `Protocol` that
was
generic over a `ParamSpec` or a `TypeVarTuple` would cause `TypeError`
to be
    raised. Patch by Alex Waygood.

###
[`v4.6.0`](https://github.com/python/typing_extensions/blob/HEAD/CHANGELOG.md#Release-460-May-22-2023)

[Compare
Source](https://github.com/python/typing_extensions/compare/4.5.0...4.6.0)

-   `typing_extensions` is now documented at
https://typing-extensions.readthedocs.io/en/latest/. Patch by Jelle
Zijlstra.
- Add `typing_extensions.Buffer`, a marker class for buffer types, as
proposed
by PEP 688. Equivalent to `collections.abc.Buffer` in Python 3.12. Patch
by
    Jelle Zijlstra.
- Backport two CPython PRs fixing various issues with `typing.Literal`:

[https://github.com/python/cpython/pull/23294](https://github.com/python/cpython/pull/23294)3294
[https://github.com/python/cpython/pull/23383](https://github.com/python/cpython/pull/23383)ll/23383.
Both CPython PRs were
originally by Yurii Karabas, and both were backported to Python >=3.9.1,
but
    no earlier. Patch by Alex Waygood.

A side effect of one of the changes is that equality comparisons of
`Literal`
objects will now raise a `TypeError` if one of the `Literal` objects
being
compared has a mutable parameter. (Using mutable parameters with
`Literal` is
    not supported by PEP 586 or by any major static type checkers.)
-   `Literal` is now reimplemented on all Python versions <= 3.10.0. The
`typing_extensions` version does not suffer from the bug that was fixed
in

[https://github.com/python/cpython/pull/29334](https://github.com/python/cpython/pull/29334)9334.
(The CPython bugfix was
    backported to CPython 3.10.1 and 3.9.8, but no earlier.)
- Backport [CPython PR
26067](https://github.com/python/cpython/pull/26067)
    (originally by Yurii Karabas), ensuring that `isinstance()` calls on
    protocols raise `TypeError` when the protocol is not decorated with
    `@runtime_checkable`. Patch by Alex Waygood.
- Backport several significant performance improvements to
runtime-checkable
protocols that have been made in Python 3.12
([https://github.com/python/cpython/issues/74690](https://github.com/python/cpython/issues/74690)es/74690
for details). Patch by Alex
    Waygood.

A side effect of one of the performance improvements is that the members
of
a runtime-checkable protocol are now considered “frozen” at runtime as
soon
    as the class has been created. Monkey-patching attributes onto a
runtime-checkable protocol will still work, but will have no impact on
    `isinstance()` checks comparing objects to the protocol. See
["What's New in Python
3.12"](https://docs.python.org/3.12/whatsnew/3.12.html#typing)
    for more details.
-   `isinstance()` checks against runtime-checkable protocols now use
    `inspect.getattr_static()` rather than `hasattr()` to lookup whether
attributes exist
(backport[https://github.com/python/cpython/pull/103034](https://github.com/python/cpython/pull/103034)3034).
    This means that descriptors and `__getattr__` methods are no longer
unexpectedly evaluated during `isinstance()` checks against
runtime-checkable
protocols. However, it may also mean that some objects which used to be
considered instances of a runtime-checkable protocol on older versions
of
`typing_extensions` may no longer be considered instances of that
protocol
using the new release, and vice versa. Most users are unlikely to be
affected
    by this change. Patch by Alex Waygood.
- Backport the ability to define `__init__` methods on Protocol classes,
a
change made in Python 3.11 (originally
implemented[https://github.com/python/cpython/pull/31628](https://github.com/python/cpython/pull/31628)ll/31628
by Adrian Garcia Badaracco).
    Patch by Alex Waygood.
- Speedup `isinstance(3, typing_extensions.SupportsIndex)` by >10x on
Python
    <3.12. Patch by Alex Waygood.
-   Add `typing_extensions` versions of `SupportsInt`, `SupportsFloat`,
`SupportsComplex`, `SupportsBytes`, `SupportsAbs` and `SupportsRound`.
These
have the same semantics as the versions from the `typing` module, but
`isinstance()` checks against the `typing_extensions` versions are >10x
faster
    at runtime on Python <3.12. Patch by Alex Waygood.
- Add `__orig_bases__` to non-generic TypedDicts, call-based TypedDicts,
and
call-based NamedTuples. Other TypedDicts and NamedTuples already had the
attribute.
    Patch by Adrian Garcia Badaracco.
-   Add `typing_extensions.get_original_bases`, a backport of

[`types.get_original_bases`](https://docs.python.org/3.12/library/types.html#types.get_original_bases),
introduced in Python 3.12
(CPython[https://github.com/python/cpython/pull/101827](https://github.com/python/cpython/pull/101827)l/101827,
originally by James
    Hilton-Balfe). Patch by Alex Waygood.

This function should always produce correct results when called on
classes
    constructed using features from `typing_extensions`. However, it may
produce incorrect results when called on some `NamedTuple` or
`TypedDict`
    classes that use `typing.{NamedTuple,TypedDict}` on Python <=3.11.
- Constructing a call-based `TypedDict` using keyword arguments for the
fields
now causes a `DeprecationWarning` to be emitted. This matches the
behaviour
    of `typing.TypedDict` on 3.11 and 3.12.
- Backport the implementation of `NewType` from 3.10 (where it is
implemented
as a class rather than a function). This allows user-defined `NewType`s
to be
    pickled. Patch by Alex Waygood.
- Fix tests and import on Python 3.12, where `typing.TypeVar` can no
longer be
    subclassed. Patch by Jelle Zijlstra.
- Add `typing_extensions.TypeAliasType`, a backport of
`typing.TypeAliasType`
    from PEP 695. Patch by Jelle Zijlstra.
- Backport changes to the repr of `typing.Unpack` that were made in
order to
    implement [PEP 692](https://peps.python.org/pep-0692/) (backport of

[https://github.com/python/cpython/pull/104048](https://github.com/python/cpython/pull/104048)4048).
Patch by Alex Waygood.

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/allenporter/flux-local).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4xMDUuMiIsInVwZGF0ZWRJblZlciI6IjM1LjEwNS4yIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Dec 4, 2023
…nst runtime-checkable protocols by avoiding costly `super()` calls
AlexWaygood added a commit that referenced this issue Dec 4, 2023
…ntime-checkable protocols by avoiding costly `super()` calls (#112708)
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Dec 4, 2023
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
…nst runtime-checkable protocols by avoiding costly `super()` calls (python#112708)
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
daregit pushed a commit to daregit/yocto-combined that referenced this issue May 22, 2024
Changelog:
============
-  Fix use of @deprecated on classes with __new__ but no __init__.
-  Fix regression in version 4.6.1 where comparing a generic class against a
   runtime-checkable protocol using isinstance() would cause AttributeError to
   be raised if using Python 3.7.
-  Change deprecated @runtime to formal API @runtime_checkable in the error message.
-  Fix regression in 4.6.0 where attempting to define a Protocol that was generic
   over a ParamSpec or a TypeVarTuple would cause TypeError to be raised.
-  typing_extensions is now documented at https://typing-extensions.readthedocs.io/en/latest/.
-  Add typing_extensions.Buffer, a marker class for buffer types, as proposed
   by PEP 688. Equivalent to collections.abc.Buffer in Python 3.12.
-  Backport two CPython PRs fixing various issues with typing.Literal:
   python/cpython#23294 and python/cpython#23383. Both CPython PRs were originally,
   and both were backported to Python >=3.9.1, but no earlier.
-  A side effect of one of the changes is that equality comparisons of Literal
   objects will now raise a TypeError if one of the Literal objects being compared
   has a mutable parameter. (Using mutable parameters with Literal is not
   supported by PEP 586 or by any major static type checkers.)
-  Literal is now reimplemented on all Python versions <= 3.10.0.
-  Backport CPython PR 26067, ensuring that isinstance() calls on protocols raise
   TypeError when the protocol is not decorated with @runtime_checkable.
-  Backport several significant performance improvements to runtime-checkable protocols
   that have been made in Python 3.12 (see python/cpython#74690 for details).
-  A side effect of one of the performance improvements is that the members of a
   runtime-checkable protocol are now considered "frozen" at runtime as soon as the
   class has been created. Monkey-patching attributes onto a runtime-checkable
   protocol will still work, but will have no impact on isinstance() checks comparing
   objects to the protocol. See "What's New in Python 3.12" for more details.
-  isinstance() checks against runtime-checkable protocols now use inspect.getattr_static()
   rather than hasattr() to lookup whether attributes exist (backporting python/cpython#103034).
-  Backport the ability to define __init__ methods on Protocol classes, a change
   made in Python 3.11 (originally implemented in python/cpython#31628
-  Speedup isinstance(3, typing_extensions.SupportsIndex) by >10x on Python <3.12.
-  Add typing_extensions versions of SupportsInt, SupportsFloat, SupportsComplex,
   SupportsBytes, SupportsAbs and SupportsRound. These have the same semantics as
   the versions from the typing module, but isinstance() checks against the
   typing_extensions versions are >10x faster at runtime on Python <3.12.
-  Add __orig_bases__ to non-generic TypedDicts, call-based TypedDicts, and call-based NamedTuples.
-  Add typing_extensions.get_original_bases, a backport of types.get_original_bases,
   introduced in Python 3.12 (CPython PR python/cpython#101827, originally
-  This function should always produce correct results when called on classes
   constructed using features from typing_extensions.
-  Constructing a call-based TypedDict using keyword arguments for the fields
   now causes a DeprecationWarning to be emitted. This matches the behaviour
   of typing.TypedDict on 3.11 and 3.12.
-  Backport the implementation of NewType from 3.10 (where it is implemented as
   a class rather than a function). This allows user-defined NewTypes to be pickled.
-  Fix tests and import on Python 3.12, where typing.TypeVar can no longer be subclassed.
-  Add typing_extensions.TypeAliasType, a backport of typing.TypeAliasType from PEP 695.
-  Backport changes to the repr of typing.Unpack that were made in order
   to implement PEP 692 (backport of python/cpython#104048).

(From OE-Core rev: a37154b9166323d05cca970ebb37bee0d5250893)

Signed-off-by: Wang Mingyu <wangmy@fujitsu.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
@namka279

This comment was marked as spam.

Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
…nst runtime-checkable protocols by avoiding costly `super()` calls (python#112708)
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes performance Performance or resource usage stdlib Python modules in the Lib dir topic-typing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants