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

bpo-45679: Fix caching of multi-value typing.Literal #29334

Merged

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Oct 30, 2021

Literal[True, 2] is no longer equal to Literal[1, 2].

https://bugs.python.org/issue45679

Literal[True, 2] is no longer equal to Literal[1, 2].
Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks Serhiy.

Comment on lines +4968 to +4969
typing.Literal[1, 2]: 'Literal',
typing.Literal[True, 2]: 'Literal',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if these tests for anything useful? It should still pass even without the fix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it passes without the fix because due to the bug typing.Literal[True, 2] and typing.Literal[1, 2] are the same.

But it fails with the PR originally proposed by @sobolevn. Some tests are added to catch possible similar errors.

@miss-islington
Copy link
Contributor

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9, 3.10.
🐍🍒⛏🤖

@serhiy-storchaka serhiy-storchaka deleted the typing-literal-caching branch October 31, 2021 08:22
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 31, 2021
Literal[True, 2] is no longer equal to Literal[1, 2].
(cherry picked from commit 634984d)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@miss-islington
Copy link
Contributor

Sorry, @serhiy-storchaka, I could not cleanly backport this to 3.9 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 634984d7dbdd91e0a51a793eed4d870e139ae1e0 3.9

@bedevere-bot
Copy link

GH-29340 is a backport of this pull request to the 3.10 branch.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Oct 31, 2021
…29334)

Literal[True, 2] is no longer equal to Literal[1, 2]..
(cherry picked from commit 634984d)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@bedevere-bot
Copy link

GH-29342 is a backport of this pull request to the 3.9 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Oct 31, 2021
miss-islington added a commit that referenced this pull request Oct 31, 2021
Literal[True, 2] is no longer equal to Literal[1, 2].
(cherry picked from commit 634984d)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Fidget-Spinner pushed a commit that referenced this pull request Nov 3, 2021
…GH-29342)

Literal[True, 2] is no longer equal to Literal[1, 2]..
(cherry picked from commit 634984d)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@serhiy-storchaka serhiy-storchaka removed their assignment Jun 21, 2022
renovate bot added a commit to allenporter/flux-local that referenced this pull request 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://togithub.com/python/typing_extensions)
([changelog](https://togithub.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://togithub.com/python/typing_extensions/blob/HEAD/CHANGELOG.md#Release-463-June-1-2023)

[Compare
Source](https://togithub.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[python/cpython#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://togithub.com/python/typing_extensions/blob/HEAD/CHANGELOG.md#Release-462-May-25-2023)

[Compare
Source](https://togithub.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://togithub.com/python/typing_extensions/blob/HEAD/CHANGELOG.md#Release-461-May-23-2023)

[Compare
Source](https://togithub.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://togithub.com/python/typing_extensions/blob/HEAD/CHANGELOG.md#Release-460-May-22-2023)

[Compare
Source](https://togithub.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`:

[python/cpython#23294
[python/cpython#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

[python/cpython#29334.
(The CPython bugfix was
    backported to CPython 3.10.1 and 3.9.8, but no earlier.)
- Backport [CPython PR
26067](https://togithub.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
([python/cpython#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[python/cpython#103034).
    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[python/cpython#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[python/cpython#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

[python/cpython#104048).
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants