From 3a03bf37cfc12768516b29899c1e33efa80ffa26 Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Sat, 26 Feb 2022 10:32:20 -0800 Subject: [PATCH 1/3] add some type checking for pygments API usage - add a module level docstr to describe the purpose of filetypes.py - add types-Pygments to dev reqs - adjust imports in filetypes.py to better suite the types-Pygments stubs. --- breathe/filetypes.py | 15 ++++++++++----- requirements/development.txt | 1 + 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/breathe/filetypes.py b/breathe/filetypes.py index f16776f39..4372d3639 100644 --- a/breathe/filetypes.py +++ b/breathe/filetypes.py @@ -1,15 +1,20 @@ +""" +A module to house the methods for resolving a code-blocks language based on filename +(and extension). +""" from typing import Optional import os.path -import pygments # type: ignore +from pygments.lexers import get_lexer_for_filename +from pygments.util import ClassNotFound def get_pygments_alias(filename: str) -> Optional[str]: "Find first pygments alias from filename" try: - lexer_cls = pygments.lexers.get_lexer_for_filename(filename) - return lexer_cls.aliases[0] - except pygments.util.ClassNotFound: + lexer_cls = get_lexer_for_filename(filename) + return lexer_cls.aliases[0] # type: ignore + except ClassNotFound: return None @@ -20,5 +25,5 @@ def get_extension(filename: str) -> str: (first, second) = os.path.splitext(filename) # Doxygen allows users to specify the file extension ".unparsed" to disable syntax highlighting. - # We translate it into the pygments unhighlighted 'text' type + # We translate it into the pygments un-highlighted 'text' type return (second or first).lstrip(".").replace("unparsed", "text") diff --git a/requirements/development.txt b/requirements/development.txt index c1b749921..c64ba2b0a 100644 --- a/requirements/development.txt +++ b/requirements/development.txt @@ -6,5 +6,6 @@ pytest mypy>=0.900 types-docutils>=0.17.0 +types-Pygments black==22.1.0 From fddb65285ba14d289def52513d9a391791a2e1d1 Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Sat, 26 Feb 2022 10:44:55 -0800 Subject: [PATCH 2/3] let mypy infer a Sequence type `create_doxygen_target()` returns a Sequence not a List --- breathe/renderer/sphinxrenderer.py | 4 ++-- breathe/renderer/target.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/breathe/renderer/sphinxrenderer.py b/breathe/renderer/sphinxrenderer.py index 9c15cf72b..9b891ce3d 100644 --- a/breathe/renderer/sphinxrenderer.py +++ b/breathe/renderer/sphinxrenderer.py @@ -15,7 +15,7 @@ from sphinx.ext.graphviz import graphviz from docutils import nodes -from docutils.nodes import Element, Node, TextElement +from docutils.nodes import Node, TextElement from docutils.statemachine import StringList, UnexpectedIndentationError from docutils.parsers.rst.states import Text @@ -846,7 +846,7 @@ def run_domain_directive(self, kind, names): signode.children = [n for n in signode.children if not n.tagname == "desc_addname"] return nodes - def create_doxygen_target(self, node) -> List[Element]: + def create_doxygen_target(self, node): """Can be overridden to create a target node which uses the doxygen refid information which can be used for creating links between internal doxygen elements. diff --git a/breathe/renderer/target.py b/breathe/renderer/target.py index 57c810380..77691c0af 100644 --- a/breathe/renderer/target.py +++ b/breathe/renderer/target.py @@ -3,11 +3,11 @@ from docutils import nodes from docutils.nodes import Element -from typing import Any, Dict, List +from typing import Any, Dict, List, Sequence class TargetHandler: - def create_target(self, refid: str) -> List[Element]: + def create_target(self, refid: str) -> Sequence[Element]: raise NotImplementedError From 9cc01b1fe78655e3f9779b3afc256c54d669d63f Mon Sep 17 00:00:00 2001 From: 2bndy5 <2bndy5@gmail.com> Date: Tue, 1 Mar 2022 09:25:04 -0800 Subject: [PATCH 3/3] force docutils stubs to follow sphinx requirements --- requirements/development.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/development.txt b/requirements/development.txt index c64ba2b0a..0a9c69754 100644 --- a/requirements/development.txt +++ b/requirements/development.txt @@ -5,7 +5,7 @@ pip-tools>=0.3.5 pytest mypy>=0.900 -types-docutils>=0.17.0 +types-docutils>=0.14,<0.18 types-Pygments black==22.1.0