-
Notifications
You must be signed in to change notification settings - Fork 224
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
[BUG] id
is unknown type-wise in find
statements.
#1098
Comments
Looking a bit more into this, the problem is that accessing the field on the class type itself (not it's instance) gives you back an This behavior is nice, but there should be some typing support for this to avoid issues like this one. You might be able to resolve this with descriptors, but I'm not entirely sure how complex would doing so be. The workaround that I'm currently using is this: def expr(ex: Any) -> ExpressionField:
"""Convert type to ExpressionField.
When accessing Beanie documents, there is a difference between attempting to access
an attribute of the document class type itself and an attribute of the document instance.
Unfortunately, Beanie doesn't provide a way to distinguish between these two cases type-wise
and misinterprets the former as an attempt to access an attribute of the document instance,
which results in type checker using an incorrect type, often leading to type errors.
This function is essentially just a cast to ExpressionField, with a runtime isinstance check
as a safeguard against accidental misuse.
"""
if not isinstance(ex, ExpressionField):
raise TypeError(f"Expected ExpressionField, got {type(ex).__name__}")
return ex
# Using it like:
events = await (Event.find(expr(Event.attendees).id == user_id, fetch_links=True)).to_list() Obviously though, this isn't ideal and as far as workarounds go, this is a very annoying one. Note that I didn't just use cast since some type checkers also provide an incorrect cast check when the cast type isn't a supertype nor a subtype of the original. E.g.:
Also, a cast would require specifying the type each time, whereas this little helper function is a bit shorter & nicer, which is important considering how many times it needs to be used in Beanie code. |
Link.id
is unknown type-wiseid
is unknown type-wise in find
statements.
Hi, thank you for opening an issue and for looking more into this. There's multiple open issues on the same or similar subject. We are (or at least I'm) aware of it. This one 'hack' is a feature in Beanie (I'll use your example) which you rightly point out: my_cat = await Category.find(Category.user.id == user.id).to_list() (the arguments / syntax in .find()) For your other example (and modifying it a little for simplicity/to achieve the original code): events = await Event.find(Event.attendees.id == user_id, fetch_links=True).to_list() (the Event.attendees.id, attendees and id types not being recognized/correct) With all that said, I simply don't believe there is an easy fix for all this, unless we drastically change the interfaces / functionality here. |
Yeah, I do get that, introducing typing support for something that was never written with it in mind can be a challenge, especially for something as dynamic in nature as an ODM library. But it would be a shame to force users that do like typing to having to use alternative libs just to get proper support. It's hard to say whether a drastic change like this would be something even worth considering without knowing how the Beanie development is currently structured and whether the people involved are ready to do that and have enough experience with typed python to even be able to do that. Not to mention that it would be at a cost of likely breaking a lot of backwards compatibility just to add typing support. From my, admittedly short, use of Beanie, the typing support isn't terrible, but definitely lacks a lot things, which indeed probably wouldn't be an easy fix without a lot of rewrites. |
I am in the same boat as you, more or less, that's why I have opened #1050. |
Describe the bug
The docs suggest that a find statement like:
Should work regardless of whether
fetch_links
isTrue
orFalse
. And yes, on runtime, it does.However, on typing-time the
id
attribute is not recognized.To Reproduce
Using the pyright type checker:
Expected behavior
The
id
attribute should have a proper type definition.The text was updated successfully, but these errors were encountered: