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

getattr(myclass,key) not working if key changes #223

Closed
simplynail opened this Issue Jan 17, 2017 · 5 comments

Comments

Projects
None yet
4 participants
@simplynail

simplynail commented Jan 17, 2017

I'm doing a 'DataWarehouse' from multiple MS Excel files. From each workbook I get separate "tables_data" that I then add to the database to proper tables.
I get error when referring to dynamically assigned attribute_name via getattr, it looks like it uses old/previous value of attribute_name

Here's models declaration:

class MyClass1(db.Entity):
    _table_ = 'table_name1'

    id = orm.PrimaryKey(int, auto=True)
    name = orm.Required(str, nullable=True)
    other_attr = orm.Required(str, nullable=True)

class MyClass2(db.Entity):
    _table_ = 'table_name2'

    id = orm.PrimaryKey(int, auto=True)
    number = orm.Required(str, nullable=True)
    other_attr = orm.Required(str, nullable=True)

class MyClass3(db.Entity):
    _table_ = 'table_name3'

    id = orm.PrimaryKey(int, auto=True)
    number = orm.Required(str, nullable=True)
    other_attr = orm.Required(str, nullable=True)

and app code is:

detail_classes = [myClass1, myClass2, myClass3]
tables_data = {'table_name1':
                        [{'name':'xy','other_attr':'some_value1'},
                         {'name':'yz','other_attr':'some_value2'}],
                   'table_name2':
                        [{'number':'ab','other_attr':'some_value3'},
                         {'number':'bc','other_attr':'some_value4'}],
                   'table_name3':
                        [{'number':'hi','other_attr':'some_value5'},
                         {'number':'ij','other_attr':'some_value6'}]}

for cls_template in detail_classes:
            # select data for current class template
            data = tables_data[cls_template._table_] 
            # set column name that is unique for current dict
            if cls_template._table_ != 'table_name1':
                key = 'name'
            else:
                key = 'number'

so when I do:
selected = list(cls_template.select(lambda c: getattr(c, key) == row[key]))

I get:
AttributeError: Entity MyClass2 does not have attribute name: getattr(c, key)

Even though current key == 'number'

@andreymal

This comment has been minimized.

Show comment
Hide comment
@andreymal

andreymal Jul 25, 2017

Same problem for me

andreymal commented Jul 25, 2017

Same problem for me

@kozlovsky

This comment has been minimized.

Show comment
Hide comment
@kozlovsky

kozlovsky Sep 6, 2017

Member

I cannot reproduce this bug. The tests I tried works without any problem. On the other side, the code presented contains mistakes. For example, the last if condition looks wrong: it should use == instead of !=.

I suspect, the problem was in application logic and not in Pony implementation of getattr.

Member

kozlovsky commented Sep 6, 2017

I cannot reproduce this bug. The tests I tried works without any problem. On the other side, the code presented contains mistakes. For example, the last if condition looks wrong: it should use == instead of !=.

I suspect, the problem was in application logic and not in Pony implementation of getattr.

@kozlovsky kozlovsky self-assigned this Sep 6, 2017

@andreymal

This comment has been minimized.

Show comment
Hide comment
@andreymal

andreymal Sep 6, 2017

I'll try to write test code later.

My code has no logic errors; if I replace lambda x: ... to eval('lambda x: ...'), then all works good (but memory leaks of course :)

andreymal commented Sep 6, 2017

I'll try to write test code later.

My code has no logic errors; if I replace lambda x: ... to eval('lambda x: ...'), then all works good (but memory leaks of course :)

@andreymal

This comment has been minimized.

Show comment
Hide comment
@andreymal

andreymal Sep 6, 2017

It's really hard to reproduce it >_<

I don't know if this is case of author of this issue, but this is my case:

from pony.orm import *

db = Database()


class Test1(db.Entity):
    foo = Required(str, 4)
    bar = Required(str, 4)
    PrimaryKey(foo, bar)  # optional: can be removed


class Test2(db.Entity):
    pass


class Test3(db.Entity):
    foo = Required(str, 4)
    bar = Required(str, 4)
    PrimaryKey(foo, bar)  # required


db.bind('sqlite', ':memory:')
db.generate_mapping(create_tables=True)


with db_session:
    for entity, attr in [
        (Test1, 'foo'),
        (Test2, 'id'),
        (Test3, 'foo'),
    ]:
        print('Entity: {}, attr: {}, exists: {}'.format(
            entity, attr, hasattr(entity, attr)
        ))
        entity.select(lambda x: getattr(x, attr) == 'foo')
        # replace last line to:
        # entity.select(eval("lambda x: getattr(x, attr) == 'foo'"))
        # and everything will work
