[issue40582] Inconsistent exceptions caused by typing + tuple subclasses

2020-05-10 Thread Guido van Rossum


Guido van Rossum  added the comment:

I recommend taking this to a user forum or list. I see no but in typing.py here.

--
resolution:  -> not a bug
stage:  -> 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



[issue40582] Inconsistent exceptions caused by typing + tuple subclasses

2020-05-10 Thread Dennis Sweeney


Change by Dennis Sweeney :


--
nosy: +gvanrossum, levkivskyi

___
Python tracker 

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



[issue40582] Inconsistent exceptions caused by typing + tuple subclasses

2020-05-10 Thread Ruairidh MacLeod


Ruairidh MacLeod  added the comment:

The original code for this was:

```
from typing import List
from unittest.mock import call

def f(n: List[call]): ...
```

Which produces "SyntaxError: Forward reference must be an expression -- got ''".

I think my only query is whether this behavior is ok, or whether it should 
produce the clearer "TypeError: Parameters to generic types must be types" 
message instead?

--

___
Python tracker 

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



[issue40582] Inconsistent exceptions caused by typing + tuple subclasses

2020-05-09 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

I think the behavior is consistent between tuple and an empty subclass:

>>> from typing import List
>>> class T(tuple):
pass

== Empty tuple/T ==

>>> List[()]
Traceback (most recent call last):
...
TypeError: Too few parameters for typing.List; actual 0, expected 1


>>> List[T()]
Traceback (most recent call last):
...
TypeError: Too few parameters for typing.List; actual 0, expected 1


== tuple/T whose only entry is 1 ==

>>> List[(1,)]
Traceback (most recent call last):
...
TypeError: Parameters to generic types must be types. Got 1.


>>> List[T((1,))]
Traceback (most recent call last):
...
TypeError: Parameters to generic types must be types. Got 1.


== tuple/T whose only entry is "" ==

>>> List[T(("",))]
Traceback (most recent call last):
...
SyntaxError: unexpected EOF while parsing

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
...
SyntaxError: Forward reference must be an expression -- got ''


>>> List[("",)]
Traceback (most recent call last):
...
SyntaxError: unexpected EOF while parsing

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
...
SyntaxError: Forward reference must be an expression -- got ''


== tuple/T whose only entry can rightly become a forward reference ==

>>> List[("Foo",)]
typing.List[ForwardRef('Foo')]


>>> List[T(("Foo",))]
typing.List[ForwardRef('Foo')]


Your ``return tuple.__new__(cls, ("",))`` constructor is just always
returning the same as ``T(("",))``

--
nosy: +Dennis Sweeney

___
Python tracker 

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



[issue40582] Inconsistent exceptions caused by typing + tuple subclasses

2020-05-09 Thread Ruairidh MacLeod


New submission from Ruairidh MacLeod :

When incorrectly defining a function with a typed List[T] argument where T is a 
tuple instance, a TypeError is correctly raised:

t = (1,)
def f(a: List[t]): ...
# => TypeError: Parameters to generic types must be types. Got 1.


When t is an instance of a tuple subclass though, and one of its items is an 
empty string, a SyntaxError is raised instead in the typing module:

class T(tuple):
def __new__(cls):
return tuple.__new__(cls, ("",))

t = T()
def f(a: List[t]): ...
# => SyntaxError: Forward reference must be an expression -- got ''

Full stack trace:

Traceback (most recent call last):
  File "/opt/python37/lib/python3.7/typing.py", line 449, in __init__
code = compile(arg, '', 'eval')
  File "", line 0

^
SyntaxError: unexpected EOF while parsing

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 5, in 
def f(a: List[call]):
  File "/opt/python37/lib/python3.7/typing.py", line 254, in inner
return func(*args, **kwds)
  File "/opt/python37/lib/python3.7/typing.py", line 631, in __getitem__
params = tuple(_type_check(p, msg) for p in params)
  File "/opt/python37/lib/python3.7/typing.py", line 631, in 
params = tuple(_type_check(p, msg) for p in params)
  File "/opt/python37/lib/python3.7/typing.py", line 132, in _type_check
return ForwardRef(arg)
  File "/opt/python37/lib/python3.7/typing.py", line 451, in __init__
raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
SyntaxError: Forward reference must be an expression -- got ''


Lastly, a different TypeError is raised for an empty subclass:

class C(tuple): ...
c = C()
def f(a: List[c]): ...
# => TypeError: Too few parameters for typing.List; actual 0, expected 1

This exception behavior seems inconsistent, although it's definitely a minor 
issue.

--
components: Interpreter Core
messages: 368554
nosy: rkm
priority: normal
severity: normal
status: open
title: Inconsistent exceptions caused by typing + tuple subclasses
type: behavior
versions: Python 3.9

___
Python tracker 

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