[issue43682] Make static methods created by @staticmethod callable

2021-04-13 Thread Inada Naoki


Inada Naoki  added the comment:

Strictly speaking, adding any method is "potential" breaking change because 
hasattr(obj, "new_method") become from False to True. And since Python is 
dynamic language, any change is "potential" breaking change.

But we don't treat such change as breaking change. Practical beats purity.
We can use beta period to see is this change breaks real world application.

In case of staticmethod, I think creating a new thread in python-dev is ideal 
because it is language core feature. I will post a thread.

--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-12 Thread Mark Shannon


Mark Shannon  added the comment:

Are you asking why breaking backwards compatibility is an issue?
Or how it breaks backwards compatibility?

pydoc could be changed to produce the proposed output, it doesn't need this 
change.

We don't know what this change will break, but we do know that it is a 
potentially breaking change.
`callable(staticmethod(f))` will change from `False` to `True`.

I don't think you should be making changes like this unilaterally.

--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-12 Thread STINNER Victor


STINNER Victor  added the comment:

Mark Shannon:
> Changing static methods to be callable will break backwards compatibility for 
> any code that tests `callable(x)` where `x` is a static method.

Can you please elaborate on why this is an issue?

In the pydoc case, it sounds like an enhancement:
https://bugs.python.org/issue43682#msg390525

--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-12 Thread Mark Shannon


Mark Shannon  added the comment:

This is a significant change to the language.
There should be a PEP, or at the very least a discussion on Python Dev.

There may well be a very good reason why static methods have not been made 
callable before that you have overlooked.

Changing static methods to be callable will break backwards compatibility for 
any code that tests `callable(x)` where `x` is a static method.

I'm not saying that making staticmethods callable is a bad idea, just that it 
needs proper discussion.

https://bugs.python.org/issue20309 was closed as "won't fix". What has changed?

--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-12 Thread STINNER Victor


STINNER Victor  added the comment:

Ok, static methods are now callable in Python 3.10. Moreover, @staticmethod and 
@classmethod copy attributes from the callable object, same as 
functools.wraps().

Thanks to this change, I was able to propose to PR 25354 "bpo-43680: 
_pyio.open() becomes a static method".

Serhiy: if you want to "Implement __get__ for C functions", I suggest you 
opening a new issue for that. To be honest, I'm a little bit scared by the 
migration path, I expect that it will require to fix *many* projects.

--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-11 Thread STINNER Victor


STINNER Victor  added the comment:


New changeset 553ee2781a37ac9d2068da3e1325a780ca79e21e by Victor Stinner in 
branch 'master':
bpo-43682: Make staticmethod objects callable (GH-25117)
https://github.com/python/cpython/commit/553ee2781a37ac9d2068da3e1325a780ca79e21e


--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-10 Thread STINNER Victor


STINNER Victor  added the comment:

> Implement __get__ for C functions. Of course it is breaking change so we 
> should first emit a warning. It will force all users to use staticmethod 
> explicitly if they set a C function as a class attribute. We can also start 
> emitting warnings for all callable non-descriptor class attributes.

Well... such change would impact way more code and sounds to require a painful 
migration plan.

Also, it doesn't prevent to make static methods callable, no?

--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-10 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

> Do you see a way to make C functions and Python functions behave the same?

Implement __get__ for C functions.

Of course it is breaking change so we should first emit a warning. It will 
force all users to use staticmethod explicitly if they set a C function as a 
class attribute. We can also start emitting warnings for all callable 
non-descriptor class attributes.

--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-09 Thread STINNER Victor


STINNER Victor  added the comment:


New changeset 507a574de31a1bd7fed8ba4f04afa285d985109b by Victor Stinner in 
branch 'master':
bpo-43682: @staticmethod inherits attributes (GH-25268)
https://github.com/python/cpython/commit/507a574de31a1bd7fed8ba4f04afa285d985109b


--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-09 Thread STINNER Victor


STINNER Victor  added the comment:

Serhiy:
> I concur with Mark Shannon. The root problem is that Python functions and 
> built-in functions have different behavior when assigned as class attribute. 
> The former became an instance method, but the latter is not.

Do you see a way to make C functions and Python functions behave the same?

--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-09 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

Currently pydoc on X.sm gives:
---
sm(x, y)
A static method
---


I concur with Mark Shannon. The root problem is that Python functions and 
built-in functions have different behavior when assigned as class attribute. 
The former became an instance method, but the latter is not.

