-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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.nonmember
type-decays Flag values
#120361
Comments
I don't see this functionality tested one way or another in |
This does not look like a bug to me. It does what is advertised: Because >>> class DepType(Flag):
... LIBRARY = auto()
... TEST = auto()
... print(LIBRARY, TEST, LIBRARY | TEST)
...
1 2 3 So, I recommend doing something like: >>> DepType.LIBRARY in DepType
True This won't require to define But, I agree that adding a test case for this corner case might be a good idea. |
Oh duh!
The real-world scenario is a bit more hoop-y: def foo(dep_type: DepType = DepType.ALL):
if DepType.LIBRARY in dep_type:
do_thing() So having the nice |
Why are you having |
But, the code is not correct :) def foo(dep_type: DepType = DepType.ALL):
if DepType.LIBRARY in dep_type:
do_thing() We now know that def foo(dep_type: DepType | int = DepType.ALL):
if not isinstance(dep_typ, DepType):
dep_type = DepType(dep_typ)
if DepType.LIBRARY in dep_type:
do_thing() And it will do what you want :) |
Well in this case I want to be a nice easy I think this might capture the intent best (albeit a little verbose): class DepType(Flag):
LIBRARY = auto()
SCRIPT = auto()
TEST = auto()
ALL: "DepType"
DepType.ALL = DepType.LIBRARY | DepType.SCRIPT | DepType.TEST |
Demo for @ethanfurman's suggestion: >>> class Example(Flag):
... A = 1
... B = 2
... ALL = A | B
...
>>> Example.ALL
<Example.ALL: 3>
>>> Example.A & Example.ALL
<Example.A: 1>
>>> Example.A & Example.B
<Example: 0>
>>> bool(Example.A & Example.B)
False |
@thejcannon You're last example is exactly the same as if you had not used |
Not quite (see below) although:
Welp, I incorrectly assumed it'd show up in Apparently my intuiting surrounding from enum import Flag, auto, nonmember
class DepType(Flag):
LIBRARY = auto()
SCRIPT = auto()
TEST = auto()
IMMEDIATE = nonmember(LIBRARY | SCRIPT | TEST)
DELAYED: "DepType"
ALIAS = LIBRARY | SCRIPT | TEST
DepType.DELAYED = DepType.LIBRARY | DepType.SCRIPT | DepType.TEST
print(type(DepType.IMMEDIATE)) # <class 'int'>
print(type(DepType.DELAYED)) # <flag 'DepType'>
print(type(DepType.ALIAS)) # <flag 'DepType'>
print(list(DepType)) # [<DepType.LIBRARY: 1>, <DepType.SCRIPT: 2>, <DepType.TEST: 4>] So, looks like I do just want I'll leave bug report open for the added test cases, and if anyone wants to noodle if the docs could be more illuminating. Otherwise, pack it up nothing to see here. Just bad intuition. |
@thejcannon When |
…GH-120364) * gh-120361: Add `nonmember` test with enum flags inside to `test_enum`
…_enum` (pythonGH-120364) * pythongh-120361: Add `nonmember` test with enum flags inside to `test_enum` (cherry picked from commit 7fadfd8) Co-authored-by: Nikita Sobolev <[email protected]>
…_enum` (pythonGH-120364) * pythongh-120361: Add `nonmember` test with enum flags inside to `test_enum` (cherry picked from commit 7fadfd8) Co-authored-by: Nikita Sobolev <[email protected]>
Thanks everyone! |
…_enum` (pythonGH-120364) * pythongh-120361: Add `nonmember` test with enum flags inside to `test_enum`
…_enum` (pythonGH-120364) * pythongh-120361: Add `nonmember` test with enum flags inside to `test_enum`
…_enum` (pythonGH-120364) * pythongh-120361: Add `nonmember` test with enum flags inside to `test_enum`
Bug report
Bug description:
this manifests by seeing a funky error on:
If this isn't a bug, it should at least be mentioned on the docs.
CPython versions tested on:
3.11
Operating systems tested on:
Linux
Linked PRs
nonmember
test with enum flags inside totest_enum
#120364nonmember
test with enum flags inside totest_enum
(GH-120364) #120511nonmember
test with enum flags inside totest_enum
(GH-120364) #120512The text was updated successfully, but these errors were encountered: