On 04/25/2017 05:50 PM, Mark Jones wrote:

I'm keying off hasattr(obj, 'keys') which is working. Still seems strange that it is returning a non-exported class though.

Python's own namedtuple() produces a custom class that itself is only a subclass of tuple:

>>> from collections import namedtuple
>>> n = namedtuple('hi', ['x', 'y', 'z'])
>>> n1 = n(1, 2, 3)
>>> type(n1)
<class '__main__.hi'>
>>> isinstance(n1, n)
True
>>> isinstance(n1, namedtuple)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
>>> isinstance(n1, tuple)
True

Above, there's no "exported" class we can compare this custom tuple against except for the ad-hoc class that is linked to the specific keys, there's no way to see that it was derived from namedtuple, which itself is not a class. SQLAlchemy's own named tuple object is going for the same type of thing - it's a tuple with keys on it, and that's all anyone needs to know. If SQLAlchemy began using the regular collections.namedtuple() approach (which we currently do not as it does not have the best performance characteristics for SQLA's use case), this would again break code that is tying to a specific kind of tuple implementation.

Here's a Python3 standard library example. the list_iterator type. Where is it exported?

>>> iter_ = iter([1, 2, 3])
>>> type(iter_)
<class 'list_iterator'>


Seems to claim it's in "builtins":

>>> type(iter_).__module__
'builtins'

but it's not!

>>> import builtins
>>> builtins.list_iterator
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'builtins' has no attribute 'list_iterator'


other things in "builtins" seem to be...

>>> getattr.__module__
'builtins'
>>> builtins.getattr
<built-in function getattr>


How do we test if something is a list_iterator then? Check it against the ABC in this case collections.Iterable:

>>> import collections
>>> isinstance(iter_, collections.Iterable)
True


list_iterator itself is not mentioned in the Python 3 documentation at all:

https://docs.python.org/3/search.html?q=list_iterator&check_keywords=yes&area=default


There's lots of precedent for libraries returning sequences, iterables, and mappings that themselves are not an "exported" type (I have recollections that this might be commonplace in libraries like lxml, numpy, but I'd have to test), but that one is from the standard library itself.







--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com <mailto:sqlalchemy+unsubscr...@googlegroups.com>. To post to this group, send email to sqlalchemy@googlegroups.com <mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to