Skip to content

Commit

Permalink
Enable check_untyped_defs mypy option and fix resultant issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jbms committed Jan 2, 2023
1 parent c4f1c40 commit 2837128
Show file tree
Hide file tree
Showing 28 changed files with 126 additions and 100 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ files = [
]
exclude = "setup.py"
plugins = ["pydantic.mypy"]
check_untyped_defs = true

[tool.black]
include = '\.py$'
Expand Down
4 changes: 2 additions & 2 deletions sphinx_immaterial/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def init_js_files(self):
if nav_adapt.READTHEDOCS is None:
excluded_scripts.add("_static/jquery.js")
excluded_scripts.add("_static/_sphinx_javascript_frameworks_compat.js")
self.script_files = [
self.script_files: List[sphinx.builders.html.JavaScript] = [
x for x in self.script_files if x.filename not in excluded_scripts
]

Expand All @@ -93,7 +93,7 @@ def init_css_files(self):
def gen_additional_pages(self):
# Prevent the search.html page from being written since this theme provides
# its own search results display that does not use it.
search = self.search
search = self.search # type: ignore[has-type]
self.search = False
super().gen_additional_pages()
self.search = search
Expand Down
40 changes: 20 additions & 20 deletions sphinx_immaterial/apidoc/cpp/api_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,36 +289,36 @@ class Config:
include_directory_map_pattern: ClassVar[Pattern]

def __post_init__(self):
self.allow_path_pattern = _combine_regexp_list(self.allow_paths)
self.disallow_path_pattern = _combine_regexp_list(self.disallow_paths)
self.allow_path_pattern = _combine_regexp_list(self.allow_paths)
self.allow_path_pattern = _combine_regexp_list(self.allow_paths) # type: ignore[misc]
self.disallow_path_pattern = _combine_regexp_list(self.disallow_paths) # type: ignore[misc]
self.allow_path_pattern = _combine_regexp_list(self.allow_paths) # type: ignore[misc]
self.disallow_namespaces_pattern = _combine_regexp_list(
self.disallow_namespaces
)
self.allow_symbols_pattern = _combine_regexp_list(self.allow_symbols)
self.disallow_symbols_pattern = _combine_regexp_list(self.disallow_symbols)
self.allow_macros_pattern = _combine_regexp_list(self.allow_macros)
self.disallow_macros_pattern = _combine_regexp_list(self.disallow_macros)
self.ignore_diagnostics_pattern = _combine_regexp_list(self.ignore_diagnostics)
self.hide_types_pattern = _combine_regexp_list(self.hide_types)
self.type_replacements_pattern = _make_replacement_pattern(
self.type_replacements, prefix=r"\b", suffix=r"\b"
self.allow_symbols_pattern = _combine_regexp_list(self.allow_symbols) # type: ignore[misc]
self.disallow_symbols_pattern = _combine_regexp_list(self.disallow_symbols) # type: ignore[misc]
self.allow_macros_pattern = _combine_regexp_list(self.allow_macros) # type: ignore[misc]
self.disallow_macros_pattern = _combine_regexp_list(self.disallow_macros) # type: ignore[misc]
self.ignore_diagnostics_pattern = _combine_regexp_list(self.ignore_diagnostics) # type: ignore[misc]
self.hide_types_pattern = _combine_regexp_list(self.hide_types) # type: ignore[misc]
self.type_replacements_pattern = _make_replacement_pattern( # type: ignore[misc]
list(self.type_replacements.keys()), prefix=r"\b", suffix=r"\b"
)
self.ignore_template_parameters_pattern = _combine_regexp_list(
self.ignore_template_parameters_pattern = _combine_regexp_list( # type: ignore[misc]
self.ignore_template_parameters
)
self.hide_initializers_pattern = _combine_regexp_list(self.hide_initializers)
self.hide_initializers_pattern = _combine_regexp_list(self.hide_initializers) # type: ignore[misc]
if os.name == "nt":
self._include_directory_map = {
self._include_directory_map = { # type: ignore[misc]
key.replace("\\", "/"): value
for key, value in self.include_directory_map.items()
}
else:
self._include_directory_map = self.include_directory_map
self.include_directory_map_pattern = _make_replacement_pattern(
self._include_directory_map = self.include_directory_map # type: ignore[misc]
self.include_directory_map_pattern = _make_replacement_pattern( # type: ignore[misc]
list(self._include_directory_map.keys()), prefix="^", suffix=""
)
self._cached_mapped_include_directories = {}
self._cached_mapped_include_directories = {} # type: ignore[misc]

_include_directory_map: ClassVar[Dict[str, str]]
_cached_mapped_include_directories: ClassVar[Dict[str, str]]
Expand Down Expand Up @@ -613,8 +613,8 @@ def get_spellings():
# angle brackets.
if prev_token is not None:
spelling = prev_token.spelling
token_end = prev_token.extent.end
offset_diff = token_end.offset - extent.end.offset
token_end = cast(SourceLocation, prev_token.extent.end)
offset_diff = token_end.offset - cast(SourceLocation, extent.end).offset
if offset_diff != 0:
yield spelling[:-offset_diff]
else:
Expand Down Expand Up @@ -1439,7 +1439,7 @@ def _transform_cursor_to_json(self, decl: Cursor, parent: Optional[Cursor]):
json_repr["parent"] = get_entity_id(parent)
entity_id = get_entity_id(decl)
if document_with:
prev_json = self._prev_decl[1]
prev_json = cast(Any, self._prev_decl)[1]
if (
prev_json is None
or not _kinds_are_compatible(prev_json["kind"], json_repr["kind"])
Expand Down
10 changes: 7 additions & 3 deletions sphinx_immaterial/apidoc/cpp/ast_fixes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""Fixes limitations of the Sphinx C++ parser."""

from . import fix_cpp_domain_requires_clause # type: ignore[unused-import]
import sphinx

from . import fix_cpp_domain_symbol_resolution_through_type_aliases # type: ignore[unused-import]
from . import fix_cpp_is_pack # type: ignore[unused-import]
from . import fix_cpp_symbol_to_normalize_template_args # type: ignore[unused-import]

if sphinx.version_info < (5, 2):
from . import fix_cpp_domain_requires_clause # type: ignore[unused-import]
from . import fix_cpp_is_pack # type: ignore[unused-import]
from . import fix_cpp_symbol_to_normalize_template_args # type: ignore[unused-import]
4 changes: 2 additions & 2 deletions sphinx_immaterial/apidoc/cpp/cpp_resolve_c_xrefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def _resolve_xref_inner(

return None, None

sphinx.domains.cpp.CPPDomain._resolve_xref_inner = _resolve_xref_inner
sphinx.domains.cpp.CPPDomain._resolve_xref_inner = _resolve_xref_inner # type: ignore[assignment]


def _monkey_patch_cpp_expr_role_to_include_c_parent_key():
Expand All @@ -112,7 +112,7 @@ def run(
refnode["c:parent_key"] = c_parent_key
return nodes, messages

CPPExprRole.run = run
CPPExprRole.run = run # type: ignore[assignment]


_monkey_patch_cpp_resolve_c_xrefs()
Expand Down
15 changes: 10 additions & 5 deletions sphinx_immaterial/apidoc/cpp/fix_cpp_domain_requires_clause.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
import re
from typing import Optional, Union

import sphinx
import sphinx.domains.cpp
from sphinx.domains.cpp import DefinitionParser, ASTDeclaration


# Not needed in Sphinx 5.2
assert sphinx.version_info < (5, 2)


def _monkey_patch_cpp_domain_support_requires_clause():
orig_parse_declaration = DefinitionParser.parse_declaration

Expand All @@ -33,9 +38,9 @@ def assert_end_or_requires(allowSemicolon=False):
return orig_parse_type(self, named, outer)
finally:
self.assert_end = orig_assert_end
return orig_parse_type(self, named, outer)
return orig_parse_type(self, named, outer) # type: ignore[arg-type]

DefinitionParser._parse_type = _parse_type
DefinitionParser._parse_type = _parse_type # type: ignore[assignment]

def parse_declaration(
self: DefinitionParser, objectType: str, directiveType: str
Expand All @@ -59,7 +64,7 @@ def parse_declaration(
requires_clause = None

def parse_template_declaration_prefix(*args, **kwargs):
self._parse_template_declaration_prefix = (
self._parse_template_declaration_prefix = ( # type: ignore[assignment]
orig_parse_template_declaration_prefix
)
result = orig_parse_template_declaration_prefix(*args, **kwargs)
Expand All @@ -72,14 +77,14 @@ def parse_template_declaration_prefix(*args, **kwargs):
self._parse_template_declaration_prefix = parse_template_declaration_prefix # type: ignore[assignment]

result = orig_parse_declaration(self, objectType, directiveType)
result.requiresClause = requires_clause
result.requiresClause = requires_clause # type: ignore[attr-defined]
return result
finally:
self._parse_template_declaration_prefix = ( # type: ignore[assignment]
orig_parse_template_declaration_prefix
)

DefinitionParser.parse_declaration = parse_declaration
DefinitionParser.parse_declaration = parse_declaration # type: ignore[assignment]


_monkey_patch_cpp_domain_support_requires_clause()
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _find_named_symbols(
self,
identOrOp,
None,
None,
None, # type: ignore[arg-type]
templateShorthand,
matchSelf,
recurseInAnon,
Expand Down Expand Up @@ -145,7 +145,7 @@ def _find_named_symbols(
searchInSiblings=False,
)

Symbol._find_named_symbols = _find_named_symbols
Symbol._find_named_symbols = _find_named_symbols # type: ignore[assignment]

in_symbol_lookup_with_shorthand = []

Expand Down Expand Up @@ -184,7 +184,7 @@ def _symbol_lookup(
finally:
in_symbol_lookup_with_shorthand.pop()

Symbol._symbol_lookup = _symbol_lookup
Symbol._symbol_lookup = _symbol_lookup # type: ignore[assignment]

orig_find_identifier = Symbol.find_identifier

Expand Down Expand Up @@ -220,7 +220,7 @@ def find_identifier(
return s
return None

Symbol.find_identifier = find_identifier
Symbol.find_identifier = find_identifier # type: ignore[assignment]


_monkey_patch_cpp_domain_symbol_resolution_through_type_aliases()
14 changes: 9 additions & 5 deletions sphinx_immaterial/apidoc/cpp/fix_cpp_is_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
https://github.com/sphinx-doc/sphinx/pull/10257
"""

import sphinx
import sphinx.domains.cpp

# Not needed in Sphinx 5.2
assert sphinx.version_info < (5, 2)


def _monkey_patch_cpp_is_pack():
sphinx.domains.cpp.ASTDeclaratorPtr.isPack = property(lambda self: self.next.isPack)
sphinx.domains.cpp.ASTDeclaratorRef.isPack = property(lambda self: self.next.isPack)
sphinx.domains.cpp.ASTDeclaratorMemPtr.isPack = property(
sphinx.domains.cpp.ASTDeclaratorPtr.isPack = property(lambda self: self.next.isPack) # type: ignore[assignment]
sphinx.domains.cpp.ASTDeclaratorRef.isPack = property(lambda self: self.next.isPack) # type: ignore[assignment]
sphinx.domains.cpp.ASTDeclaratorMemPtr.isPack = property( # type: ignore[assignment]
lambda self: self.next.isPack
)
sphinx.domains.cpp.ASTDeclaratorParamPack.isPack = property(lambda self: True)
sphinx.domains.cpp.ASTDeclaratorParen.isPack = property(
sphinx.domains.cpp.ASTDeclaratorParamPack.isPack = property(lambda self: True) # type: ignore[assignment]
sphinx.domains.cpp.ASTDeclaratorParen.isPack = property( # type: ignore[assignment]
lambda self: self.inner.isPack or self.next.isPack
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@

from typing import Union, Any

import sphinx
import sphinx.domains.cpp
from sphinx.domains.cpp import Symbol

# Not needed in Sphinx 5.2
assert sphinx.version_info < (5, 2)


def _is_cpp_ast_specialization(
templateParams: Union[
Expand Down Expand Up @@ -92,7 +96,7 @@ def __init__(
line,
)

Symbol.__init__ = __init__
Symbol.__init__ = __init__ # type: ignore[assignment]


_monkey_patch_cpp_symbol_to_normalize_template_args()
4 changes: 2 additions & 2 deletions sphinx_immaterial/apidoc/cpp/macro_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ def _add_function_params(self: CSymbol) -> None:
assert len(nn.names) == 1
self._add_symbols(nn, decl, self.docname, self.line)

CSymbol._add_function_params = _add_function_params
CSymbol._add_function_params = _add_function_params # type: ignore[assignment]

def get_id(
self: ASTMacroParameter, version: int, objectType: str, symbol: CSymbol
) -> str:
# the anchor will be our parent
return symbol.parent.declaration.get_id(version, prefixed=False)

ASTMacroParameter.get_id = get_id
ASTMacroParameter.get_id = get_id # type: ignore[attr-defined]

sphinx.domains.c.CDomain.object_types["macroParam"] = sphinx.domains.ObjType(
"macro parameter", "identifier", "var", "member", "data"
Expand Down
2 changes: 1 addition & 1 deletion sphinx_immaterial/apidoc/cpp/parameter_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def get_objects(
priority = OBJECT_PRIORITY_DEFAULT
yield (name, dispname, objectType, docname, anchor, priority)

sphinx.domains.cpp.CPPDomain.get_objects = get_objects
sphinx.domains.cpp.CPPDomain.get_objects = get_objects # type: ignore[assignment]


def _add_parameter_links_to_signature(
Expand Down
4 changes: 2 additions & 2 deletions sphinx_immaterial/apidoc/cpp/signodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sphinx.addnodes
import sphinx.application
import sphinx.domains.cpp
from sphinx.domains.cpp import ASTTemplateParams


class desc_cpp_template_param(sphinx.addnodes.desc_sig_element):
Expand Down Expand Up @@ -37,7 +38,6 @@ class desc_cpp_explicit(sphinx.addnodes.desc_sig_element):


def _monkey_patch_cpp_ast_template_params():
ASTTemplateParams = sphinx.domains.cpp.ASTTemplateParams
orig_describe_signature_as_introducer = (
ASTTemplateParams.describe_signature_as_introducer
)
Expand All @@ -60,7 +60,7 @@ def describe_signature_as_introducer(
x["classes"].append("sig-name-nonprimary")
parentNode.extend(fake_parent.children)

ASTTemplateParams.describe_signature_as_introducer = (
ASTTemplateParams.describe_signature_as_introducer = ( # type: ignore[assignment]
describe_signature_as_introducer
)

Expand Down
16 changes: 8 additions & 8 deletions sphinx_immaterial/apidoc/generic_synopses.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ def _monkey_patch_generic_object_to_support_synopses():

def transform_content(self: GenericObject, contentnode) -> None:
setattr(self, "contentnode", contentnode)
orig_transform_content(self, contentnode)
orig_transform_content(self, contentnode) # type: ignore[arg-type]

GenericObject.transform_content = transform_content
GenericObject.transform_content = transform_content # type: ignore[assignment]

def after_content(self: GenericObject) -> None:
orig_after_content(self)
orig_after_content(self) # type: ignore[arg-type]
noindex = "noindex" in self.options
if noindex:
return
Expand All @@ -48,15 +48,15 @@ def after_content(self: GenericObject) -> None:
for name in self.names:
std.data["synopses"][self.objtype, name] = synopsis

GenericObject.after_content = after_content
GenericObject.after_content = after_content # type: ignore[assignment]

orig_merge_domaindata = StandardDomain.merge_domaindata

def merge_domaindata(self, docnames: List[str], otherdata: dict) -> None:
orig_merge_domaindata(self, docnames, otherdata)
self.data["synopses"].update(otherdata["synopses"])

StandardDomain.merge_domaindata = merge_domaindata
StandardDomain.merge_domaindata = merge_domaindata # type: ignore[assignment]

def make_refnode(
std: StandardDomain,
Expand Down Expand Up @@ -108,7 +108,7 @@ def _resolve_obj_xref(
self, builder, fromdocname, docname, labelid, contnode, objtype, target
)

StandardDomain._resolve_obj_xref = _resolve_obj_xref
StandardDomain._resolve_obj_xref = _resolve_obj_xref # type: ignore[assignment]

def resolve_any_xref(
self: StandardDomain,
Expand Down Expand Up @@ -157,7 +157,7 @@ def resolve_any_xref(
)
return results

StandardDomain.resolve_any_xref = resolve_any_xref
StandardDomain.resolve_any_xref = resolve_any_xref # type: ignore[assignment]

def get_object_synopses(
self: StandardDomain,
Expand All @@ -169,7 +169,7 @@ def get_object_synopses(
continue
yield ((docname, labelid), synopsis)

StandardDomain.get_object_synopses = get_object_synopses
StandardDomain.get_object_synopses = get_object_synopses # type: ignore[attr-defined]


def setup(app: sphinx.application.Sphinx):
Expand Down
Loading

0 comments on commit 2837128

Please sign in to comment.