Add class-based schema definitions #663
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR replaces the old syntax for defining ehrQL schemas e.g.
with a class-based one that looks like this:
There are some obvious peculiarities here (the decorator and the lowercase class name) which we'll come to later, but the primary motivation for this change is to make it possible to have full autocomplete for ehrQL when writing dataset definitions in VSCode. As far as I can see, this is only possible if we use classes to define the schema because no other syntax makes the column names available.
This change gives us autocomplete on table and column names, and also on frame methods like
exists_for_patient
,take
anddrop
. It doesn't yet give us autocomplete on series methods but I have a proof-of-concept which shows that this ought to be possible.The
@construct
decorator, and the lowercase class name are to avoid having to write something like this:Not only would that be more verbose and error prone, but it would leave the
Patients
class in the namespace where it would show as a (misleading) autocomplete option.