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

Enum with str or int Mixin Breaking Change in Python 3.11 #100458

Closed
anze3db opened this issue Dec 23, 2022 · 6 comments · Fixed by batfish/pybatfish#902
Closed

Enum with str or int Mixin Breaking Change in Python 3.11 #100458

anze3db opened this issue Dec 23, 2022 · 6 comments · Fixed by batfish/pybatfish#902
Assignees
Labels
3.11 only security fixes docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@anze3db
Copy link
Contributor

anze3db commented Dec 23, 2022

Bug report

Looks like there was a breaking change with the way str and int mixins work with Enums in Python 3.11:

from enum import Enum


class Foo(str, Enum):
    BAR = "bar"

# Python 3.10
f"{Foo.BAR}"  # > bar

# Python 3.11
f"{Foo.BAR}"  # > Foo.BAR

The same goes for Enum classes with the int mixin.

In my project we were relying on Foo.BAR to return the enum value, so this change broke our code. We fixed it by replacing str Enum mixin with the newly added StrEnum class (thanks for that, it's exactly what we needed!).

I think reverting the breaking change would only introduce another breaking change so that's probably not the way to go. But maybe updating the whatsnew page and call out the change there could help people stumbling into this when doing the upgrade. I've found the existing point about this change in the release notes a little bit confusing and I have already opened a PR to try and clear it up a bit: #100387

I've also written a longer blog post about it here, and there has been some lively discussion in r/python.

Your environment

  • CPython versions tested on: 3.11.0, 3.11.1
  • Operating system and architecture: MacOS, Ubuntu 22.04

Linked PRs

@anze3db anze3db added the type-bug An unexpected behavior, bug, or error label Dec 23, 2022
@AlexWaygood AlexWaygood added docs Documentation in the Doc dir 3.11 only security fixes labels Dec 23, 2022
@JosephSBoyle
Copy link
Contributor

JosephSBoyle commented Dec 25, 2022

@anze3db StrEnum was added in 3.11, does that work as a 'slot in' replacement for your use case?

docs: https://docs.python.org/3.11/library/enum.html#enum.StrEnum

@anze3db
Copy link
Contributor Author

anze3db commented Dec 27, 2022

@JosephSBoyle yes, we replaced the str mixin with StrEnum to fix this on our end. If we could let people know about this breaking change in the what's new docs it would save me a lot of head scratching, especially since the current note about this change seems wrong (see my attempt of at least making it a bit more accurate: #100387)

@JosephSBoyle
Copy link
Contributor

Coincidentally I've also used the mixin approach you describe in professional projects, which suggests that this use is at the very least not uncommon.

As such it makes sense that the docs also suggest a way to achieve the old behaviour, like you say @anze3db. I've added a small suggestion to your PR; hopefully that will help resolve things for others in the same position!

@anze3db
Copy link
Contributor Author

anze3db commented Dec 27, 2022

Thank you! The text that you added would have helped me a lot when I was figuring out how to fix this 👍

ethanfurman added a commit that referenced this issue May 1, 2023
…whatsnew/3.11.rst (GH-100387)

Co-authored-by: JosephSBoyle <48555120+JosephSBoyle@users.noreply.github.com>
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 1, 2023
…n the whatsnew/3.11.rst (pythonGH-100387)

(cherry picked from commit e665563)

Co-authored-by: Anže Pečar <anze@pecar.me>
Co-authored-by: JosephSBoyle <48555120+JosephSBoyle@users.noreply.github.com>
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
ethanfurman added a commit that referenced this issue May 1, 2023
…in the whatsnew/3.11.rst (GH-100387) (GH-104060)

Co-authored-by: Anže Pečar <anze@pecar.me>
Co-authored-by: JosephSBoyle <48555120+JosephSBoyle@users.noreply.github.com>
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
carljm added a commit to carljm/cpython that referenced this issue May 1, 2023
* main: (463 commits)
  pythongh-104057: Fix direct invocation of test_super (python#104064)
  pythongh-87092: Expose assembler to unit tests (python#103988)
  pythongh-97696: asyncio eager tasks factory (python#102853)
  pythongh-84436: Immortalize in _PyStructSequence_InitBuiltinWithFlags() (pythongh-104054)
  pythongh-104057: Fix direct invocation of test_module (pythonGH-104059)
  pythongh-100458: Clarify Enum.__format__() change of mixed-in types in the whatsnew/3.11.rst (pythonGH-100387)
  pythongh-104018: disallow "z" format specifier in %-format of byte strings (pythonGH-104033)
  pythongh-104016: Fixed off by 1 error in f string tokenizer (python#104047)
  pythonGH-103629: Update Unpack's repr in compliance with PEP 692 (python#104048)
  pythongh-102799: replace sys.exc_info by sys.exception in inspect and traceback modules (python#104032)
  Fix typo in "expected" word in few source files (python#104034)
  pythongh-103824: fix use-after-free error in Parser/tokenizer.c (python#103993)
  pythongh-104035: Do not ignore user-defined `__{get,set}state__` in slotted frozen dataclasses (python#104041)
  pythongh-104028: Reduce object creation while calling callback function from gc (pythongh-104030)
  pythongh-104036: Fix direct invocation of test_typing (python#104037)
  pythongh-102213: Optimize the performance of `__getattr__` (pythonGH-103761)
  pythongh-103895: Improve how invalid `Exception.__notes__` are displayed (python#103897)
  Adjust expression from `==` to `!=` in alignment with the meaning of the paragraph. (pythonGH-104021)
  pythongh-88496: Fix IDLE test hang on macOS (python#104025)
  Improve int test coverage (python#104024)
  ...
gordonhart added a commit to kolenaIO/kolena that referenced this issue May 10, 2023
…m.KEY, which works on Python<3.11 but broke in 3.11 (see: python/cpython#100458)
gordonhart added a commit to kolenaIO/kolena that referenced this issue May 11, 2023
* Pin transient urllib3 dependency to unbreak requests==2.30

* Pin to requests-toolbelt>=1 instead of pinning transient urllib3

* Add back importlib-metadata dependency

* Allow Python 3.11 and add version to CI matrix

* Pin urllib3 per requests issue recommendation, leave TODO

* Pin transient urllib3 dependency to unbreak requests==2.30

* Pin to requests-toolbelt>=1 instead of pinning transient urllib3

* Add back importlib-metadata dependency

* Tediously update all enum usages to use Enum.KEY.value instead of Enum.KEY, which works on Python<3.11 but broke in 3.11 (see: python/cpython#100458)

* Move FR-specific upload_image_chips to kolena.fr._utils; remove kolena.fr._consts; rename some already-hidden classes to remove leading underscore (already in underscored files)

* Revert {_=>}BatchedLoader diff, this PR is not the place for that

* Start updating integration tests with new description edit semantics

* Add missing pytest-depends dev dependency

* Fix FR test__edit__reset

* Assert description update without version update

* Unbreak upload_image_chips after move

* Unbreak FR test suite test__load by comparing data to data

* Remove bad comparison
@jkbrzt
Copy link

jkbrzt commented Jun 5, 2023

Also, str(HTTPStatus.OK) now returns '200' instead of 'HTTPStatus.OK'. Workaround:

from enum import Enum
from http import HTTPStatus

HTTPStatus.__str__ = Enum.__str__

maerzd added a commit to kraussmaffei/graphql-codegen-pydantic that referenced this issue Aug 30, 2023
BREAKING CHANGE: class Type(str, Enum) --> class Type(StrEnum)
python/cpython#100458
maerzd added a commit to kraussmaffei/graphql-codegen-pydantic that referenced this issue Aug 30, 2023
BREAKING CHANGE: class Type(str, Enum) --> class Type(StrEnum)
python/cpython#100458
@hauntsaninja
Copy link
Contributor

Improving the What's New documentation was a good call, thank you! While I also found this needlessly breaking when updating my work codebase, I don't think there's anything left to be done here.

dhalperi added a commit to batfish/pybatfish that referenced this issue Dec 16, 2023
Fix python/cpython#100458

Fix a pandas incompatibility

commit-id:f0b690de
dhalperi added a commit to batfish/pybatfish that referenced this issue Dec 16, 2023
Fix python/cpython#100458

Fix a pandas incompatibility

commit-id:f0b690de
sanjacob added a commit to sanjacob/bblearn that referenced this issue Jan 8, 2024
alangenfeld added a commit to dagster-io/dagster that referenced this issue Jan 9, 2024
in 3.11 these f strings change for the enum
python/cpython#100458

so just do `.value`

## How I Tested These Changes

bk passes
alangenfeld added a commit to dagster-io/dagster that referenced this issue Jan 19, 2024
another place effected by
python/cpython#100458

the string `<GraphQLWS.PROTOCOL: 'graphql-ws'>` was being sent instead
of `graphql-ws`

## How I Tested These Changes

added assert to test that would have caught this
PedramNavid pushed a commit to dagster-io/dagster that referenced this issue Jan 26, 2024
another place effected by
python/cpython#100458

the string `<GraphQLWS.PROTOCOL: 'graphql-ws'>` was being sent instead
of `graphql-ws`

## How I Tested These Changes

added assert to test that would have caught this
lqc added a commit to syncron-oss/mage-ai that referenced this issue Apr 25, 2024
lqc pushed a commit to syncron-oss/mage-ai that referenced this issue Apr 25, 2024
Changes made:

* Replace usage of Enum plus str mixin with StrEnum, as per
  python/cpython#100458
lqc pushed a commit to syncron-oss/mage-ai that referenced this issue Apr 25, 2024
Changes made:

* Replace usage of Enum plus str mixin with StrEnum, as per
  python/cpython#100458

* Sync "typing_extensions" version between requirements.txt and pyproject.toml
lqc pushed a commit to syncron-oss/mage-ai that referenced this issue Apr 26, 2024
Changes made:

* Replace usage of Enum plus str mixin with StrEnum, as per
  python/cpython#100458

* Sync "typing_extensions" version between requirements.txt and pyproject.toml
DLT1412 added a commit to DLT1412/airbyte that referenced this issue Jun 19, 2024
jambayk pushed a commit to microsoft/Olive that referenced this issue Aug 9, 2024
## Describe your changes

python/cpython#100458
Quarot require python>=3.11 where the mixin usage of (str, Enum) did not
work. This PR is used to create olive strEnum based on python version.

## Checklist before requesting a review
- [ ] Add unit tests for this change.
- [ ] Make sure all tests can pass.
- [ ] Update documents if necessary.
- [ ] Lint and apply fixes to your code by running `lintrunner -a`
- [ ] Is this a user-facing change? If yes, give a description of this
change to be included in the release notes.
- [ ] Is this PR including examples changes? If yes, please remember to
update [example
documentation](https://github.com/microsoft/Olive/blob/main/docs/source/examples.md)
in a follow-up PR.

## (Optional) Issue link
DavitGrigoryan132 pushed a commit to DavitGrigoryan132/Olive that referenced this issue Aug 14, 2024
## Describe your changes

python/cpython#100458
Quarot require python>=3.11 where the mixin usage of (str, Enum) did not
work. This PR is used to create olive strEnum based on python version.

## Checklist before requesting a review
- [ ] Add unit tests for this change.
- [ ] Make sure all tests can pass.
- [ ] Update documents if necessary.
- [ ] Lint and apply fixes to your code by running `lintrunner -a`
- [ ] Is this a user-facing change? If yes, give a description of this
change to be included in the release notes.
- [ ] Is this PR including examples changes? If yes, please remember to
update [example
documentation](https://github.com/microsoft/Olive/blob/main/docs/source/examples.md)
in a follow-up PR.

## (Optional) Issue link
giancarloromeo added a commit to giancarloromeo/osparc-simcore that referenced this issue Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants