[issue44941] Add check_methods function to standard library

2021-08-20 Thread Finn Mason


Finn Mason  added the comment:

Thank you for the suggestions.

>From what I could tell, the cited python-ideas discussion was more about 
>checking the signature of a method. However, an issue cited by the ex-BDFL 
>(issue9731) was helpful. It's a similar idea that verifies that a class 
>implements an ABC. However, I'm looking at a function that simplifies writing 
>the ABC itself.

I will post the idea to python-ideas and see what people think.

--

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



[issue44941] Add check_methods function to standard library

2021-08-18 Thread Finn Mason


Finn Mason  added the comment:

I strongly feel that `check_methods` shouldn't be in collections.abc, even 
though that's where it's originally found, because it's not related 
specifically to collections but to ABCs and classes in general. I would prefer 
for it to be implemented in `abc` or similar.

I'm reverting to the original title until a module is decided on.

I'd be happy to do the implementation and pull request once approval is given 
and a module is decided on.

--
title: Add check_methods function to collections.abc in standard library -> Add 
check_methods function to standard library

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



[issue44941] Add check_methods function to standard library

2021-08-17 Thread Finn Mason


New submission from Finn Mason :

In _collections_abc.py is a private function titled `_check_methods`. It takes 
a class and a number of method names (as strings), checks if the class has all 
of the methods, and returns NotImplemented if any are missing. The code is 
below:

```
def _check_methods(C, *methods):
mro = C.__mro__
for method in methods:
for B in mro:
if method in B.__dict__:
if B.__dict__[method] is None:
return NotImplemented
break
else:
return NotImplemented
return True
```

This is an incredibly convenient function (referred to as check_methods here on 
out) for creating abstract base classes, and is much simpler than using 
`hasattr` for each method you want to check. For example:

```
>>> from abc import ABCMeta
>>> # Without check_methods
>>> class A(metaclass=ABCMeta):
... @classmethod
... def __subclasshook__(cls, subclass):
... return (hasattr(subclass, 'foo') and
... callable(subclass.foo) and
... hasattr(subclass, 'bar') and
... callable(subclass.bar) or
... NotImplemented)
...
>>> # With check_methods
>>> class B(metaclass=ABCMeta):
... @classmethod
... def __subclasshook(cls, subclass):
... return check_methods(subclass, 'foo', 'bar')
...
>>>
```

This would be a great function to add to the standard lib, perhaps in the `abc` 
module.

One problem with `check_methods` as defined in _collections_abc.py is that it 
doesn't check if the name is callable. Also, type hints and more readable 
variables may be desirable. The final code, if implemented, may look something 
like this:

```
# In imports section: from typing import Literal
def check_methods(Class: type, *methods: str) -> Literal[True, NotImplemented]:
"""Check if class `Class` has methods `methods`."""
mro = Class.__mro__
for method in methods:
for Base in mro:
if (attr := getattr(Base, method, None)) is not None:
if not callable(attr):
return NotImplemented
break
else:
return NotImplemented
return True
```

Again, this would be a great function to add to the `abc` module or a similar 
one.

--
components: Library (Lib)
messages: 399814
nosy: finnjavier08
priority: normal
severity: normal
status: open
title: Add check_methods function to standard library
type: enhancement

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



[issue44341] Conflict between re.match and match keyword

2021-06-07 Thread Finn Mason


Change by Finn Mason :


--
nosy:  -finnjavier08

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



[issue44341] Conflict between re.match and match keyword

2021-06-07 Thread Finn Mason


New submission from Finn Mason :

>>> import re
>>> re.match('str', 'str').group()
'str'
>>> match 'str':
... case 'str':
... print('match!')
...
match!
>>> from re import match
>>> match


As the above example demonstrates, while re.match doesn't raise an error 
despite having a keyword name, importing re.match directly into __main__ 
replaces the keyword with the function. The obvious solution is to rename 
re.match, but this would break many, many pieces of code.

--
components: Library (Lib), Regular Expressions
messages: 395295
nosy: ezio.melotti, finnjavier08, mrabarnett
priority: normal
severity: normal
status: open
title: Conflict between re.match and match keyword
type: behavior
versions: Python 3.10, Python 3.11

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