Comments (9)
Hi @marcuslimdw,
This is not at all a bad idea. However, if that relies on something which works only on Python 3.12, it's going to heavily reduce the number of Python versions we can support. Currently, we go all the way from Python 3.8 to Python 3.11. So, if a feature requires features that are available only since Python 3.12, we unfortunately cannot adopt it.
Maybe I've misunderstood the limitation to Python 3.12, in which case let me know. I've labeled this as an enhancement so we can keep it around.
from python-arango.
@apetenchea the "inline type parameter" syntax (that's how I think of it) is only available in 3.12, so to support >=3.8, we can use the older syntax instead.
The above example might look like this in 3.8:
T = TypeVar("T")
class Serializer(Protocol, Generic[T]):
def __call__(self, value: T) -> Json:
pass
class ArangoClient(Generic[T]):
def __init__(self, serializer: Serializer[T], *args, **kwargs):
pass
def db(self, *args, **kwargs) -> Database[T]:
pass
class StandardDatabase(Generic[T]):
def collection(self, *args, **kwargs) -> Collection[T]:
pass
class StandardCollection(Generic[T]):
def insert(self, document: T, *args, **kwargs): # no longer Json
pass
from python-arango.
I agree that having the Json
type hint seems overly restrictive. As you pointed out, one can implement a custom serialiser, which might work with completely different data from Json
(serialise from bytes, for example).
However, I have thought about it carefully and I am not convinced that going full generic is exactly what we need right now. The reason for having Json
in there is not to enforce (the Python ignores type hinting), but to give an idea on how the method is supposed to be used. So while going fully generic would indeed silence potential false-positive type-related warnings, it would render the whole type-hinting thing useless. There are other ways to work around such false positives, apart from modifying the driver.
I'm not adamant against it, and I can see why would someone opt for your proposed solution, but I'm wondering if there's other benefits you see that this potential change could bring to the driver.
from python-arango.
Perhaps it might help if I explained my use case.
At work, we use ArangoDB for storage, but our data models are defined with Pydantic. We've created a custom serialiser that allows us to pass Pydantic models directly to, for example, aql.execute
, or insert_many
.
We also use Pyright for type checking, which necessarily fails because these methods expect builtin Python objects, but get Pydantic models. To put it another way, we're widening the types that the AQL
/Collection
methods can take at runtime, but the compile-time types stay the same, causing type checking failure. To me, this is exactly what generics are meant for.
An example that doesn't use Pydantic:
import json
from datetime import datetime
from datetime import timezone
from typing import Any
from arango.client import ArangoClient
# define a custom serialiser that can also handle datetimes
def serialiser(data: Any) -> str:
if isinstance(data, datetime):
return data.isoformat()
return json.dumps(data)
ArangoClient(serializer=serialiser).db().aql.execute(
"INSERT @document INTO @@collection", bind_vars={"document": {"timestamp": datetime.now(tz=timezone.utc)}, "@collection": "my_collection"}
)
This works at runtime, but fails type checking:
error: Argument of type "dict[str, dict[str, datetime]]" cannot be assigned to parameter "bind_vars" of type "MutableMapping[str, DataTypes] | None" in function "execute"
Type "dict[str, dict[str, datetime]]" cannot be assigned to type "MutableMapping[str, DataTypes] | None"
"dict[str, dict[str, datetime]]" is incompatible with "MutableMapping[str, DataTypes]"
Type parameter "_VT@MutableMapping" is invariant, but "dict[str, datetime]" is not the same as "DataTypes"
"dict[str, dict[str, datetime]]" is incompatible with "None" (reportArgumentType)
There are other ways to work around such false positives, apart from modifying the driver.
Could you please suggest an alternative? I'd be happy to hear any possible solution to my problem.
from python-arango.
Related Issues (20)
- Documentation issue: ordered/geoJSON on collection.add_geo_index HOT 1
- Authenticate using User JWT-Token HOT 1
- Custom JSON serializer not being used for bulk operations HOT 3
- Edges not retrievable without committing transaction HOT 2
- python-arango is not working without pip-install installation HOT 3
- Content in API docs of branch `main` disappeared HOT 1
- merge functionality HOT 1
- Replacement for `pkg_resources` HOT 1
- Transactions Failing on Collection Deletion HOT 3
- Unable to disable request timeout HOT 2
- Requests with endpoint="/_api/simple/..." fail HOT 10
- API Specification Page is not rendering
- Collection.find does not work in 7.6.1 if filter attribute is set to none HOT 3
- Enum can't be serialized anymore after upgrade to 7.6.2 HOT 2
- bind_vars type hints are incomplete HOT 4
- broken build_filter_conditions for keys with spaces // backtick escaping missing? HOT 2
- Unix socket support HOT 5
- feature request: Continue an existing transaction HOT 3
- `arango.exceptions.GraphTraverseError: [HTTP 404][ERR 404] unknown path '/_api/traversal'` when using `traverse` (arangodb 3.12) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-arango.