Skip to content
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

Update flake8 and mypy stuff #781

Merged
merged 3 commits into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ black:

.PHONY: type-check
type-check:
mypy breathe tests
mypy --warn-redundant-casts --warn-unused-ignores breathe tests
3 changes: 1 addition & 2 deletions breathe/directives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from sphinx.directives import SphinxDirective

from docutils import nodes
from docutils.nodes import Node

from typing import Any, Dict, List, Optional, Sequence

Expand Down Expand Up @@ -95,7 +94,7 @@ def render(
target_handler: TargetHandler,
mask_factory: MaskFactoryBase,
directive_args,
) -> List[Node]:
) -> List[nodes.Node]:
"Standard render process used by subclasses"

try:
Expand Down
4 changes: 2 additions & 2 deletions breathe/directives/class_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from docutils.nodes import Node
from docutils.parsers.rst.directives import unchanged_required, unchanged, flag # type: ignore

from typing import Any, List, Optional, Type # noqa
from typing import Any, List


class _DoxygenClassLikeDirective(BaseDirective):
Expand Down Expand Up @@ -47,7 +47,7 @@ def run(self) -> List[Node]:
finder_filter = self.filter_factory.create_compound_finder_filter(name, self.kind)

# TODO: find a more specific type for the Doxygen nodes
matches = [] # type: List[Any]
matches: List[Any] = []
finder.filter_(finder_filter, matches)

if len(matches) == 0:
Expand Down
14 changes: 6 additions & 8 deletions breathe/directives/content_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from docutils.nodes import Node
from docutils.parsers.rst.directives import unchanged_required, flag # type: ignore

from typing import Any, List, Optional, Type # noqa
from typing import Any, List


class _DoxygenContentBlockDirective(BaseDirective):
Expand Down Expand Up @@ -48,7 +48,7 @@ def run(self) -> List[Node]:
finder_filter = self.filter_factory.create_finder_filter(self.kind, name)

# TODO: find a more specific type for the Doxygen nodes
matches = [] # type: List[Any]
matches: List[Any] = []
finder.filter_(finder_filter, matches)

# It shouldn't be possible to have too many matches as namespaces & groups in their nature
Expand All @@ -68,7 +68,7 @@ def run(self) -> List[Node]:
node_stack[0], project_info
)
# TODO: find a more specific type for the Doxygen nodes
contents = [] # type: List[Any]
contents: List[Any] = []
contents_finder.filter_(filter_, contents)

# Replaces matches with our new starting points
Expand All @@ -77,7 +77,7 @@ def run(self) -> List[Node]:
target_handler = create_target_handler(self.options, project_info, self.state.document)
filter_ = self.filter_factory.create_render_filter(self.kind, self.options)

