[issue44617] Undesired Behavior on `match` using Singleton object

2021-07-12 Thread Pablo Aguilar


New submission from Pablo Aguilar :

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 
<https://bugs.python.org/issue44617>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44617] Undesired Behavior on `match` using Singleton object

2021-07-13 Thread Pablo Aguilar


Pablo Aguilar  added the comment:

But, `Maybe.empty` works like a `Literal Pattern` or `Constant Pattern`?

--

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



[issue44617] Undesired Behavior on `match` using Singleton object

2021-07-13 Thread Pablo Aguilar


Pablo Aguilar  added the comment:

In fact, I'm worried about how to explain some behaviors.

Look here:
```python
# `Maybe` here is the same class described in the first message
Nothing = Maybe()
Maybe.empty = Nothing


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

I can't use `Nothing` to match the values even though it's a variable but using 
`Maybe.empty` it works.

--

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