typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

add type annotations for 'docutils.parsers.rst.directives.images'

Open danieleades opened this issue 1 year ago • 2 comments

danieleades avatar Jul 23 '24 16:07 danieleades

@srittau docutils continues to surprise me...

i'm not sure how to annotate this-

class Image(Directive):

    align_h_values = ('left', 'center', 'right')
    align_v_values = ('top', 'middle', 'bottom')
    align_values = align_v_values + align_h_values
    loading_values = ('embed', 'link', 'lazy')

    def align(argument):
        # This is not callable as `self.align()`.  We cannot make it a
        # staticmethod because we're saving an unbound method in
        # option_spec below.
        return directives.choice(argument, Image.align_values)

    def loading(argument):
        # This is not callable as `self.loading()` (see above).
        return directives.choice(argument, Image.loading_values)

    required_arguments = 1
    optional_arguments = 0
    final_argument_whitespace = True
    option_spec = {'alt': directives.unchanged,
                   'height': directives.length_or_unitless,
                   'width': directives.length_or_percentage_or_unitless,
                   'scale': directives.percentage,
                   'align': align,
                   'target': directives.unchanged_required,
                   'loading': loading,
                   'class': directives.class_option,
                   'name': directives.unchanged}

you'll notice the 'align' and 'loading' methods are non-static methods that don't take 'self' as the first parameter. I guess docutils is just using the class as a namespace?

danieleades avatar Jul 23 '24 16:07 danieleades

Diff from mypy_primer, showing the effect of this PR on open source code:

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/directives/patches.py:10: note: In module imported here:
+ sphinx/directives/patches.py:30: error: Unused "type: ignore" comment  [unused-ignore]
+ sphinx/directives/patches.py: note: In member "run" of class "Figure":
+ sphinx/directives/patches.py:39:20: error: Incompatible return value type (got "Sequence[Node]", expected "list[Node]")  [return-value]

github-actions[bot] avatar Jul 23 '24 16:07 github-actions[bot]

Diff from mypy_primer, showing the effect of this PR on open source code:

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/directives/patches.py:10: note: In module imported here:
+ sphinx/directives/patches.py:33: error: Unused "type: ignore" comment  [unused-ignore]
+ sphinx/directives/patches.py: note: In member "run" of class "Figure":
+ sphinx/directives/patches.py:42:20: error: Incompatible return value type (got "Sequence[Node]", expected "list[Node]")  [return-value]

github-actions[bot] avatar Mar 15 '25 12:03 github-actions[bot]

@danieleades I'm sorry for the late answer. I would just ignore the pyright warnings using # pyright: ignore. Alternatively you could just mark those methods using @staticmethod in the stubs and silence stubtest.

srittau avatar Mar 16 '25 10:03 srittau