
On Wed, Jun 9, 2021 at 3:33 PM Joseph Perez <joperez@hotmail.fr> wrote:
If the singleton pattern of PEP 484 is too cumbersome, why not update [PEP 586](https://www.python.org/dev/peps/pep-0586/#legal-and-illegal-parameterization...) (which defines `Literal` type) to extend the legal parameterization by adding `object` variable declared as `Final` ? (Because `object` instances are immutable, so they meet the `Literal` criteria when they are also declared as `Final`)
My previous example could be written: ```python from typing import Final, Literal, Union
empty: Final = object()
def func(x: Union[int, Literal[empty]]) -> int: return 0 if x is empty else x ```
This would be a quite simple and elegant solution, which doesn't require any additional stuff too.
The PEP details why I consider the empty = object() pattern to be problematic in several senses: Besides the strict typing issue (which could perhaps be addressed using Literal), it also suffers from having a very poor repr and from not behaving correctly when copied or unpickled. https://www.python.org/dev/peps/pep-0661/#use-notgiven-object That being said, all of these could be addressed by using a dedicated class rather than object(). If we could allow for good type annotations without needing to generate a class for each sentinel, that seems like it would be ideal. - Tal