Skip to content

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

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

danieleades
Copy link
Contributor

No description provided.

@danieleades
Copy link
Contributor Author

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

This comment has been minimized.

Copy link
Contributor

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]

@srittau
Copy link
Collaborator

srittau commented Mar 16, 2025

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

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.

2 participants