Union now uses the instance checks against its parameters instead of
the subclass checks.
(cherry picked from commit b2ac70a62a)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
gh-126699: allow AsyncIterator to be used as a base for Protocols (GH-126702)
(cherry picked from commit feb3e0b19c)
Co-authored-by: Stephen Morton <github@tungol.org>
Fix _PyArg_UnpackKeywordsWithVararg for the case when argument for
positional-or-keyword parameter is passed by keyword.
There was only one such case in the stdlib -- the TypeVar constructor.
(cherry picked from commit 540fcc62f5)
gh-116325: Raise `SyntaxError` rather than `IndexError` on ForwardRef with empty string arg (GH-116341)
(cherry picked from commit a29998a06b)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
gh-115165: Fix `typing.Annotated` for immutable types (GH-115213)
The return value from an annotated callable can raise any exception from
__setattr__ for the `__orig_class__` property.
(cherry picked from commit 564385612c)
Co-authored-by: dave-shawley <daveshawley@gmail.com>
- Only attempt to figure out whether protocol members are "method members" or not if the class is marked as a runtime protocol. This information is irrelevant for non-runtime protocols; we can safely skip the risky introspection for them.
- Only do the risky getattr() calls in one place (the runtime_checkable class decorator), rather than in three places (_ProtocolMeta.__init__, _ProtocolMeta.__instancecheck__ and _ProtocolMeta.__subclasscheck__). This reduces the number of locations in typing.py where the risky introspection could go wrong.
- For runtime protocols, if determining whether a protocol member is callable or not fails, give a better error message. I think it's reasonable for us to reject runtime protocols that have members which raise strange exceptions when you try to access them. PEP-544 clearly states that all protocol member must be callable for issubclass() calls against the protocol to be valid -- and if a member raises when we try to access it, there's no way for us to figure out whether it's a callable member or not!
(cherry-picked from commit ed6ea3ea79)
gh-111126: Use `isinstance` instead of `assert[Not]IsInstance` in `test_typing` (GH-111127)
(cherry picked from commit ea7c26e4b8)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
gh-110782: Fix crash when TypeVar is constructed with keyword args (GH-110784)
(cherry picked from commit d2a536b170)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
gh-110178: Use fewer weakrefs in test_typing.py (GH-110194)
Confirmed that without the C changes from GH-108517, this test still segfaults with only 10 weakrefs.
(cherry picked from commit 732ad44cec)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
gh-105974: Revert unintentional behaviour change for protocols with non-callable members and custom `__subclasshook__` methods (GH-105976)
(cherry picked from commit 9499b0f138)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Some parts of the implementation of `typing.Protocol` had poor test coverage
(cherry picked from commit 70c075c194)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Forward-port of the tests that were added to the 3.11 branch in GH-105445
(cherry picked from commit f5df347fcf)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Improve test coverage for is_typeddict (GH-104884)
In particular, it's important to test that is_typeddict(TypedDict)
returns False.
(cherry picked from commit 1497607a8e)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
During the PEP 695 implementation at one point I made
TypeVar.__name__ return garbage, and all of test_typing passed.
So I decided to add a few more tests. In the process I discovered
a minor incompatibility from the C implementation of TypeVar:
empty constraints were returned as None instead of an empty tuple.
This implements PEP 695, Type Parameter Syntax. It adds support for:
- Generic functions (def func[T](): ...)
- Generic classes (class X[T](): ...)
- Type aliases (type X = ...)
- New scoping when the new syntax is used within a class body
- Compiler and interpreter changes to support the new syntax and scoping rules
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Co-authored-by: Eric Traut <eric@traut.com>
Co-authored-by: Larry Hastings <larry@hastings.org>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>