[PATCH 8/9] Replace callable with collections.Callable (#1014220)
Martin Kolman
mkolman at redhat.com
Thu Jan 29 11:06:05 UTC 2015
On Thu, 2015-01-29 at 09:29 +0100, Vratislav Podzimek wrote:
> On Wed, 2015-01-28 at 18:43 +0100, Martin Kolman wrote:
> > The callable() function has been deprecated in Python 3,
> > so replace it with collections.Callable, which is available
> > in both Python 2 & 3.
> >
> > Signed-off-by: Martin Kolman <mkolman at redhat.com>
> > ---
> > pyanaconda/network.py | 3 ++-
> > pyanaconda/ui/tui/spokes/__init__.py | 5 +++--
> > 2 files changed, 5 insertions(+), 3 deletions(-)
> >
> > diff --git a/pyanaconda/network.py b/pyanaconda/network.py
> > index b1d006a..513f905 100644
> > --- a/pyanaconda/network.py
> > +++ b/pyanaconda/network.py
> > @@ -49,6 +49,7 @@ from pyanaconda.regexes import HOSTNAME_PATTERN_WITHOUT_ANCHORS
> > from gi.repository import NetworkManager
> >
> > import logging
> > +import collections
> > log = logging.getLogger("anaconda")
> >
> > sysconfigDir = "/etc/sysconfig"
> > @@ -766,7 +767,7 @@ def find_ifcfg_file(values, root_path=""):
> > ifcfg = IfcfgFile(filepath)
> > ifcfg.read()
> > for key, value in values:
> > - if callable(value):
> > + if isinstance(value, collections.Callable):
> > if not value(ifcfg.get(key)):
> > break
> > else:
> > diff --git a/pyanaconda/ui/tui/spokes/__init__.py b/pyanaconda/ui/tui/spokes/__init__.py
> > index 8429bbf..ddd7c70 100644
> > --- a/pyanaconda/ui/tui/spokes/__init__.py
> > +++ b/pyanaconda/ui/tui/spokes/__init__.py
> > @@ -27,6 +27,7 @@ from collections import namedtuple
> > from pyanaconda.iutil import setdeepattr, getdeepattr
> > from pyanaconda.i18n import N_, _
> > from pyanaconda.constants import PASSWORD_CONFIRM_ERROR_TUI, PW_ASCII_CHARS
> > +import collections
> >
> > __all__ = ["TUISpoke", "EditTUISpoke", "EditTUIDialog", "EditTUISpokeEntry",
> > "StandaloneSpoke", "NormalTUISpoke"]
> > @@ -238,9 +239,9 @@ class EditTUISpoke(NormalTUISpoke):
> > # changes dynamically
> > ret = []
> > for entry in self.edit_fields:
> > - if callable(entry.visible) and entry.visible(self, self.args):
> > + if isinstance(entry.visible, collections.Callable) and entry.visible(self, self.args):
> > ret.append(entry)
> > - elif not callable(entry.visible) and entry.visible:
> > + elif not isinstance(entry.visible, collections.Callable) and entry.visible:
> > ret.append(entry)
> >
> > return ret
> I collections.Callable really working this way? If yes, I'm really
> curious about its implementation because it seems just weird that our
> object is all of a sudden an instance of some class that has to be
> imported from an external module.
>
Yeah, while this is the fix proposed by the 2to3 tool, it indeed looks
weird. I
ve looked in the Python 3.0 release notes and they have this about
callable:
"Removed callable(). Instead of callable(f) you can use hasattr(f,
'__call__'). The operator.isCallable() function is also gone."
Well, that looks a bit nicer, but I looked further and in 3.2 release
notes:
"The callable() builtin function from Py2.x was resurrected. It provides
a concise, readable alternative to using an abstract base class in an
expression like isinstance(x, collections.Callable):
>>>
>>> callable(max)
True
>>> callable(20)
False
(See issue 10518.)"
I also recommend to check issue 10518, it provides an interesting window
into the Python development process: :)
https://bugs.python.org/issue10518
So in short, callable() is back in Python 3.2 and this batch can be
considered dropped from the set. :)
More information about the anaconda-patches
mailing list