... and, runtime type checking (like https://github.com/agronholm/typeguard)
must have a synthetised Union[] in __annotations__.



Le jeu. 29 août 2019 à 15:02, Philippe Prados <philippe.pra...@gmail.com> a
écrit :

> No, it's not possible, because
>
> >>> int | str
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: unsupported operand type(s) for |: 'type' and 'type'
>
> Regards
>
> Philippe Prados
>
>
> Le jeu. 29 août 2019 à 14:55, Inada Naoki <songofaca...@gmail.com> a
> écrit :
>
>> I don't want to add runtime behaviors for static type hinting.
>>
>> There is PEP 563 instead.
>> Tools like mypy can implement them without touching runtime behavior.
>>
>>
>> On Thu, Aug 29, 2019 at 9:48 PM Philippe Prados <
>> philippe.pra...@gmail.com> wrote:
>>
>>> Hello everybody,
>>>
>>> Scala 3 propose the a new syntax for Union type. See here
>>> <https://dotty.epfl.ch/docs/reference/new-types/union-types.html>. I
>>> propose to add a similar syntax in Python.
>>>
>>> # Operator for Union
>>> assert( int | str == Union[int,str])
>>> assert( int | str | float == Union[int,str,float])
>>> # Operator for Optional
>>> assert( ~int == Optional[int])
>>>
>>> Now, it's possible to write:
>>>
>>> def fn(bag:List[int | str], option: ~int = None) -> float | str: ...
>>>
>>> in place of
>>>
>>> def fn(bag:List[Option[int,str]], option: Optional[int] = None) ->
>>> Union[float,str]: ...
>>>
>>> I think these syntaxes are more clear, and can help with the adoption of
>>> typing.
>>>
>>>
>>> I test and implement these ideas in a two fork : One for CPython
>>> <https://github.com/pprados/cpython> and one for MyPy
>>> <https://github.com/pprados/mypy>. See the branches add_OR_to_types
>>> (for Union syntax) or add_INVERT_to_types (for Union and Optional
>>> syntax).
>>>
>>> How I implement that ? I add the operators __or__ and __revert__ to
>>> PyType_Type. The C code is similar of :
>>>
>>> from typing import *
>>> def type_or(self,right):
>>>   return Union[self,right]
>>> type(type).__or__ = type_or
>>>
>>> Actually, the accepted syntax for typing is :
>>>
>>> annotation: name_type
>>> name_type: NAME (args)?
>>> args: '[' paramslist ']'
>>> paramslist: annotation (',' annotation)* [',']
>>>
>>> I propose to extend the syntax to :
>>>
>>> annotation: ( name_type | or_type | invert_type )
>>> name_type: NAME (args)?
>>> args: '[' paramslist ']'
>>> paramslist: annotation (',' annotation)* [',']
>>>
>>> or_type: name_type '|' annotation
>>>
>>> invert_type: '~' annotation
>>>
>>>
>>> What do you think about that ?
>>>
>>> The draft of a PEP is here
>>> <https://github.com/pprados/peps/blob/master/pep-9999.rst>.
>>>
>>> Regards
>>> _______________________________________________
>>> Python-ideas mailing list -- python-ideas@python.org
>>> To unsubscribe send an email to python-ideas-le...@python.org
>>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>>> Message archived at
>>> https://mail.python.org/archives/list/python-ideas@python.org/message/FCTXGDT2NNKRJQ6CDEPWUXHVG2AAQZZY/
>>> Code of Conduct: http://python.org/psf/codeofconduct/
>>>
>>
>>
>> --
>> Inada Naoki  <songofaca...@gmail.com>
>>
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/RB6MGEMFLGEOJU7UEMR7A2XR2JOTT6YG/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to