python-driver icon indicating copy to clipboard operation
python-driver copied to clipboard

refactor: slightly improve typing for cqlengine

Open VincentRPS opened this issue 3 years ago • 13 comments

This PR slightly improves typing for the CQLEngine. I am having a bit of trouble though, because of the weird way Model and Queries are done, its very hard to properly type model.object.* functions properly. Any ideas?

VincentRPS avatar Dec 22 '22 11:12 VincentRPS

seems like there's no way to properly type model.object.* functions so, as for what I see, this PR should be ready

VincentRPS avatar Dec 22 '22 11:12 VincentRPS

@VincentRPS I've let CI run on this change, but I think it might fail on python2

for reasons™️ we still need to support python2, so this might need to wait a bit, for when we can drop python2 support completely

fruch avatar Dec 27 '22 19:12 fruch

@VincentRPS

seems like it's failing with python3.7

2022-12-27T19:04:08.5435241Z ______________ ERROR collecting tests/unit/cqlengine/test_udt.py _______________
2022-12-27T19:04:08.5435657Z /project/tests/unit/cqlengine/test_udt.py:18: in <module>
2022-12-27T19:04:08.5436038Z     from cassandra.cqlengine.models import Model
2022-12-27T19:04:08.5436576Z /tmp/tmp.ALd4n4nucG/venv/lib/python3.7/site-packages/cassandra/cqlengine/models.py:24: in <module>
2022-12-27T19:04:08.5437001Z     from cassandra.cqlengine import query
2022-12-27T19:04:08.5437514Z /tmp/tmp.ALd4n4nucG/venv/lib/python3.7/site-packages/cassandra/cqlengine/query.py:1073: in <module>
2022-12-27T19:04:08.5437975Z     T = TypeVar('T', 'ModelQuerySet')
2022-12-27T19:04:08.5438399Z /opt/python/cp37-cp37m/lib/python3.7/typing.py:545: in __init__
2022-12-27T19:04:08.5438786Z     raise TypeError("A single constraint is not allowed")
2022-12-27T19:04:08.5439150Z E   TypeError: A single constraint is not allowed

fruch avatar Dec 29 '22 22:12 fruch

@VincentRPS

and as expected python2 doesn't support type annotations:

______________ ERROR collecting tests/unit/cqlengine/test_udt.py _______________
/opt/hostedtoolcache/Python/2.7.18/x64/lib/python2.7/site-packages/_pytest/python.py:507: in _importtestmodule
    mod = self.fspath.pyimport(ensuresyspath=importmode)
/opt/hostedtoolcache/Python/2.7.18/x64/lib/python2.7/site-packages/py/_path/local.py:704: in pyimport
    __import__(modname)
/opt/hostedtoolcache/Python/2.7.18/x64/lib/python2.7/site-packages/_pytest/assertion/rewrite.py:304: in load_module
    exec(co, mod.__dict__)
tests/unit/cqlengine/test_udt.py:18: in <module>
    from cassandra.cqlengine.models import Model
E     File "/home/runner/work/python-driver/python-driver/cassandra/cqlengine/models.py", line 90
E       def __get__(self, obj: Any, model: "BaseModel"):
E                            ^
E   SyntaxError: invalid syntax
=============================== warnings summary ===============================

fruch avatar Dec 29 '22 22:12 fruch

@VincentRPS

and as expected python2 doesn't support type annotations:

______________ ERROR collecting tests/unit/cqlengine/test_udt.py _______________
/opt/hostedtoolcache/Python/2.7.18/x64/lib/python2.7/site-packages/_pytest/python.py:507: in _importtestmodule
    mod = self.fspath.pyimport(ensuresyspath=importmode)
/opt/hostedtoolcache/Python/2.7.18/x64/lib/python2.7/site-packages/py/_path/local.py:704: in pyimport
    __import__(modname)
/opt/hostedtoolcache/Python/2.7.18/x64/lib/python2.7/site-packages/_pytest/assertion/rewrite.py:304: in load_module
    exec(co, mod.__dict__)
tests/unit/cqlengine/test_udt.py:18: in <module>
    from cassandra.cqlengine.models import Model
E     File "/home/runner/work/python-driver/python-driver/cassandra/cqlengine/models.py", line 90
E       def __get__(self, obj: Any, model: "BaseModel"):
E                            ^
E   SyntaxError: invalid syntax
=============================== warnings summary ===============================

hmm, seeing that I'd have to say it'd be better waiting until py2 support is dropped

VincentRPS avatar Jan 11 '23 10:01 VincentRPS

thanks @VincentRPS, I'll mark this one as pending for now

fruch avatar Jan 11 '23 10:01 fruch

@VincentRPS, now that we remove python2 support, we can try this again

fruch avatar Jul 18 '23 14:07 fruch

@VincentRPS, I would recommend send it also to the upstream cassandra (don't know when they would except it)

fruch avatar Jul 18 '23 14:07 fruch

Seems like those changes are also breaking python3.7, so for now we can't merge those

fruch avatar Jul 18 '23 15:07 fruch

@fruch I thought I had already replied here but I guess I didn't, it should work with 3.7 now since I did a little fix, though I could be mistaken.

VincentRPS avatar Aug 01 '23 13:08 VincentRPS

@fruch I thought I had already replied here but I guess I didn't, it should work with 3.7 now since I did a little fix, though I could be mistaken.

I'll let CI run again

fruch avatar Aug 01 '23 13:08 fruch

@fruch I thought I had already replied here but I guess I didn't, it should work with 3.7 now since I did a little fix, though I could be mistaken.

I'll let CI run again

I've rebase this one, and triggered CI again

fruch avatar May 30 '24 05:05 fruch

@VincentRPS could you send this to upstream? It would be better to do it there: less conflicts when merging + we don't really touch cqlengine code, so it's hard to do a proper review.

Lorak-mmk avatar Jun 18 '24 14:06 Lorak-mmk