node_list = []
node_list: List[Node] = []
for node_stack in matches:
object_renderer = SphinxRenderer(
self.parser_factory.app,
Expand All @@ -103,10 +103,8 @@ class DoxygenNamespaceDirective(_DoxygenContentBlockDirective):

class DoxygenGroupDirective(_DoxygenContentBlockDirective):
kind = "group"
option_spec = {
**_DoxygenContentBlockDirective.option_spec,
"inner": flag,
}
option_spec = _DoxygenContentBlockDirective.option_spec.copy()
option_spec.update({"inner": flag})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of interest, why do you prefer this formulation? Does it have an objective benefit or just preferred style?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is just a style choice, to be consistent with the equivalent cases I have seen in Sphinx to more quickly spot what is going on.



class DoxygenPageDirective(_DoxygenContentBlockDirective):
Expand Down
24 changes: 12 additions & 12 deletions breathe/directives/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import re

from typing import Any, List, Optional, Type # noqa
from typing import Any, List, Optional


class _NoMatchingFunctionError(BreatheError):
Expand Down Expand Up @@ -62,7 +62,7 @@ def run(self) -> List[Node]:
assert match is not None # TODO: this is probably not appropriate, for now it fixes typing
namespace = (match.group(1) or "").strip()
function_name = match.group(2).strip()
args = match.group(3)
argsStr = match.group(3)

try:
project_info = self.project_info_factory.create_project_info(self.options)
Expand All @@ -78,13 +78,13 @@ def run(self) -> List[Node]:

# Extract arguments from the function name.
try:
args = self._parse_args(args)
args = self._parse_args(argsStr)
except cpp.DefinitionError as e:
return self.create_warning(
project_info,
namespace="%s::" % namespace if namespace else "",
function=function_name,
args=str(args),
args=argsStr,
cpperror=str(e),
).warn(
"doxygenfunction: Unable to resolve function "
Expand All @@ -97,7 +97,7 @@ def run(self) -> List[Node]:
)

# TODO: find a more specific type for the Doxygen nodes
matchesAll = [] # type: List[Any]
matchesAll: List[Any] = []
finder.filter_(finder_filter, matchesAll)
matches = []
for m in matchesAll:
Expand Down Expand Up @@ -170,17 +170,17 @@ def _parse_args(self, function_description: str) -> Optional[cpp.ASTParametersQu
# strip everything that doesn't contribute to overloading

def stripParamQual(paramQual):
paramQual.exceptionSpec = None # type: ignore
paramQual.final = None # type: ignore
paramQual.override = None # type: ignore
paramQual.exceptionSpec = None
paramQual.final = None
paramQual.override = None
# TODO: strip attrs when Doxygen handles them
paramQual.initializer = None # type: ignore
paramQual.trailingReturn = None # type: ignore
paramQual.initializer = None
paramQual.trailingReturn = None
for p in paramQual.args:
if p.arg is None:
assert p.ellipsis
continue
p.arg.init = None # type: ignore
p.arg.init = None
declarator = p.arg.type.decl

def stripDeclarator(declarator):
Expand All @@ -192,7 +192,7 @@ def stripDeclarator(declarator):
else:
assert isinstance(declarator, cpp.ASTDeclaratorNameParamQual)
assert hasattr(declarator, "declId")
declarator.declId = None # type: ignore
declarator.declId = None
if declarator.paramQual is not None:
stripParamQual(declarator.paramQual)

Expand Down
4 changes: 2 additions & 2 deletions breathe/directives/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from docutils.parsers.rst.directives import unchanged_required, flag # type: ignore

from typing import Any, List, Optional, Type # noqa
from typing import Any, List


class _DoxygenBaseItemDirective(BaseDirective):
Expand Down Expand Up @@ -49,7 +49,7 @@ def run(self) -> List[Node]:
finder_filter = self.create_finder_filter(namespace, name)

# TODO: find a more specific type for the Doxygen nodes
matches = [] # type: List[Any]
matches: List[Any] = []
finder.filter_(finder_filter, matches)

if len(matches) == 0:
Expand Down
4 changes: 1 addition & 3 deletions breathe/directives/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import os
import subprocess

from typing import Any, List, Optional, Type # noqa


def setup(app: Sphinx) -> None:
directives = {
Expand Down Expand Up @@ -96,7 +94,7 @@ def set_temp_data(
app.add_config_value("breathe_separate_member_pages", False, "env")

breathe_css = "breathe.css"
if os.path.exists(os.path.join(app.confdir, "_static", breathe_css)): # type: ignore
if os.path.exists(os.path.join(app.confdir, "_static", breathe_css)):
app.add_css_file(breathe_css)

def write_file(directory, filename, content):
Expand Down
4 changes: 2 additions & 2 deletions breathe/finder/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def create_finder(self, project_info: ProjectInfo) -> Finder:
return self.create_finder_from_root(root, project_info)

def create_finder_from_root(self, root, project_info: ProjectInfo) -> Finder:
finders = {
finders: Dict[str, Type[ItemFinder]] = {
"doxygen": indexfinder.DoxygenTypeSubItemFinder,
"compound": _CreateCompoundTypeSubFinder(self.app, self.parser_factory), # type: ignore
"member": indexfinder.MemberTypeSubItemFinder,
Expand All @@ -68,6 +68,6 @@ def create_finder_from_root(self, root, project_info: ProjectInfo) -> Finder:
"sectiondef": compoundfinder.SectionDefTypeSubItemFinder,
"memberdef": compoundfinder.MemberDefTypeSubItemFinder,
"ref": compoundfinder.RefTypeSubItemFinder,
} # type: Dict[str, Type[ItemFinder]]
}
item_finder_factory = DoxygenItemFinderFactory(finders, project_info)
return Finder(root, item_finder_factory)
4 changes: 2 additions & 2 deletions breathe/finder/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from sphinx.application import Sphinx

from typing import Any, List # noqa
from typing import Any, List


class DoxygenTypeSubItemFinder(ItemFinder):
Expand Down Expand Up @@ -42,7 +42,7 @@ def filter_(self, ancestors, filter_: Filter, matches) -> None:
# Descend to member children
members = self.data_object.get_member()
# TODO: find a more precise type for the Doxygen nodes
member_matches = [] # type: List[Any]
member_matches: List[Any] = []
for member in members:
member_finder = self.item_finder_factory.create_finder(member)
member_finder.filter_(node_stack, filter_, member_matches)
Expand Down
2 changes: 1 addition & 1 deletion breathe/path_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ def resolve_path(app: Sphinx, directory: str, filename: str):
"""

# os.path.join does the appropriate handling if _project_path is an absolute path
return os.path.join(app.confdir, directory, filename) # type: ignore
return os.path.join(app.confdir, directory, filename)
23 changes: 11 additions & 12 deletions breathe/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import os
import fnmatch

if False:
# For type annotation
from typing import Dict, Optional # noqa

from typing import Dict


class ProjectError(BreatheError):
Expand Down Expand Up @@ -94,11 +93,11 @@ def reference(self):
def domain_for_file(self, file_: str) -> str:
extension = file_.split(".")[-1]
try:
domain = self.app.config.breathe_domain_by_extension[extension] # type: ignore
domain = self.app.config.breathe_domain_by_extension[extension]
except KeyError:
domain = ""

domainFromFilePattern = self.app.config.breathe_domain_by_file_pattern # type: ignore
domainFromFilePattern = self.app.config.breathe_domain_by_file_pattern
for pattern, pattern_domain in domainFromFilePattern.items():
if fnmatch.fnmatch(file_, pattern):
domain = pattern_domain
Expand All @@ -116,20 +115,20 @@ def __init__(self, app: Sphinx):
# This can be overridden with the breathe_build_directory config variable
self._default_build_dir = os.path.dirname(app.doctreedir.rstrip(os.sep))
self.project_count = 0
self.project_info_store = {} # type: Dict[str, ProjectInfo]
self.project_info_for_auto_store = {} # type: Dict[str, AutoProjectInfo]
self.auto_project_info_store = {} # type: Dict[str, AutoProjectInfo]
self.project_info_store: Dict[str, ProjectInfo] = {}
self.project_info_for_auto_store: Dict[str, AutoProjectInfo] = {}
self.auto_project_info_store: Dict[str, AutoProjectInfo] = {}

@property
def build_dir(self) -> str:
config = self.app.config # type: ignore
config = self.app.config
if config.breathe_build_directory:
return config.breathe_build_directory
else:
return self._default_build_dir

def default_path(self) -> str:
config = self.app.config # type: ignore
config = self.app.config
if not config.breathe_default_project:
raise NoDefaultProjectError(
"No breathe_default_project config setting to fall back on "
Expand All @@ -148,7 +147,7 @@ def default_path(self) -> str:
)

def create_project_info(self, options) -> ProjectInfo:
config = self.app.config # type: ignore
config = self.app.config
name = config.breathe_default_project

if "project" in options:
Expand Down Expand Up @@ -194,7 +193,7 @@ def retrieve_project_info_for_auto(self, options) -> AutoProjectInfo:
sense.
"""

name = options.get("project", self.app.config.breathe_default_project) # type: ignore
name = options.get("project", self.app.config.breathe_default_project)
if name is None:
raise NoDefaultProjectError(
"No breathe_default_project config setting to fall back on "
Expand Down
12 changes: 4 additions & 8 deletions breathe/renderer/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,7 @@ def create_render_filter(self, kind: str, options: Dict[str, Any]) -> Filter:
raise UnrecognisedKindError(kind)

# Generate new dictionary from defaults
filter_options = dict(
(entry, "") for entry in self.app.config.breathe_default_members
) # type: ignore
filter_options = dict((entry, "") for entry in self.app.config.breathe_default_members)

# Update from the actual options
filter_options.update(options)
Expand Down Expand Up @@ -621,9 +619,7 @@ def create_class_filter(self, target: str, options: Dict[str, Any]) -> Filter:
"""Content filter for classes based on various directive options"""

# Generate new dictionary from defaults
filter_options = dict(
(entry, "") for entry in self.app.config.breathe_default_members
) # type: ignore
filter_options = dict((entry, "") for entry in self.app.config.breathe_default_members)

# Update from the actual options
filter_options.update(options)
Expand Down Expand Up @@ -842,7 +838,7 @@ def create_outline_filter(self, options: Dict[str, Any]) -> Filter:
return OpenFilter()

def create_file_filter(self, filename: str, options: Dict[str, Any]) -> Filter:
valid_names = [] # type: List[str]
valid_names: List[str] = []

filter_ = AndFilter(
NotFilter(
Expand Down Expand Up @@ -1021,7 +1017,7 @@ def create_member_finder_filter(self, namespace: str, name: str, kind: str) -> F
else:
is_implementation_file = parent.name.endswith(
self.app.config.breathe_implementation_filename_extensions
) # type: ignore
)
parent_is_compound = parent.node_type == "compound"
parent_is_file = (parent.kind == "file") & (~is_implementation_file)
parent_is_not_file = parent.kind != "file"
Expand Down
Loading