-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-46170: Improve the error message when subclassing NewType #30268
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-46170: Improve the error message when subclassing NewType #30268
Conversation
You can use https://blurb-it.herokuapp.com/ to add a News entry 🙂 |
I've just realised that what I thought was the |
I've had the idea to return a dummy class from mro entries and have the dummy class implement init subclass which then raises the type error. |
That looks like it might work, but I'd beware of making the implementation too complex for |
What Alex says. |
Should I update this to use |
Hmm, I don't think this is really the use case for which that was designed. How exactly are you thinking it might help? 🙂 |
With this patch applied, we get the following traceback if we try to subclass a pseudo-class created with >>> from typing import NewType
>>> UserID = NewType('UserID', int)
>>> class Foo(UserID): pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Alex\Desktop\Code dump\cpython\Lib\typing.py", line 2478, in __init_subclass__
raise TypeError(
^^^^^^^^^^^^^^^^
TypeError: Cannot subclass UserID, perhaps you were looking for: Foo = NewType('Foo', UserID) A simpler patch might simply define class NewType:
def __mro_entries__(cls, bases):
raise TypeError(f'Cannot subclass an instance of `NewType`') With that patch applied, you get the following traceback: >>> from typing import NewType
>>> UserID = NewType('UserID', int)
>>> class Foo(UserID): pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Alex\Desktop\Code dump\cpython\Lib\typing.py", line 2473, in __mro_entries__
raise TypeError(f'Cannot subclass an instance of `NewType`')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Cannot subclass an instance of `NewType` The existing patch has the benefit that it tells you what to do instead, but I think a simpler implementation would still be much better than the status quo, and would still be easily googleable. @Gobot1234, what do you think? 🙂 |
I'm personally a bit fan of being told how to fix something but if you think the implementation is too much added maintenance I'd be happy to remove it |
I thought the point of the added attribute was to enrich exceptions. |
It is, of course, but the intended use case (as far as I understand PEP 678) is generally to add information to exceptions that "make sense". The error message when you try to subclass an instance of |
…' into better-new-type-subclass-message # Conflicts: # Lib/test/test_typing.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I would still go for the simpler implementation myself, but this is looking a lot better to me now 👍
Co-authored-by: Alex Waygood <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a creative abuse of __mro_entries__
, but it doesn't seem likely to cause maintenance or performance issues and it gives a much nicer error message. I'm fine with moving forward with it.
Misc/NEWS.d/next/Library/2021-12-26-14-45-51.bpo-46170.AQ7kSM.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: Nikita Sobolev <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is such a clever workaround. I'm at a loss for words. My only minor concern is that it feels slightly hacky.
Misc/NEWS.d/next/Library/2021-12-26-14-45-51.bpo-46170.AQ7kSM.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: Ken Jin <[email protected]>
@JelleZijlstra I plan to merge, is that alright? Does anyone else have any objections? |
Sounds good, I was planning to hold off until the next alpha is out since it's bumpy and I don't want to give Pablo more headaches. |
Good idea, I forgot about that. Let's do that then :). |
https://bugs.python.org/issue46170