[New-bugs-announce] [issue44617] Undesired Behavior on `match` using Singleton object

Pablo Aguilar report at bugs.python.org
Mon Jul 12 23:34:48 EDT 2021


New submission from Pablo Aguilar <pablo.aguilar at fatec.sp.gov.br>:

Hey, I'm not sure if this is really a bug but I'd like to show to prevent some undesired behavior!

I'm working in the `match` support for returns (https://github.com/dry-python/returns) library when I saw a behavior similar to the described in `Constant Value Patterns` section on PEP-622 (https://www.python.org/dev/peps/pep-0622/#constant-value-patterns).

A very small and reproducible example:
```python
from typing import Any, ClassVar, Optional


class Maybe:
    empty: ClassVar['Maybe']
    _instance: Optional['Maybe'] = None

    def __new__(cls, *args: Any, **kwargs: Any) -> 'Maybe':
        if cls._instance is None:
            cls._instance = object.__new__(cls)
        return cls._instance


Maybe.empty = Maybe()


if __name__ == '__main__':
    my_maybe = Maybe()
    match my_maybe:
        case Maybe.empty:
            print('FIRST CASE')
        case _:
            print('DEFAULT CASE')
```

The output here is `FIRST CASE`, but if I delete `__new__` method the output is `DEFAULT CASE`.

Is that the correct behavior?

Python version: 3.10.0a7

----------
components: Interpreter Core
messages: 397380
nosy: thepabloaguilar
priority: normal
severity: normal
status: open
title: Undesired Behavior on `match` using Singleton object
type: behavior
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44617>
_______________________________________


More information about the New-bugs-announce mailing list