If wrap builtin open with statickmethod, the repr of open will be something 
like "staticmethod()" instead of just 
"". It is confusing. It will produce a lot of 
questions why open (and only open) is so special.

--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-08 Thread Inada Naoki


Change by Inada Naoki :


--
nosy: +methane

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-08 Thread STINNER Victor


STINNER Victor  added the comment:

There is a nice side effect of PR 25268 + PR 25117: pydoc provides better self 
documentation for the following code:

class X:
@staticmethod
def sm(x, y):
'''A static method'''
...

pydoc on X.sm:
---
sm(x, y)
A static method
---

instead of:
---

---

--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-07 Thread STINNER Victor


STINNER Victor  added the comment:

Serhiy Storchaka:
> If make staticmethod a calllable and always wrap open, we need to change also 
> its repr and add the __doc__ attribute (and perhaps other attributes to make 
> it more interchangeable with the original function).

You right and I like this idea! I created PR 25268 to inherit the function 
attributes (__name__, __doc__, etc.) in @staticmethod and @classmethod wrappers.

--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-07 Thread STINNER Victor


Change by STINNER Victor :


--
pull_requests: +24003
pull_request: https://github.com/python/cpython/pull/25268

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-04-01 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

If make staticmethod a calllable and always wrap open, we need to change also 
its repr and add the __doc__ attribute (and perhaps other attributes to make it 
more interchangeable with the original function).

Alternate option: make staticmethod(func) returning func if it is not a 
descriptor.

--
nosy: +serhiy.storchaka

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-03-31 Thread STINNER Victor


STINNER Victor  added the comment:

> Isn't the problem that Python functions are (non-overriding) descriptors, but 
> builtin-functions are not descriptors?
> Changing static methods is not going to fix that.
> How about adding wrappers to make Python functions behave like builtin 
> functions and vice versa?

I would love consistency, but is that possible without breaking almost all 
Python projects?

Honestly, I'm annoying by the having to use staticmethod(), or at least the 
fact that built-in functions and functions implemented in Python don't behave 
the same. It's hard to remind if a stdlib function requires staticmethod() or 
not. Moreover, maybe staticmethod() is not needed today, but it would become 
required tomorrow if the built-in function becomes a Python function somehow.

So yeah, I would prefer consistency. But backward compatibility may enter into 
the game as usual. PR 25117 tries to minimize the risk of backward 
compatibility issues.

For example, if we add __get__() to built-in methods and a bound method is 
created on the following example, it means that all code relying on the current 
behavior of built-in functions (don't use staticmethod) would break :-(
---
class MyClass:
# built-in function currently converted to a method
# magically without having to use staticmethod()
method = len
---

Would it be possible to remove __get__() from FunctionType to allow using a 
Python function as a method? How much code would it break? :-) What would 
create the bound method on a method call?
---
def func():
...

class MyClass:
method = func

# magic happens here!
bound_method = MyClass().method
---

--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-03-31 Thread STINNER Victor

STINNER Victor  added the comment:

> Changing static methods is not going to fix that.

My plan for the _pyio module is:

(1) Make static methods callable
(2) Decorate _pyio.open() with @staticmethod

That would only fix the very specific case of _pyio.open(). But open() use case 
seems to be common enough to became the example in the @staticmethod 
documentation!
https://docs.python.org/dev/library/functions.html#staticmethod

Example added in bpo-31567 "Inconsistent documentation around decorators" by:

commit 03b9537dc515d10528f83c920d38910b95755aff
Author: Éric Araujo 
Date:   Thu Oct 12 12:28:55 2017 -0400

bpo-31567: more decorator markup fixes in docs (GH-3959) (#3966)

--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-03-31 Thread Mark Dickinson


Change by Mark Dickinson :


--
nosy: +rhettinger

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-03-31 Thread Mark Shannon


Mark Shannon  added the comment:

Isn't the problem that Python functions are (non-overriding) descriptors, but 
builtin-functions are not descriptors?
Changing static methods is not going to fix that.

How about adding wrappers to make Python functions behave like builtin 
functions and vice versa?

--

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-03-31 Thread STINNER Victor


Change by STINNER Victor :


--
title: Make function wrapped by staticmethod callable -> Make static methods 
created by @staticmethod callable

___
Python tracker 

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



[issue43682] Make static methods created by @staticmethod callable

2021-03-31 Thread STINNER Victor


Change by STINNER Victor :


--
title: Make function wrapped by staticmethod callable -> Make static methods 
created by @staticmethod callable

___
Python tracker 

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