Skip to content
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

Expose descriptor object annotation hints to containing classes. #12

Open
amcgregor opened this issue Oct 21, 2019 · 1 comment
Open

Comments

@amcgregor
Copy link
Member

amcgregor commented Oct 21, 2019

Trivial example:

from marrow.mongo import Document
from marrow.mongo.field import String

class Sample(Document):
	name:str = String()

This should stand out, glaringly, as a DRY failure. We are assigning a data descriptor object explicitly to handle strings, why must we declare, then, that instances contain strings within this attribute? The trivial example hides the scope of the majesty of this problem. A better example:

from bson import ObjectId as OID
from datetime import datetime, timedelta
from collections.abc import MutableMapping

from marrow.mongo import Document
from marrow.mongo.field import ObjectId

class Sample(Document):
	name:Union[OID,datetime,timedelta,MutableMapping,str,bytes] = ObjectId()

Ruh-roh! Requiring developers using Marrow Mongo to declare such a Union on every ObjectId field (even if abbreviated, e.g. through assignment of that Union to a variable) is a very bad idea. The descriptor itself knows what values it can handle, and what it will hand out. (Additionally, until lazy evaluation of annotations is more widespread, it'd be nice to not force those extra imports on people.)

Introduced in c2a4554 and patched in 98a0f5d to permit preservation of explicit annotations, remaining are tests and documentation.

The Attribute subclasses implementing fields may then declare an annotation attribute, which will be hoisted up into the containing class' __annotations__.

MCVE. Demonstration of intent.

@amcgregor
Copy link
Member Author

Partially implemented in develop, refinement to final form in #13 as utilized by the next branch of marrow.mongo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant