Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to find out if an object is an instance of a namedtuple #52044

Closed
pwaller mannequin opened this issue Jan 27, 2010 · 22 comments
Closed

No way to find out if an object is an instance of a namedtuple #52044

pwaller mannequin opened this issue Jan 27, 2010 · 22 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@pwaller
Copy link
Mannequin

pwaller mannequin commented Jan 27, 2010

BPO 7796
Nosy @rhettinger, @amauryfa, @pitrou, @merwok, @durban, @ericsnowcurrently

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/rhettinger'
closed_at = <Date 2011-04-20.22:17:48.863>
created_at = <Date 2010-01-27.16:11:08.227>
labels = ['type-feature', 'library']
title = 'No way to find out if an object is an instance of a namedtuple'
updated_at = <Date 2014-01-13.01:15:13.696>
user = 'https://bugs.python.org/pwaller'

bugs.python.org fields:

activity = <Date 2014-01-13.01:15:13.696>
actor = 'abarnert'
assignee = 'rhettinger'
closed = True
closed_date = <Date 2011-04-20.22:17:48.863>
closer = 'rhettinger'
components = ['Library (Lib)']
creation = <Date 2010-01-27.16:11:08.227>
creator = 'pwaller'
dependencies = []
files = []
hgrepos = []
issue_num = 7796
keywords = []
message_count = 22.0
messages = ['98437', '98439', '98440', '98441', '98442', '98443', '98444', '98453', '98555', '99869', '115347', '115371', '132363', '132365', '132377', '132384', '132713', '132716', '134186', '190816', '191870', '207992']
nosy_count = 12.0
nosy_names = ['rhettinger', 'amaury.forgeotdarc', 'pitrou', 'thead', 'eric.araujo', 'zuo', 'pwaller', 'daniel.urban', 'santoso.wijaya', 'python-dev', 'eric.snow', 'abarnert']
pr_nums = []
priority = 'low'
resolution = 'rejected'
stage = 'needs patch'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue7796'
versions = ['Python 3.3']

@pwaller
Copy link
Mannequin Author

pwaller mannequin commented Jan 27, 2010

Apologies if there is a way of doing this, but I haven't been able to find anything.

I need to be able to do the following:

my_tuple = namedtuple("my_tuple", "a b c")
obj = my_tuple(1,2,3)

if isinstance(obj, namedtuple):
    .. do stuff ..

The best I could come up with for the moment is:

if isinstance(obj, tuple) and type(obj) is not tuple:
    .. do stuff ..

@pitrou
Copy link
Member

pitrou commented Jan 27, 2010

In this case, what is wrong with:

if isinstance(obj, my_tuple): ...

or do you want to catch all namedtuples? And if so, why?

(I suppose it would be easy to make all namedtuples inherit from a common base class, though)

@pitrou pitrou added the type-feature A feature request or enhancement label Jan 27, 2010
@merwok
Copy link
Member

merwok commented Jan 27, 2010

Hello

namedtuple is a factory function, not a class (as hinted by the naming in lower case, see PEP 8), so there are no instances of namedtuple.

You can workaround that with issubclass, or checking for special namedtuples attributes (_asdict, _replace, but this is fragile), or just not use class cheking, often unneeded in Python code.

Kind regards

@pwaller
Copy link
Mannequin Author

pwaller mannequin commented Jan 27, 2010

Hi Antoine and Eric,

Thanks for your responses.

I need to be able to catch all named tuples, I don't know in advance what instance of a namedtuple it might be, but I want my function to only accept named tuples. Is this unreasonable?

(I am actually writing a C interface which does conversion from namedtuples to a form of "RecordSpecification" to do with databases. The namedtuple is arbitrarily created by the user.)

@pitrou
Copy link
Member

pitrou commented Jan 27, 2010

I need to be able to catch all named tuples, I don't know in advance
what instance of a namedtuple it might be, but I want my function to
only accept named tuples. Is this unreasonable?

I don't know. Why exactly don't you want to accept something else?
Perhaps the user has their own namedtuple-like class. Or perhaps they're
fine with plain tuples.

@pwaller
Copy link
Mannequin Author

pwaller mannequin commented Jan 27, 2010

In this case, I need to have names for each object. It is also desirable to use the namedtuple because of their lightweight nature.

If they were to subclass the namedtuple I would be happy to accept that, but I really do want them to pass some form of namedtuple to me, to have some sort of consistency. The named tuple gives an obvious simple interface for determining which fields are available and the order they are in.

@amauryfa
Copy link
Member

It seems a perfect case for "duck typing" style of programming:
All namedtuple classes:

  • inherit from tuple
  • have a "_fields" class attribute
    These two properties could be the "duck test" for namedtuples, regardless of the actual implementation.

@pwaller
Copy link
Mannequin Author

pwaller mannequin commented Jan 28, 2010

Hi Amaury,

Thanks. I had heard of but never bothered to read about duck-typing before now; though I have used it passively before. I think it does make sense in this case. I can't imagine any case where checking for the _fields attribute would fail and isinstance(x, namedtuple) would not.

Besides which, for my current project I am forced to implement such a "workaround" anyway, so it doesn't affect me as such.

The only reason that remains why I would want it is that I often use isinstance(x, Y) to deal with different Ys, and that was the thing I intuitively wanted to use in this case as a python programmer for quite a few years now. This is probably a pretty weak reason, so I am happy to close this issue if the consensus points to duck typing.

@srid
Copy link
Mannequin

srid mannequin commented Jan 30, 2010

For more discussion on this, see http://stackoverflow.com/questions/2166818

@srid srid mannequin added the stdlib Python modules in the Lib dir label Jan 30, 2010
@rhettinger
Copy link
Contributor

