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

feat: XBlock.usage_key, XBlock.context_key convenience props #690

Merged
merged 1 commit into from
Jan 24, 2024

Conversation

kdmccormick
Copy link
Member

@kdmccormick kdmccormick commented Nov 15, 2023

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 or course_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 use id versus key, 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

xblock/core.py Outdated Show resolved Hide resolved
@connorhaugh
Copy link
Contributor

Things I like:

  1. no (fewer) warnings :)
  2. the name of the var describes what it is and can possibly be.

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.

@ormsbee
Copy link
Contributor

ormsbee commented Nov 15, 2023

I'm good with it. I agree with usage_key and context_key.

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.

Agreed, but I think it's important that we do. There's a VideoBlock handler that currently breaks in content libraries right now because there's no course_id in a v2 library.

@bradenmacdonald
Copy link
Contributor

I agree, and I like the proposed block.usage_key and block.context_key.

I have a question in turn, which is: can we specify a type hint that these return UsageKey and LearningContextKey? That would be nice (assuming we also added a py.typed file to this repo), but would be wrong in the specific case of using the new .usage_key convenience property in the XBlock-SDK which returns some other usage ID format ( a string I think?).

@bradenmacdonald
Copy link
Contributor

these new names

Also FWIW, the names (mostly) aren't exactly new... block.scope_ids.usage_id is 9 years old, vs. block.location 10 years old :p Only context_key is somewhat new.

@feanil
Copy link
Contributor

feanil commented Nov 16, 2023

  • block.location -> block.usage_key
  • block.course_id -> block.context_key

This sounds good to me as well.

@kdmccormick
Copy link
Member Author

I have a question in turn, which is: can we specify a type hint that these return UsageKey and LearningContextKey? That would be nice (assuming we also added a py.typed file to this repo), but would be wrong in the specific case of using the new .usage_key convenience property in the XBlock-SDK which returns some other usage ID format ( a string I think?).

@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.

@jansenk
Copy link

jansenk commented Nov 16, 2023

My understanding was that "location" was a term we were trying to move away from. Am I incorrect?

@kdmccormick
Copy link
Member Author

@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).

@bradenmacdonald
Copy link
Contributor

@kdmccormick

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?

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.

@kdmccormick
Copy link
Member Author

kdmccormick commented Jan 12, 2024

@bradenmacdonald I opened #707 and #708. I think we can merge this PR without tackling those first. Let me know if you disagree.

@bradenmacdonald
Copy link
Contributor

Sounds good to me @kdmccormick 👍🏻

@kdmccormick kdmccormick marked this pull request as ready for review January 12, 2024 19:06
CHANGELOG.rst Outdated Show resolved Hide resolved
@kdmccormick
Copy link
Member Author

@bradenmacdonald @ormsbee @feanil @jansenk This is ready for review.

Copy link
Member Author

@kdmccormick kdmccormick left a 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
Copy link
Member Author

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:

Copy link
Contributor

@bradenmacdonald bradenmacdonald left a 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 Show resolved Hide resolved
xblock/core.py Outdated
@property
def context_key(self):
"""
Convenient abbreviation for `.XBlock.scope_ids.usage_id.context_key`.
Copy link
Contributor

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?

Copy link
Member Author

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
@kdmccormick kdmccormick merged commit fcff2e9 into master Jan 24, 2024
9 checks passed
@kdmccormick kdmccormick deleted the kdmccormick/usage-key-property branch January 24, 2024 14:33
kdmccormick referenced this pull request in openedx/edx-platform Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants