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

Suggestion: Integer fields should allow numpy integer types #398

Closed
louispotok opened this Issue Oct 19, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@louispotok
Copy link

louispotok commented Oct 19, 2018

When constructing an entity instance, a numpy integer type (eg np.int64) cannot be used in an integer field (an error is thrown on this line ). This is unintuitive to me, and also "unpythonic" in the sense that an object which provides an int-like interface should be treated as an int rather than being excluded through explicit type checking.

I tested the analogous behavior in SQLAlchemy and there, numpy int64s do work in integer fields.

I don't know what the best way to implement this is - one simple way might be to just compare whether val == int(val). Alternately, we could use the same approach that is taken with strings a few lines up - just attempt to cast it to an int.

Steps to reproduce:

from pony import orm
import numpy as np
db = orm.Database()
class Person(db.Entity):
    name = orm.Required(str)
    age = orm.Required(int)

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

#works fine
p1 = Person(name='John', age=20)

# throws TypeError
p2 = Person(name='Mary', age=np.int64(20))

Happy to post the full traceback or the analogous SQLAlchemy code if that would be helpful.

@sashaaero sashaaero closed this in b237258 Oct 20, 2018

@sashaaero

This comment has been minimized.

Copy link
Member

sashaaero commented Oct 20, 2018

Hello, @louispotok. Thanks for you suggestion. It should work now.

@louispotok

This comment has been minimized.

Copy link
Author

louispotok commented Oct 20, 2018

Thanks! And now I learned about __index__, that's a very sensible way to fix it.

kozlovsky added a commit that referenced this issue Jan 17, 2019

PonyORM release 0.7.7 (2019-01-17)
# Major features

* Array type support for PostgreSQL and SQLite
* isinstance() support in queries
* Support of queries based on collections: select(x for x in y.items)

# Other features

* Support of Entity.select(**kwargs)
* Support of SKIP LOCKED option in 'SELECT ... FOR UPDATE'
* New function make_proxy(obj) to make cros-db_session proxy objects
* Specify ON DELETE CASCADE/SET NULL in foreign keys
* Support of LIMIT in `SELECT FROM (SELECT ...)` type of queries
* Support for negative JSON array indexes in SQLite

# Improvements

* Improved query prefetching: use fewer number of SQL queries
* Memory optimization: deduplication of values recieved from the database in the same session
* increase DBAPIProvider.max_params_count value

# Bugfixes

* #405: breaking change with cx_Oracle 7.0: DML RETURNING now returns a list
* #380: db_session should work with async functions
* #385: test fails with python3.6
* #386: release unlocked lock error in SQLite
* #390: TypeError: writable buffers are not hashable
* #398: add auto coversion of numpy numeric types
* #404: GAE local run detection
* Fix Flask compatibility: add support of LocalProxy object
* db_session(sql_debug=True) should log SQL commands also during db_session.__exit__()
* Fix duplicated table join in FROM clause
* Fix accessing global variables from hybrid methods and properties
* Fix m2m collection loading bug
* Fix composite index bug: stackoverflow.com/questions/53147694
* Fix MyEntity[obj.get_pk()] if pk is composite
* MySQL group_concat_max_len option set to max of 32bit platforms to avoid truncation
* Show all attribute options in show(Entity) call
* For nested db_session retry option should be ignored
* Fix py_json_unwrap
* Other minor fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment