[issue45857] PEP 604 Union syntax does not support forward references

2021-11-22 Thread Alex Waygood


Alex Waygood  added the comment:

Thanks, Ken! To clarify: I agree that changing the implementation here would 
probably be a bad way to go: it would be foolish to try to replicate all the 
functionality of the typing module as builtins. I also think the existing 
documentation at https://docs.python.org/3/library/stdtypes.html#union-type is 
actually very good, so I don't think it needs a fundamental rewrite by any 
means.

I do still think a sentence or two could be added to the documentation, 
however, clarifying how to deal with forward references, since the behaviour 
here is different to the older, more established syntax using typing.Union. 
Something like this?

"""
Note: The object on both sides of the | operand must be an object that defines 
the __or__ special method. As the str type does not support __or__, the 
expression `int | "Foo"`, where  "Foo" is a reference to a class not yet 
defined, will fail at runtime. To annotate forward references using union-type 
expressions, present the whole expression as a string, e.g. `"int | Foo"`.
"""

--
status: pending -> open

___
Python tracker 

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



[issue45857] PEP 604 Union syntax does not support forward references

2021-11-22 Thread Ken Jin


Ken Jin  added the comment:

I think I saw a similar bug report elsewhere (or maybe I'm misremembering). 
Anyways, Eric is right, the correct way is to wrap the entire thing, so 
"Foo|int" instead of "Foo"|int.

@Alex you brought up some good suggestions, I'll try to address them:

> Arguably, either the implementation should be altered to support forward 
> references

Unfortunately that's more complex than it seems. The original draft PEP 604 
proposed implementation actually imported Union from typing.py, and I recall 
Guido disliking the idea that a builtin type should depend on typing.py. I have 
to agree with that philosophy here. I also don't think the alternative -- 
implementing a builtin ForwardRef type isn't worth the complexity unless our 
situation changes.

> the documentation at 
> https://docs.python.org/3/library/stdtypes.html#union-type should be altered 
> to make clear that ...

The first line says: "A union object holds the value of the | (bitwise or) 
operation on multiple type objects." It says *type objects*, which strings 
don't belong to.

@TNThung does Eric's suggestion work for you? Or do you need something else?

--
status: open -> pending

___
Python tracker 

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



[issue45857] PEP 604 Union syntax does not support forward references

2021-11-21 Thread Alex Waygood


Alex Waygood  added the comment:

Arguably, either the implementation should be altered to support forward 
references, or the documentation at 
https://docs.python.org/3/library/stdtypes.html#union-type should be altered to 
make clear that, when type-hinting a union that includes a forward reference, 
the entire expression should be given as a string, as Eric suggests.

--

___
Python tracker 

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



[issue45857] PEP 604 Union syntax does not support forward references

2021-11-21 Thread Eric V. Smith


Eric V. Smith  added the comment:

Presumably the correct way to do this is:

def __init__(self, tmp: "Foo|int"):

That is, the entire type hint is a string.

--
nosy: +eric.smith

___
Python tracker 

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



[issue45857] PEP 604 Union syntax does not support forward references

2021-11-21 Thread Alex Waygood


Alex Waygood  added the comment:

Reproduced on 3.11. The error occurs if a type is on the left-hand-side of the 
operand, as well as if a type is on the right-hand-side:

```
>>> int | "str"
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unsupported operand type(s) for |: 'type' and 'str'
>>> "str" | int
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unsupported operand type(s) for |: 'str' and 'type'
```

--
nosy: +AlexWaygood, gvanrossum, kj
title: Type hint for methods -> PEP 604 Union syntax does not support forward 
references
type: compile error -> behavior
versions: +Python 3.11

___
Python tracker 

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