Skip to content

Commit

Permalink
annotate methods with class_lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
kdmccormick committed Jun 14, 2024
1 parent d63c4b3 commit 473ffe0
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions xblock/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,39 +266,45 @@ def service_declaration(cls, service_name: str):
return cls._combined_services.get(service_name) # pylint: disable=no-member

@class_lazy
def _services_requested(cls): # pylint: disable=no-self-argument
def _services_requested(cls) -> dict[str, t.Any]: # pylint: disable=no-self-argument
"""
A per-class dictionary to store the services requested by a particular XBlock.
"""
return {}

@class_lazy
def _combined_services(cls): # pylint: disable=no-self-argument
def _combined_services(cls) -> dict[str, t.Any]: # pylint: disable=no-self-argument
"""
A dictionary that collects all _services_requested by all ancestors of this XBlock class.
"""
# The class declares what services it desires. To deal with subclasses,
# especially mixins, properly, we have to walk up the inheritance
# hierarchy, and combine all the declared services into one dictionary.
combined = {}
for parent in reversed(cls.mro()): # pylint: disable=no-member
combined: dict[str, t.Any] = {}
for parent in reversed(cls._mro()):
combined.update(getattr(parent, "_services_requested", {}))
return combined

@classmethod
def _mro(cls) -> list[type[t.Self]]:
"""
Silly helper for getting this object's/class's resolution order (MRO) without upsetting mypy or pylint.
"""
return cls.mro()

@class_lazy
def fields(cls): # pylint: disable=no-self-argument
def fields(cls) -> dict[str, Field]: # pylint: disable=no-self-argument
"""
A dictionary mapping the attribute name to the Field object for all Field attributes of the class.
"""
fields = {}
fields: dict[str, Field] = {}
# Loop through all of the baseclasses of cls, in
# the order that methods are resolved (Method Resolution Order / mro)
# and find all of their defined fields.
#
# Only save the first such defined field (as expected for method resolution)

bases = cls.mro() # pylint: disable=no-member
local = bases.pop(0)
bases = cls._mro()
local = bases.pop(0) # pylint: disable=no-member # pylint thinks `bases` is a tuple?

# First, descend the MRO from the top down, updating the 'fields' dictionary
# so that the dictionary always has the most specific version of fields in it
Expand Down Expand Up @@ -705,13 +711,12 @@ class XBlock(Plugin, Blocklike, metaclass=_HasChildrenMetaclass):
children: ReferenceListNotNone

@class_lazy
def _class_tags(cls): # pylint: disable=no-self-argument
def _class_tags(cls) -> set[str]: # pylint: disable=no-self-argument
"""
Collect the tags from all base classes.
"""
class_tags = set()

for base in cls.mro()[1:]: # pylint: disable=no-member
class_tags: set[str] = set()
for base in cls._mro()[1:]:
class_tags.update(getattr(base, '_class_tags', set()))

return class_tags
Expand Down

0 comments on commit 473ffe0

Please sign in to comment.