Skip to content

Commit

Permalink
refactor: make default select use overrides, raise if overridden bloc…
Browse files Browse the repository at this point in the history
…k not found
  • Loading branch information
nsprenkle committed Aug 14, 2024
1 parent 4c6885e commit f4d69e5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
15 changes: 8 additions & 7 deletions xblock/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, all_entry_points):
super().__init__(msg)


def default_select(identifier, all_entry_points): # pylint: disable=inconsistent-return-statements
def _default_select_no_override(identifier, all_entry_points): # pylint: disable=inconsistent-return-statements
"""
Raise an exception when we have ambiguous entry points.
"""
Expand All @@ -41,7 +41,7 @@ def default_select(identifier, all_entry_points): # pylint: disable=inconsisten
raise AmbiguousPluginError(all_entry_points)


def select_with_overrides(identifier, all_entry_points):
def default_select(identifier, all_entry_points):
"""
Honors the ability for an XBlock to override the default entry point.
Raise an exception when we have ambiguous entry points.
Expand All @@ -57,14 +57,15 @@ def select_with_overrides(identifier, all_entry_points):
else:
block_entry_points.append(block_entry_point)

# Overrides get priority
# Get the default entry point
default_plugin = _default_select_no_override(identifier, block_entry_points)

# If we have an unambiguous override, that gets priority. Otherwise, return default.
if len(overrides) == 1:
return overrides[0]
elif len(overrides) > 1:
raise AmbiguousPluginError(all_entry_points)

# ... then default behavior
return default_select(identifier, block_entry_points)
return default_plugin


class Plugin:
Expand Down Expand Up @@ -124,7 +125,7 @@ def select(identifier, all_entry_points):
if key not in PLUGIN_CACHE:

if select is None:
select = select_with_overrides
select = default_select

all_entry_points = [
*importlib.metadata.entry_points(group=f'{cls.entry_point}.overrides', name=identifier),
Expand Down
8 changes: 1 addition & 7 deletions xblock/test/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,8 @@ def boom(identifier, entry_points):


@XBlock.register_temp_plugin(OverriddenBlock, "overridden_block", group='xblock.v1.overrides')
@XBlock.register_temp_plugin(AmbiguousBlock1, "overridden_block")
@XBlock.register_temp_plugin(AmbiguousBlock2, "overridden_block")
@XBlock.register_temp_plugin(UnambiguousBlock, "good_block")
@XBlock.register_temp_plugin(UnambiguousBlock, "overridden_block")
def test_plugin_override():
# We can load ok blocks even if there are bad blocks.
cls = XBlock.load_class("good_block")
assert cls is UnambiguousBlock

# Trying to load a block that is overridden returns the correct override
override = XBlock.load_class("overridden_block")
assert override is OverriddenBlock
Expand Down

0 comments on commit f4d69e5

Please sign in to comment.