[issue46170] Improving the error message when subclassing NewType

2022-03-07 Thread Jelle Zijlstra


Change by Jelle Zijlstra :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46170] Improving the error message when subclassing NewType

2022-03-07 Thread Jelle Zijlstra


Jelle Zijlstra  added the comment:


New changeset f391f9bf28f0bba7939d9f9e5a7a6396d2b0df62 by James Hilton-Balfe in 
branch 'main':
bpo-46170: Improve the error message when subclassing NewType (GH-30268)
https://github.com/python/cpython/commit/f391f9bf28f0bba7939d9f9e5a7a6396d2b0df62


--
nosy: +Jelle Zijlstra

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46170] Improving the error message when subclassing NewType

2021-12-26 Thread Nikita Sobolev


Nikita Sobolev  added the comment:

Turns out modern docs already have this problem fixed: 
https://docs.python.org/3/library/typing.html#newtype

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46170] Improving the error message when subclassing NewType

2021-12-26 Thread Nikita Sobolev


Nikita Sobolev  added the comment:

> So maybe those docs are incorrect? I can't find anything in PEP 484 about 
> this.

Mypy even has an explicit test that NewType of NewType should work: 
https://github.com/python/mypy/blob/f96446ce2f99a86210b21d39c7aec4b13a8bfc66/test-data/unit/check-newtype.test#L162-L165

I tend to agree with this behavior. This allows us to create complex type-safe 
DSLs:

```python
from typing import NewType

UserId = NewType('UserId', int)
ProUserId = NewType('ProUserId', UserId)

x: ProUserId = ProUserId(UserId(1))
```

Mypy is happy with that!

I will open a new issue about incorrect docs.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46170] Improving the error message when subclassing NewType

2021-12-26 Thread Guido van Rossum


Guido van Rossum  added the comment:

Weird, the docs you cite claims


   # Also does not typecheck
   ProUserId = NewType('ProUserId', UserId)

but when I try it, it works at runtime and passes mypy (even with --strict) and 
also pyright.

So maybe those docs are incorrect? I can't find anything in PEP 484 about this.

In any case I'd drop the newline in the message -- other "Did you mean" errors 
don't have those.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46170] Improving the error message when subclassing NewType

2021-12-26 Thread Gobot1234


Gobot1234  added the comment:

> Do you have evidence that people make this particular mistake often? And that 
> the suggested solution is indeed what they should use? Why would you want to 
> subclass UserId?

I was just trying to implement something that I thought had a non-obvious error 
message and isn't necessarily initially obvious if you have the idea that 
NewType creates a new type/class. The documentation suggests this is what you 
should be doing instead of subclassing it
https://github.com/python/cpython/commit/342e800e974475cc302c46ed6d9f75276035278f#diff-8a0f115fde6769c122b771b6d0eca184c4580f7b5fabe2f0b0579c679424364fR101-R113

> Do we have examples of other error messages split across two lines like this?

No if you want me to remove it thats fine by me.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46170] Improving the error message when subclassing NewType

2021-12-26 Thread Guido van Rossum


Guido van Rossum  added the comment:

Do you have evidence that people make this particular mistake often? And that 
the suggested solution is indeed what they should use? Why would you want to 
subclass UserId?

Do we have examples of other error messages split across two lines like this?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46170] Improving the error message when subclassing NewType

2021-12-26 Thread Gobot1234


Change by Gobot1234 :


--
keywords: +patch
pull_requests: +28485
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/30268

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46170] Improving the error message when subclassing NewType

2021-12-26 Thread Alex Waygood


Change by Alex Waygood :


--
nosy: +AlexWaygood

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46170] Improving the error message when subclassing NewType

2021-12-26 Thread Nikita Sobolev


Nikita Sobolev  added the comment:

> I'd be happy to patch this myself if this sounds like a good idea.

Oh, please, go ahead! :)
Would be happy to review.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46170] Improving the error message when subclassing NewType

2021-12-26 Thread Nikita Sobolev


Nikita Sobolev  added the comment:

This seems like a good idea to me.

I will send a PR for others to judge :)

--
nosy: +sobolevn

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46170] Improving the error message when subclassing NewType

2021-12-23 Thread Gobot1234


New submission from Gobot1234 :

I'd like to propose making the error message when subclassing typing.NewType 
much more understandable. Currently it looks like:
```
TypeError: NewType.__init__() takes 3 positional arguments but 4 were given
```
But I think we could do much better by adding __mro_entries__ to the class and 
then just having that raise a TypeError telling users they cannot subclass 
NewType and they are probably looking for `NewType('Subclass', OlderType)`. I'd 
be happy to patch this myself if this sounds like a good idea.

--
components: Library (Lib)
messages: 409114
nosy: Gobot1234, gvanrossum, kj
priority: normal
severity: normal
status: open
title: Improving the error message when subclassing NewType
type: enhancement
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com