-
Notifications
You must be signed in to change notification settings - Fork 217
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
feat: XBlock.usage_key, XBlock.context_key convenience props #690
Conversation
Things I like:
Therefore, I think your changes make sense. That being said, course_id -> context_key will require some evangelization. "Course id" is common parlance, and there certainly are a broad variety of contexts outside of xblocks where it is used. A line as to where context_key is used as opposed to course_id will require good, visible documentation. |
I'm good with it. I agree with
Agreed, but I think it's important that we do. There's a |
I agree, and I like the proposed I have a question in turn, which is: can we specify a type hint that these return |
Also FWIW, the names (mostly) aren't exactly new... |
This sounds good to me as well. |
@bradenmacdonald Oof, I had no idea that xblock-sdk's runtime had different types for scope_ids. Is that difference considered a legit feature of the XBlock framework, or was it just that the opaque-keys project never got around to moving xblock-sdk from strings to keys? I love adding type hints, but I don't want to add ones that will be wrong in certain cases. |
My understanding was that "location" was a term we were trying to move away from. Am I incorrect? |
@jansenk You're correct. It's just that the current replacement is unwieldy (block.scope_ids.usage_id). This PR would make the replacement nicer (block.usage_key). |
You can see the details here: https://github.com/openedx/xblock-sdk/blob/529c0c87a740e39f12ae42ec9ff735e198d983f1/workbench/runtime.py#L100-L157 The XBlock library has never cared what format your Scope IDs are in; they can be anything. XBlock SDK used strings, and edx-platform uses opaque keys. The XBlock library also requires a "definition ID" as one of the Scope IDs and requires some usage of it in the API, though that hasn't proven to be a very useful concept, and Learning Core may not use it at all. I think it would be reasonable at this point to change the XBlock SDK core lib to require that the scope IDs are opaque keys, and change the XBlock SDK accordingly. The XBlock SDK doesn't have to use the same opaque keys as edx-platform; since it uses scenarios instead of courses for example it could use its own type of usage key that references a scenario as its context, not a course. I think that would be a nice win for consistency if we're using opaque keys across the board, and let use do type hints. |
@bradenmacdonald I opened #707 and #708. I think we can merge this PR without tackling those first. Let me know if you disagree. |
Sounds good to me @kdmccormick 👍🏻 |
e03418a
to
5a2b33b
Compare
f25efe0
to
5410e4e
Compare
@bradenmacdonald @ormsbee @feanil @jansenk This is ready for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for reviewers.
@@ -1,6 +1,7 @@ | |||
# Core requirements for using this package | |||
-c constraints.txt | |||
|
|||
edx-opaque-keys |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added opaque-keys as a dependency for the new unit tests. Normally I wouldn't add a base dependency just for a couple tests, but we anticipate to make the repo depend on opaque-keys soon anyway, so this seemed like a good step:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't run this, but the code LGTM. Thanks so much for taking this on, @kdmccormick !
xblock/core.py
Outdated
@property | ||
def context_key(self): | ||
""" | ||
Convenient abbreviation for `.XBlock.scope_ids.usage_id.context_key`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: On the first line of the docstring, I would prefer to say something more helpful like "This is the ID of the context (course, library, etc.) that contains this XBlock". Then, you can mention that it's an alias.
Same above for usage_key
/usage_id
- what is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, I'll make that change.
Add two new properties to `XBlock` objects: `.usage_key` and `.context_key``. These simply expose the values of `.scope_ids.usage_id`` and `.scope_ids.usage_id.context_key`, providing a convenient replacement to the deprecated, edx-platform-specific block properties `.location` and `.course_id`, respectively. Note: this adds opaque-keys as a dependency for some new unit tests. Normally I wouldn't add a dependency just for a couple tests, but we anticipate to make the repo depend on opaque-keys soon anyway: * #707 * #708 Bumps version from 1.9.1 to 1.10.0
5410e4e
to
d98b85e
Compare
Reasoning
According to these warnings, we should eventually make these updates across the board:
block.location
->block.scope_ids.usage_id
block.course_id
->block.scope_ids.usage_id.context_key
The thing is... these new names are (subjectively, of course) not very good 😄 They take longer to type out, they take up more space on a line, and IMO are more intimidating to devs who don't have their heads way into XBlock land. Every time I update code from
location
orcourse_id
to the new, longer name, I find the resulting line harder to read. It also took me a long time to remember whether to useid
versuskey
, which is maddeningly inconsistent in the phrase "block.scope_ids.usage_id.context_key".I propose:
block.location
->block.usage_key
block.course_id
->block.context_key
I'd also be open to:
block.location
->block.usage_id
block.course_id
->block.context_id
or, if we want the interface to remind people that a context key is, conceptually, just a component of a usage key:
block.location
->block.usage_key
block.course_id
->block.usage_key.context_key
To Do