1111import warnings
1212import typing as t
1313from collections import defaultdict
14+ from xml .etree import ElementTree as ET
1415
1516import pkg_resources
1617from opaque_keys .edx .keys import LearningContextKey , UsageKey
1718from web_fragments .fragment import Fragment
1819
1920import xblock .exceptions
2021from xblock .exceptions import DisallowedFileError
21- from xblock .fields import String , List , Scope
22+ from xblock .fields import String , List , Scope , ScopeIds
23+ from xblock .field_data import FieldData
2224from xblock .internal import class_lazy
2325import xblock .mixins
2426from xblock .mixins import (
3335from xblock .plugin import Plugin
3436from xblock .validation import Validation
3537
38+
39+ if t .TYPE_CHECKING :
40+ from xblock .runtime import Runtime
41+
3642# exposing XML_NAMESPACES as a member of core, in order to avoid importing mixins where
3743# XML_NAMESPACES are needed (e.g. runtime.py).
3844XML_NAMESPACES = xblock .mixins .XML_NAMESPACES
@@ -87,7 +93,7 @@ def get_i18n_js_namespace(cls) -> str | None:
8793 return cls .i18n_js_namespace
8894
8995 @classmethod
90- def open_local_resource (cls , uri : str ) -> t .BinaryIO :
96+ def open_local_resource (cls , uri : str ) -> t .IO [ bytes ] :
9197 """
9298 Open a local resource.
9399
@@ -143,11 +149,11 @@ class XBlock(XmlSerializationMixin, HierarchyMixin, ScopedStorageMixin, RuntimeS
143149 tags = List (help = "Tags for this block" , scope = Scope .settings )
144150
145151 @class_lazy
146- def _class_tags (cls : type [XBlock ]) -> set [str ]: # pylint: disable=no-self-argument
152+ def _class_tags (cls : type [XBlock ]) -> set [str ]: # type: ignore[misc]
147153 """
148154 Collect the tags from all base classes.
149155 """
150- class_tags = set ()
156+ class_tags : set [ str ] = set ()
151157
152158 for base in cls .mro ()[1 :]: # pylint: disable=no-member
153159 class_tags .update (getattr (base , '_class_tags' , set ()))
@@ -165,7 +171,9 @@ def dec(cls: type[XBlock]) -> type[XBlock]:
165171 return dec
166172
167173 @classmethod
168- def load_tagged_classes (cls , tag , fail_silently = True ) -> t .Iterable [type [XBlock ]]:
174+ def load_tagged_classes (
175+ cls , tag : str , fail_silently : bool = True
176+ ) -> t .Iterable [tuple [str , type [XBlock ]]]:
169177 """
170178 Produce a sequence of all XBlock classes tagged with `tag`.
171179
@@ -178,18 +186,17 @@ def load_tagged_classes(cls, tag, fail_silently=True) -> t.Iterable[type[XBlock]
178186 (e.g. on startup or first page load), and in what
179187 contexts. Hence, the flag.
180188 """
181- # Allow this method to access the `_class_tags`
182- # pylint: disable=W0212
183189 for name , class_ in cls .load_classes (fail_silently ):
184- if tag in class_ ._class_tags :
185- yield name , class_
190+ xblock_class : type [XBlock ] = class_ # type: ignore
191+ if tag in xblock_class ._class_tags : # pylint: disable=protected-access
192+ yield name , xblock_class
186193
187194 # pylint: disable=keyword-arg-before-vararg
188195 def __init__ (
189196 self ,
190197 runtime : Runtime ,
191198 field_data : FieldData | None = None ,
192- scope_ids : ScopeIds = UNSET ,
199+ scope_ids : ScopeIds | object = UNSET ,
193200 * args ,
194201 ** kwargs
195202 ):
@@ -264,7 +271,7 @@ def ugettext(self, text) -> str:
264271 runtime_ugettext = runtime_service .ugettext
265272 return runtime_ugettext (text )
266273
267- def add_xml_to_node (self , node : etree .Element ) -> None :
274+ def add_xml_to_node (self , node : ET .Element ) -> None :
268275 """
269276 For exporting, set data on etree.Element `node`.
270277 """
@@ -273,18 +280,19 @@ def add_xml_to_node(self, node: etree.Element) -> None:
273280 self .add_children_to_node (node )
274281
275282
276- XBlockAsideView : t .TypeAlias = t .Callable [[XBlockAside , XBlock , dict | None ], Fragment ]
283+ # An XBlockAside's view method takes itself, an XBlock, and optional context dict,
284+ # and returns a Fragment.
285+ AsideView = t .Callable [["XBlockAside" , XBlock , t .Optional [dict ]], Fragment ]
277286
278287
279288class XBlockAside (XmlSerializationMixin , ScopedStorageMixin , RuntimeServicesMixin , HandlersMixin , SharedBlockBase ):
280289 """
281290 This mixin allows Xblock-like class to declare that it provides aside functionality.
282291 """
283-
284292 entry_point : str = "xblock_asides.v1"
285293
286294 @classmethod
287- def aside_for (cls , view_name : str ) -> t .Callable [[XBlockAsideView ], XBlockAsideView ]:
295+ def aside_for (cls , view_name : str ) -> t .Callable [[AsideView ], AsideView ]:
288296 """
289297 A decorator to indicate a function is the aside view for the given view_name.
290298
@@ -297,11 +305,11 @@ def student_aside(self, block, context=None):
297305
298306 """
299307 # pylint: disable=protected-access
300- def _decorator (func : XBlockAsideView ) -> XBlockAsideView :
308+ def _decorator (func : AsideView ) -> AsideView :
301309 if not hasattr (func , '_aside_for' ):
302- func ._aside_for = []
310+ func ._aside_for = [] # type: ignore
303311
304- func ._aside_for .append (view_name ) # pylint: disable=protected-access
312+ func ._aside_for .append (view_name ) # type: ignore
305313 return func
306314 return _decorator
307315
@@ -321,14 +329,14 @@ def _combined_asides(cls) -> dict[str, str | None]: # pylint: disable=no-self-a
321329 """
322330 # The method declares what views it decorates. We rely on `dir`
323331 # to handle subclasses and overrides.
324- combined_asides = defaultdict (None )
332+ combined_asides : dict [ str , str | None ] = defaultdict (None )
325333 for _view_name , view_func in inspect .getmembers (cls , lambda attr : hasattr (attr , '_aside_for' )):
326334 aside_for = getattr (view_func , '_aside_for' , [])
327335 for view in aside_for :
328336 combined_asides [view ] = view_func .__name__
329337 return combined_asides
330338
331- def aside_view_declaration (self , view_name : str ) -> XBlockAsideView | None :
339+ def aside_view_declaration (self , view_name : str ) -> AsideView | None :
332340 """
333341 Find and return a function object if one is an aside_view for the given view_name
334342
0 commit comments