Discussed this with GvR.
Here's a recap:

For 2.6 and 3.1 which are already released, check for the _fields attribute. This is a guaranteed part of the API is not fragile. For the C structures, check for the n_fields attribute.

In the future, the C API needs to grow to match the namedtuple() API and we should add an abstract base class describing the common API. Then you'll be able to write:
if isinstance(obj, abc_namedtuple).

@rhettinger
Copy link
Contributor

Am deferring this to Py3.3. There is a workaround available just using duck-typing and I would like to wait until more more has been done on StructSeq before setting committing to an new namedtuple Abstract Base Class (one released, it would be very hard to change). Also, I would like to see how pprint() evolves (because it may also want to have special handling for named tuples).

@merwok
Copy link
Member

merwok commented Sep 2, 2010

To make sure I understand: StructSeq is the C-pendent of named tuples, and a NamedTuple ABC would have to work with StructSeqs too?

@zuo
Copy link
Mannequin

zuo mannequin commented Mar 27, 2011

On python-ideas I have proposed an ABC being also a kind of a mix-in, potentially making namedtuple subclassing (with custom methods etc.) more convenient, e.g.:

    class MyRecord(namedtuple.abc):
        _fields = 'x y z'
        def _my_custom_method(self):
            return list(self._asdict().items())

or

    class MyAbstractRecord(namedtuple.abc):
        def _my_custom_method(self):
            return list(self._asdict().items())

    class AnotherAbstractRecord(MyAbstractRecord):
        def __str__(self):
            return '<<<{}>>>'.format(super().__str__())
 
    class MyRecord2(MyAbstractRecord):
        _fields = 'a, b'
 
    class MyRecord3(AnotherAbstractRecord):
        _fields = 'p', 'q', 'r'

Here is an experimental monkey-patcher adding the 'abc' attribute to namedtuple:

http://dpaste.org/T9w6/

I am not sure if it is worth preparing an actual patch based on it. If you think it is I could prepare one.

@zuo
Copy link
Mannequin

zuo mannequin commented Mar 27, 2011

PS. Newer, shorter version: http://dpaste.org/2aiQ/

@merwok
Copy link
Member

merwok commented Mar 27, 2011

Thanks for working on this. I have some remarks:

  1. Please post diff files here instead of using external sites. See http://docs.python.org/devguide/patch#preparation

  2. The license you chose doesn’t allow the PSF to include it into Python, see http://www.python.org/psf/contrib/contrib-form/

  3. abc looks like a module name, here something like NamedTupleABC or simply NamedTuple would be better. Note that this bug is only about type checking, the alternate form of defining named tuples thanks to this ABC is an additional feature; Raymond may reject it.

@zuo
Copy link
Mannequin

zuo mannequin commented Mar 28, 2011

Thank you. Raymond is against the idea so I don't know if it makes sense to create the real patch for now (it would patch the collections module and, I suppose, addming tests, docs etc.). Anyway, if somebody would be interested in the idea, the newest version of the draft is here: http://dpaste.org/qyKv/.

@zuo
Copy link
Mannequin

zuo mannequin commented Mar 31, 2011

@python-dev
Copy link
Mannequin

python-dev mannequin commented Mar 31, 2011

New changeset 7aa3f1f7ac94 by Raymond Hettinger in branch '3.2':
Issue bpo-7796: Add link to Jan Kaliszewski's alternate constructor and ABC for named tuples.
http://hg.python.org/cpython/rev/7aa3f1f7ac94

New changeset 330d3482cad8 by Raymond Hettinger in branch 'default':
Issue bpo-7796: Add link to Jan Kaliszewski's alternate constructor and ABC for named tuples.
http://hg.python.org/cpython/rev/330d3482cad8

@rhettinger
Copy link
Contributor

Detecting _fields is the simplest thing we can do right now. There are too many structseq API differences to warrant bringing them under a single ABC umbrella.

@ericsnowcurrently
Copy link
Member

It may not enough, but the use of namedtuples (vs. plain tuples) with functools.singledispatch() would be messier without a NamedTuple ABC (or other base type). Of course, when would you want to dispatch specifically on namedtuple? I can think of a few relatively weak uses, but not enough to justify the cost of establishing a common base class for namedtuple.

@ericsnowcurrently
Copy link
Member

A NamedTuple ABC doesn't have to define any API (so that part could wait?)[1]. I see it as most useful for isinstance checks. Here's a solution along those lines:

class NamedTuple(Sequence):
    @classmethod
    def __subclasshook__(cls, C):
        if cls is NamedTuple:
            if any("_fields" in B.__dict__ for B in C.__mro__):
                return True
        return NotImplemented

def namedtuple(...):
    ...
    NamedTuple.register(result)
    return result

(or include NamedTuple in the bases in the template)

For structseq support:

class NamedTuple(Sequence):
    @classmethod
    def __subclasshook__(cls, C):
        if cls is NamedTuple:
            if any("_fields" in B.__dict__ or  # for namedtuple
                   "n_fields" in B.__dict__  # for structseq
                   for B in C.__mro__):
                return True
        return NotImplemented

[1] I agree there is still a problem if we define an ABC here with no API and then add some later. Then classes that inherit from NamedTuple would break later when we add an API (just _fields?). Not sure how much that is a concern though. Then again we could just close out bpo-1820 and actually establish the API.

@abarnert
Copy link
Mannequin

abarnert mannequin commented Jan 13, 2014

I believe the structseq issues are a lot easier to solve than the appear. See bpo-20230, which adds a _fields member to every structseq type. Having done that, a NamedTuple ABC works on them just fine. And of course duck typing them without an ABC does too.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants