|
| 1 | +Why is the keyword class hierarchy flat? |
| 2 | +======================================== |
| 3 | +*Wed 08 Mar 2023* |
| 4 | + |
| 5 | +In developing jschon, I’ve drawn a lot of inspiration from two great Python |
| 6 | +libraries that I use extensively in my day job: Pydantic and SQLAlchemy. |
| 7 | + |
| 8 | +Applications that use Pydantic or the ORM paradigm in SQLAlchemy are easy to |
| 9 | +spot: you’ll find a collection of declarative models, each derived from a |
| 10 | +quintessential base class provided by the library - :class:`BaseModel` in Pydantic, |
| 11 | +:class:`DeclarativeBase` in SQLAlchemy. A given application might define a model |
| 12 | +hierarchy to roll up common behaviours, but that would be private to the |
| 13 | +application and, in general, should not affect the relationships between the |
| 14 | +declarative models and other foundational entities in the library. One of the |
| 15 | +great things about implementing declarative models using these libraries is |
| 16 | +that I know that I can just derive from :class:`BaseModel` or :class:`DeclarativeBase`, |
| 17 | +I don't have to know which is the most appropriate abstract subclass to use, |
| 18 | +and I don't need to be aware of any variance in implementation higher up in the |
| 19 | +hierarchy. Another great thing is that each model is completely self-contained |
| 20 | +and completely self-descriptive. |
| 21 | + |
| 22 | +In jschon, keywords are analogous to declarative models, with :class:`Keyword` |
| 23 | +being the quintessential base class. One might argue that declarative |
| 24 | +models are conceptually more analogous to schemas, and model fields to |
| 25 | +keywords, but I'm looking at things from a programming workflow perspective: as |
| 26 | +declarative models are the building blocks of a Pydantic / SQLAlchemy |
| 27 | +application, so keywords are the building blocks of a vocabulary |
| 28 | +implementation. Taking the analogy further, a given vocabulary might define a |
| 29 | +keyword class hierarchy to roll up common behaviours, but that should be |
| 30 | +private to the module, extension or application that implements the vocabulary, |
| 31 | +and should not affect the underlying relationships between keywords and other |
| 32 | +foundational entities in the jschon library. From the perspective of those |
| 33 | +other entities, a keyword is a keyword is a keyword, and from the perspective |
| 34 | +of the vocabulary, a keyword is self-contained and self-descriptive. |
| 35 | + |
| 36 | +*marksparkza* |
0 commit comments