Skip to content

bpo-44242: [Enum] remove missing bits test from Flag creation #26586

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

Merged
merged 4 commits into from
Jun 9, 2021

Conversation

ethanfurman
Copy link
Member

@ethanfurman ethanfurman commented Jun 7, 2021

Move the check for missing named flags in composite flag aliases from
Flag creation to a new verify decorator.

https://bugs.python.org/issue44242

Move the check for missing named flags in composite flag aliases from
Flag creation to a new *verify* decorator.
*EnumCheck* contains the options used by the :func:`verify` decorator to ensure
various constraints; failed constraints result in a :exc:`TypeError`.

.. attribute:: UNIQUE
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any guidance on when to use @unique vs @verify(UNIQUE)?

Copy link
Member Author

Choose a reason for hiding this comment

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

The only difference between the two is unique raises ValueError and verify raises TypeError. I don't see unique going away, so it's pretty much personal preference.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed that so both raise ValueError. I added UNIQUE to verify() for two reasons:

  • so multiple checks would not require multiple decorators
  • so unique() could eventually go away (not that it will any time soon)

change COMPOSITE to NAMED_FLAGS
change TypeError to ValueError
@ethanfurman ethanfurman merged commit eea8148 into python:main Jun 9, 2021
@ethanfurman ethanfurman self-assigned this Jun 9, 2021
@ethanfurman ethanfurman added the needs backport to 3.10 only security fixes label Jun 9, 2021
@miss-islington
Copy link
Contributor

Thanks @ethanfurman for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @ethanfurman, I could not cleanly backport this to 3.10 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker eea8148b7dff5ffc7b84433859ac819b1d92a74d 3.10

raise Exception('verify: unknown type %r' % enum_type)
if missing:
raise ValueError('invalid %s %r: missing values %s' % (
enum_type, cls_name, ', '.join((str(m) for m in missing)))
Copy link
Contributor

@akulakov akulakov Jun 10, 2021

Choose a reason for hiding this comment

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

Would it make sense to limit missing to missing[:100] or so? It can be arbitrarily large and it's not obvious to users that a large flag value can create a very large exception (and log msg) here. [edit: should have reloaded, didn't realize it's already merged]

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it would.

Copy link
Contributor

Choose a reason for hiding this comment

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

I can make a PR on the same issue, if that's okay?

Copy link
Member Author

Choose a reason for hiding this comment

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

[I merged it after your comment, as it fixes a regression; we can still make your change if needed.]

Having thought about it a little more, I'm not sure we need that: For any static enum, and probably most dynamic ones, the exception will happen the first time the .py file is loaded and not at some random time on the end-users machine. Which specific scenarios were you thinking about, and is your experience different?

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. For some subset of dynamic Enums where let's say a developer tests it with small values and the app logic logs an error and proceeds. In actual use the dynamic value might be calculated or loaded from somewhere and might be much larger.
  2. If a value is calculated or loaded from a web request, it can be used as a DoS attack or to overfill the log files.

Both seem pretty unlikely so I'm not sure it's a needed fix, just wanted to point it out.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good points. I changed the error message to list the problem aliases and the combined value of missing flags, then limited that to 256 characters.

Thank you for your help!

ethanfurman added a commit that referenced this pull request Jun 10, 2021
…GH-26586)

Move the check for missing named flags in flag aliases from Flag creation
to a new *verify* decorator..
                    (cherry picked from commit eea8148)

                    Co-authored-by: Ethan Furman <[email protected]>
@bedevere-bot
Copy link

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

@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label Jun 10, 2021
ethanfurman added a commit that referenced this pull request Jun 10, 2021
…H-26586) (GH-26635)

Move the check for missing named flags in flag aliases from Flag creation
to a new *verify* decorator..

(cherry picked from commit eea8148)

Co-authored-by: Ethan Furman <[email protected]>
@akulakov
Copy link
Contributor

akulakov commented Jun 10, 2021 via email

@ethanfurman ethanfurman deleted the enum-decorator branch October 21, 2021 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants