Skip to content

bpo-33217: Raise TypeError for non-Enum lookups in Enums#6651

Merged
ethanfurman merged 4 commits intopython:masterfrom
RJ722:bpo-33217
Sep 10, 2018
Merged

bpo-33217: Raise TypeError for non-Enum lookups in Enums#6651
ethanfurman merged 4 commits intopython:masterfrom
RJ722:bpo-33217

Conversation

@RJ722
Copy link
Copy Markdown
Contributor

@RJ722 RJ722 commented Apr 30, 2018

Raise :exc:TypeError when attempting to lookup non-Enum objects in a :class:Enum member (e.g. 1 in Color) or non-Flag objects in a :class:Flag member (e.g. 1 in Perm.RW).

https://bugs.python.org/issue33217

@the-knights-who-say-ni
Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately our records indicate you have not signed the CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

When your account is ready, please add a comment in this pull request
and a Python core developer will remove the CLA not signed label
to make the bot check again.

Thanks again to your contribution and we look forward to looking at it!

@ethanfurman
Copy link
Copy Markdown
Member

Looks good! Let me know when you've signed the CLA so I can double-check the automated system for acknowleding that.

@RJ722
Copy link
Copy Markdown
Contributor Author

RJ722 commented Apr 30, 2018

Comment thread Lib/enum.py Outdated
if not isinstance(member, Enum):
raise TypeError(
"Unsupported operands type(s) for 'in': '%s' and '%s'" % (
type(member).__qualname__, type(Enum).__qualname__))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I just noticed that if we use cls.__qualname__ instead of type(Enum).__qualname__, we get a much better output.

For example if we have the following program

ABC = enum.Flag('ABC', 'a, b, c')
'test' in ABC

then after changing to cls.__qualname__, we get:

TypeError: Unsupported operands type(s) for 'in': 'str' and 'ABC'

which looks better than what we were getting before:

TypeError: Unsupported operands type(s) for 'in': 'str' and 'EnumMeta'

Which one should we use?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That definitely looks better; go ahead and use cls.__qualname__ where ever it's needed.

Comment thread Lib/enum.py Outdated
return NotImplemented
raise TypeError(
"Unsupported operands type(s) for 'in': '%s' and '%s'" % (
type(other).__qualname__, type(self).__qualname__))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We would need similar changes here as well.

@RJ722
Copy link
Copy Markdown
Contributor Author

RJ722 commented May 1, 2018

I have made the requested changes @ethanfurman.

@RJ722
Copy link
Copy Markdown
Contributor Author

RJ722 commented May 1, 2018

Oops! The tests are failing - Looks like not all the classes have a __qualname__ defined. What should I do?

Comment thread Lib/enum.py Outdated
raise TypeError(
"Unsupported operands type(s) for 'in': '%s' and '%s'" % (
type(other).__qualname__, type(self).__qualname__))
type(other).__qualname__, self.__qualname__))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

self is the member itself, not the class -- that is why the __qualname__ lookup is faling; use self.__class__._qualname__ instead.

@bedevere-bot
Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@RJ722
Copy link
Copy Markdown
Contributor Author

RJ722 commented May 1, 2018

I have made the requested changes; please review again.

@bedevere-bot
Copy link
Copy Markdown

Thanks for making the requested changes!

@ethanfurman: please review the changes made to this pull request.

@RJ722
Copy link
Copy Markdown
Contributor Author

RJ722 commented May 11, 2018

Hey @ethanfurman, I don't want to sound desperate but I am rather too excited. Can I have one final review, please? 😋

Copy link
Copy Markdown
Member

@ethanfurman ethanfurman left a comment

Choose a reason for hiding this comment

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

Almost there!

Comment thread Lib/test/test_enum.py
3 in Season
with self.assertRaises(TypeError):
'AUTUMN' in Season

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's change these to assertRaisesRegex and test for the qualname being returned; something like

self.assertRaisesRegex(TypeError, r"unsupported operand type.s. for .in.: .int. and .Season."):

@bedevere-bot
Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@ethanfurman ethanfurman dismissed their stale review September 10, 2018 18:20

Minor change not needed.

@ethanfurman ethanfurman merged commit 9430652 into python:master Sep 10, 2018
@RJ722 RJ722 deleted the bpo-33217 branch March 14, 2020 18:31
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.

5 participants