summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2020-03-07 11:15:54 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2020-03-07 19:36:41 +0900
commit28e5e66f78f46e5e44b053297a0c90a718effcf7 (patch)
tree5290cb6113599dadf6a47f247683dfa06183151f
parent1fb79cff452144761a09fdd0feee28724f1b904b (diff)
downloadsphinx-git-28e5e66f78f46e5e44b053297a0c90a718effcf7.tar.gz
Use typing.NamedTuple instead of collections.namedtuple as possible
-rw-r--r--sphinx/builders/_epub_base.py33
-rw-r--r--sphinx/builders/epub3.py10
-rw-r--r--sphinx/config.py8
-rw-r--r--sphinx/domains/changeset.py13
-rw-r--r--sphinx/ext/autodoc/importer.py8
-rw-r--r--sphinx/testing/fixtures.py1
-rw-r--r--sphinx/util/i18n.py9
7 files changed, 57 insertions, 25 deletions
diff --git a/sphinx/builders/_epub_base.py b/sphinx/builders/_epub_base.py
index f86715fcf..0f440dc9b 100644
--- a/sphinx/builders/_epub_base.py
+++ b/sphinx/builders/_epub_base.py
@@ -12,9 +12,8 @@ import html
import os
import re
import warnings
-from collections import namedtuple
from os import path
-from typing import Any, Dict, List, Set, Tuple
+from typing import Any, Dict, List, NamedTuple, Set, Tuple
from zipfile import ZIP_DEFLATED, ZIP_STORED, ZipFile
from docutils import nodes
@@ -85,10 +84,30 @@ VECTOR_GRAPHICS_EXTENSIONS = ('.svg',)
REFURI_RE = re.compile("([^#:]*#)(.*)")
-ManifestItem = namedtuple('ManifestItem', ['href', 'id', 'media_type'])
-Spine = namedtuple('Spine', ['idref', 'linear'])
-Guide = namedtuple('Guide', ['type', 'title', 'uri'])
-NavPoint = namedtuple('NavPoint', ['navpoint', 'playorder', 'text', 'refuri', 'children'])
+class ManifestItem(NamedTuple):
+ href: str
+ id: str
+ media_type: str
+
+
+class Spine(NamedTuple):
+ idref: str
+ linear: bool
+
+
+class Guide(NamedTuple):
+ type: str
+ title: str
+ uri: str
+
+
+class NavPoint(NamedTuple):
+ navpoint: str
+ playorder: int
+ text: str
+ refuri: str
+ children: List[Any] # mypy does not support recursive types
+ # https://github.com/python/mypy/issues/7069
def sphinx_smarty_pants(t: str, language: str = 'en') -> str:
@@ -635,7 +654,7 @@ class EpubBuilder(StandaloneHTMLBuilder):
the parent node is reinserted in the subnav.
"""
navstack = [] # type: List[NavPoint]
- navstack.append(NavPoint('dummy', '', '', '', []))
+ navstack.append(NavPoint('dummy', 0, '', '', []))
level = 0
lastnode = None
for node in nodes:
diff --git a/sphinx/builders/epub3.py b/sphinx/builders/epub3.py
index 5d7121fe6..7caac74d3 100644
--- a/sphinx/builders/epub3.py
+++ b/sphinx/builders/epub3.py
@@ -11,9 +11,8 @@
import html
import warnings
-from collections import namedtuple
from os import path
-from typing import Any, Dict, List, Set, Tuple
+from typing import Any, Dict, List, NamedTuple, Set, Tuple
from sphinx import package_dir
from sphinx.application import Sphinx
@@ -29,7 +28,12 @@ from sphinx.util.osutil import make_filename
logger = logging.getLogger(__name__)
-NavPoint = namedtuple('NavPoint', ['text', 'refuri', 'children'])
+class NavPoint(NamedTuple):
+ text: str
+ refuri: str
+ children: List[Any] # mypy does not support recursive types
+ # https://github.com/python/mypy/issues/7069
+
# writing modes
PAGE_PROGRESSION_DIRECTIONS = {
diff --git a/sphinx/config.py b/sphinx/config.py
index 87007c33d..652682168 100644
--- a/sphinx/config.py
+++ b/sphinx/config.py
@@ -39,9 +39,11 @@ CONFIG_FILENAME = 'conf.py'
UNSERIALIZABLE_TYPES = (type, types.ModuleType, types.FunctionType)
copyright_year_re = re.compile(r'^((\d{4}-)?)(\d{4})(?=[ ,])')
-ConfigValue = NamedTuple('ConfigValue', [('name', str),
- ('value', Any),
- ('rebuild', Union[bool, str])])
+
+class ConfigValue(NamedTuple):
+ name: str
+ value: Any
+ rebuild: Union[bool, str]
def is_serializable(obj: Any) -> bool:
diff --git a/sphinx/domains/changeset.py b/sphinx/domains/changeset.py
index a07944db8..0f982f418 100644
--- a/sphinx/domains/changeset.py
+++ b/sphinx/domains/changeset.py
@@ -8,8 +8,7 @@
:license: BSD, see LICENSE for details.
"""
-from collections import namedtuple
-from typing import Any, Dict, List
+from typing import Any, Dict, List, NamedTuple
from typing import cast
from docutils import nodes
@@ -40,9 +39,13 @@ versionlabel_classes = {
}
-# TODO: move to typing.NamedTuple after dropping py35 support (see #5958)
-ChangeSet = namedtuple('ChangeSet',
- ['type', 'docname', 'lineno', 'module', 'descname', 'content'])
+class ChangeSet(NamedTuple):
+ type: str
+ docname: str
+ lineno: int
+ module: str
+ descname: str
+ content: str
class VersionChange(SphinxDirective):
diff --git a/sphinx/ext/autodoc/importer.py b/sphinx/ext/autodoc/importer.py
index e98b97915..a7d68d8fa 100644
--- a/sphinx/ext/autodoc/importer.py
+++ b/sphinx/ext/autodoc/importer.py
@@ -11,8 +11,7 @@
import importlib
import traceback
import warnings
-from collections import namedtuple
-from typing import Any, Callable, Dict, List, Mapping, Tuple
+from typing import Any, Callable, Dict, List, Mapping, NamedTuple, Tuple
from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias
from sphinx.util import logging
@@ -122,7 +121,10 @@ def get_module_members(module: Any) -> List[Tuple[str, Any]]:
return sorted(list(members.values()))
-Attribute = namedtuple('Attribute', ['name', 'directly_defined', 'value'])
+class Attribute(NamedTuple):
+ name: str
+ directly_defined: bool
+ value: Any
def get_object_members(subject: Any, objpath: List[str], attrgetter: Callable,
diff --git a/sphinx/testing/fixtures.py b/sphinx/testing/fixtures.py
index eec3b4208..a864e19d1 100644
--- a/sphinx/testing/fixtures.py
+++ b/sphinx/testing/fixtures.py
@@ -76,7 +76,6 @@ def app_params(request: Any, test_params: Dict, shared_result: SharedResult,
args = [pargs[i] for i in sorted(pargs.keys())]
# ##### process pytest.mark.test_params
-
if test_params['shared_result']:
if 'srcdir' in kwargs:
raise pytest.Exception('You can not spcify shared_result and '
diff --git a/sphinx/util/i18n.py b/sphinx/util/i18n.py
index 1cb75637c..c1da94429 100644
--- a/sphinx/util/i18n.py
+++ b/sphinx/util/i18n.py
@@ -11,10 +11,9 @@ import gettext
import os
import re
import warnings
-from collections import namedtuple
from datetime import datetime, timezone
from os import path
-from typing import Callable, Generator, List, Set, Tuple
+from typing import Callable, Generator, List, NamedTuple, Set, Tuple
import babel.dates
from babel.messages.mofile import write_mo
@@ -34,7 +33,11 @@ if False:
logger = logging.getLogger(__name__)
-LocaleFileInfoBase = namedtuple('CatalogInfo', 'base_dir,domain,charset')
+
+class LocaleFileInfoBase(NamedTuple):
+ base_dir: str
+ domain: str
+ charset: str
class CatalogInfo(LocaleFileInfoBase):