Entity: <class '__main__.Test1'>, attr: foo, exists: True
Entity: <class '__main__.Test2'>, attr: id, exists: True
Entity: <class '__main__.Test3'>, attr: foo, exists: True
Traceback (most recent call last):
AttributeError: Entity Test3 does not have attribute id: getattr(x, attr)

andreymal commented Sep 6, 2017

It's really hard to reproduce it >_<

I don't know if this is case of author of this issue, but this is my case:

from pony.orm import *

db = Database()


class Test1(db.Entity):
    foo = Required(str, 4)
    bar = Required(str, 4)
    PrimaryKey(foo, bar)  # optional: can be removed


class Test2(db.Entity):
    pass


class Test3(db.Entity):
    foo = Required(str, 4)
    bar = Required(str, 4)
    PrimaryKey(foo, bar)  # required


db.bind('sqlite', ':memory:')
db.generate_mapping(create_tables=True)


with db_session:
    for entity, attr in [
        (Test1, 'foo'),
        (Test2, 'id'),
        (Test3, 'foo'),
    ]:
        print('Entity: {}, attr: {}, exists: {}'.format(
            entity, attr, hasattr(entity, attr)
        ))
        entity.select(lambda x: getattr(x, attr) == 'foo')
        # replace last line to:
        # entity.select(eval("lambda x: getattr(x, attr) == 'foo'"))
        # and everything will work
Entity: <class '__main__.Test1'>, attr: foo, exists: True
Entity: <class '__main__.Test2'>, attr: id, exists: True
Entity: <class '__main__.Test3'>, attr: foo, exists: True
Traceback (most recent call last):
AttributeError: Entity Test3 does not have attribute id: getattr(x, attr)

@sashaaero sashaaero added bug and removed cannot-reproduce labels Sep 13, 2017

@kozlovsky

This comment has been minimized.

Show comment
Hide comment
@kozlovsky

kozlovsky Sep 14, 2017

Member

Fixed in d1ca677!
It was a tricky issue, thank you @simplynail and @andreymal for the test cases!

Member

kozlovsky commented Sep 14, 2017

Fixed in d1ca677!
It was a tricky issue, thank you @simplynail and @andreymal for the test cases!

@kozlovsky kozlovsky closed this Sep 14, 2017

@kozlovsky kozlovsky added this to the 0.7.3 milestone Sep 14, 2017

kozlovsky added a commit that referenced this issue Oct 23, 2017

Pony ORM Release 0.7.3 (2017-10-23)
# New features

* `where()` method added to query
* `coalesce()` function added
* `between(x, a, b)` function added
* #295: Add `_table_options_` for entity class to specify engine, tablespace, etc.
* Make debug flag thread-local
* `sql_debugging` context manager added
* `sql_debug` and show_values arguments to db_session added
* `set_sql_debug` function added as alias to (to be deprecated) `sql_debug` function
* Allow `db_session` to accept `ddl` parameter when used as context manager
* Add `optimistic=True` option to db_session
* Skip optimistic checks for queries in `db_session` with `serializable=True`
* `fk_name` option added for attributes in order to specify foreign key name
* #280: Now it's possible to specify `timeout` option, as well as pass other keyword arguments for `sqlite3.connect` function
* Add support of explicit casting to int in queries using `int()` function
* Added modulo division % native support in queries

# Bugfixes

* Fix bugs with composite table names
* Fix invalid foreign key & index names for tables which names include schema name
* For queries like `select(x for x in MyObject if not x.description)` add "OR x.info IS NULL" for nullable string columns
* Add optimistic checking for `delete()` method
* Show updated attributes when `OptimisticCheckError` is being raised
* Fix incorrect aliases in nested queries
* Correctly pass exception from user-defined functions in SQLite
* More clear error messages for `UnrepeatableReadError`
* Fix `db_session(strict=True)` which was broken in 2d3afb2
* Fixes #170: Problem with a primary key column used as a part of another key
* Fixes #223: incorrect result of `getattr(entity, attrname)` when the same lambda applies to different entities
* Fixes #266: Add handler to `"pony.orm"` logger does not work
* Fixes #278: Cascade delete error: FOREIGN KEY constraint failed, with complex entity relationships
* Fixes #283: Lost Json update immediately after object creation
* Fixes #284: `query.order_by()` orders Json numbers like strings
* Fixes #288: Expression text parsing issue in Python 3
* Fixes #293: translation of if-expressions in expression
* Fixes #294: Real stack traces swallowed within IPython shell
* `Collection.count()` method should check if session is alive
* Set `obj._session_cache_` to None after exiting from db session for better garbage collection
* Unload collections which are not fully loaded after exiting from db session for better garbage collection
* Raise on unknown options for attributes that are part of relationship
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment