SF.net SVN: docutils:[9956] trunk/docutils/docutils

milde--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9956
          http://sourceforge.net/p/docutils/code/9956
Author:   milde
Date:     2024-10-20 19:45:21 +0000 (Sun, 20 Oct 2024)
Log Message:
-----------
Trim down overhead of type hints.

get_(reader|parser|writer)class():
  Don't use @overload for individual values of the same type that return
  a class that is a subclass of a well defined abstract base class.

docutils.nodes:
  There is no need to add a TypeVar for the type of a default value when
the alternative is any ``Any | _DefaultT``.

Cf. patches #212.

Modified Paths:
--------------
    trunk/docutils/docutils/nodes.py
    trunk/docutils/docutils/parsers/__init__.py
    trunk/docutils/docutils/readers/__init__.py
    trunk/docutils/docutils/writers/__init__.py

Modified: trunk/docutils/docutils/nodes.py
===================================================================
--- trunk/docutils/docutils/nodes.py	2024-10-20 18:57:49 UTC (rev 9955)
+++ trunk/docutils/docutils/nodes.py	2024-10-20 19:45:21 UTC (rev 9956)
@@ -41,8 +41,7 @@
     from collections.abc import (Callable, Iterable, Iterator,
                                  Mapping, Sequence)
     from types import ModuleType
-    from typing import (Any, ClassVar, Final, Literal, Self,
-                        SupportsIndex, TypeVar)
+    from typing import Any, ClassVar, Final, Literal, Self, SupportsIndex
     if sys.version_info[:2] >= (3, 12):
         from typing import TypeAlias
     else:
@@ -54,8 +53,6 @@
     from docutils.transforms import Transformer, Transform
     from docutils.utils import Reporter
 
-    _DefaultT = TypeVar('_DefaultT')
-
     _ContentModelCategory: TypeAlias = tuple['Element' | tuple['Element', ...]]
     _ContentModelQuantifier = Literal['.', '?', '+', '*']
     _ContentModelItem: TypeAlias = tuple[_ContentModelCategory,
@@ -791,14 +788,6 @@
     def attlist(self) -> list[tuple[str, Any]]:
         return sorted(self.non_default_attributes().items())
 
-    @overload
-    def get(self, key: str) -> Any:
-        ...
-
-    @overload
-    def get(self, key: str, failobj: _DefaultT) -> Any | _DefaultT:
-        ...
-
     def get(self, key: str, failobj: Any | None = None) -> Any:
         return self.attributes.get(key, failobj)
 
@@ -809,14 +798,6 @@
         if attr in self.attributes:
             del self.attributes[attr]
 
-    @overload
-    def setdefault(self, key: str) -> Any:
-        ...
-
-    @overload
-    def setdefault(self, key: str, failobj: _DefaultT) -> Any | _DefaultT:
-        ...
-
     def setdefault(self, key: str, failobj: Any | None = None) -> Any:
         return self.attributes.setdefault(key, failobj)
 

Modified: trunk/docutils/docutils/parsers/__init__.py
===================================================================
--- trunk/docutils/docutils/parsers/__init__.py	2024-10-20 18:57:49 UTC (rev 9955)
+++ trunk/docutils/docutils/parsers/__init__.py	2024-10-20 19:45:21 UTC (rev 9956)
@@ -11,26 +11,17 @@
 __docformat__ = 'reStructuredText'
 
 import importlib
-from typing import TYPE_CHECKING, overload
+from typing import TYPE_CHECKING
 
 from docutils import Component, frontend, transforms
 
 if TYPE_CHECKING:
-    from typing import Final, Literal
+    from typing import Final
 
     from docutils import nodes
-    from docutils.parsers import (
-        commonmark_wrapper,
-        docutils_xml,
-        null,
-        rst,
-        recommonmark_wrapper,
-    )
     from docutils.transforms import Transform
 
-    from myst_parser import docutils_ as myst_wrapper
 
-
 class Parser(Component):
     settings_spec = (
         'Generic Parser Options',
@@ -92,84 +83,33 @@
             self.document.note_parse_message)
 
 
-@overload
-def get_parser_class(parser_name: Literal['null']) -> type[null.Parser]:
-    ...
+PARSER_ALIASES = {  # short names for known parsers
+                  'null': 'docutils.parsers.null',
+                  # reStructuredText
+                  'rst': 'docutils.parsers.rst',
+                  'restructuredtext': 'docutils.parsers.rst',
+                  'rest': 'docutils.parsers.rst',
+                  'restx': 'docutils.parsers.rst',
+                  'rtxt': 'docutils.parsers.rst',
+                  # Docutils XML
+                  'docutils_xml': 'docutils.parsers.docutils_xml',
+                  'xml': 'docutils.parsers.docutils_xml',
+                  # 3rd-party Markdown parsers
+                  'recommonmark': 'docutils.parsers.recommonmark_wrapper',
+                  'myst': 'myst_parser.docutils_',
+                  # 'pycmark': works out of the box
+                  # dispatcher for 3rd-party Markdown parsers
+                  'commonmark': 'docutils.parsers.commonmark_wrapper',
+                  'markdown': 'docutils.parsers.commonmark_wrapper',
+                  }
 
 
-@overload
-def get_parser_class(
-    parser_name: Literal['rst', 'restructuredtext']
-) -> type[rst.Parser]:
-    ...
-
-
-@overload
-def get_parser_class(
-    parser_name: Literal['xml', 'docutils_xml']
-) -> type[docutils_xml.Parser]:
-    ...
-
-
-@overload
-def get_parser_class(
-    parser_name: Literal['recommonmark']
-) -> type[recommonmark_wrapper.Parser]:
-    ...
-
-
-@overload
-def get_parser_class(
-    parser_name: Literal['myst']
-) -> type[myst_wrapper.Parser]:
-    ...
-
-
-@overload
-def get_parser_class(
-    parser_name: Literal['commonmark', 'markdown']
-) -> type[commonmark_wrapper.Parser]:
-    ...
-
-
-@overload
 def get_parser_class(parser_name: str) -> type[Parser]:
-    ...
-
-
-def get_parser_class(parser_name: str) -> type[Parser]:
     """Return the Parser class from the `parser_name` module."""
     name = parser_name.lower()
 
-    # short names for known parsers
-    if name == 'null':
-        from docutils.parsers import null
-        return null.Parser
-    if name in {'rst', 'restructuredtext', 'rest', 'restx', 'rtxt'}:
-        from docutils.parsers import rst
-        return rst.Parser
-    if name in {'docutils_xml', 'xml'}:
-        from docutils.parsers import docutils_xml
-        return docutils_xml.Parser
-
     try:
-        # 3rd-party Markdown parsers
-        # (pycmark works out of the box)
-        if name == 'recommonmark':
-            from docutils.parsers import recommonmark_wrapper
-            return recommonmark_wrapper.Parser
-        if name == 'myst':
-            from myst_parser import docutils_ as myst_wrapper
-            return myst_wrapper.Parser
-
-        # dispatcher for 3rd-party Markdown parsers
-        if name in {'commonmark', 'markdown'}:
-            from docutils.parsers import commonmark_wrapper
-            return commonmark_wrapper.Parser
-
-        # fallback to importing a fully-qualified name
-        module = importlib.import_module(name)
+        module = importlib.import_module(PARSER_ALIASES.get(name, name))
     except ImportError as err:
-        raise ImportError(f'Parser "{parser_name}" not found. {err}')
-    else:
-        return module.Parser
+        raise ImportError(f'Parser "{parser_name}" not found. {err}') from err
+    return module.Parser

Modified: trunk/docutils/docutils/readers/__init__.py
===================================================================
--- trunk/docutils/docutils/readers/__init__.py	2024-10-20 18:57:49 UTC (rev 9955)
+++ trunk/docutils/docutils/readers/__init__.py	2024-10-20 19:45:21 UTC (rev 9956)
@@ -12,18 +12,17 @@
 
 import importlib
 import warnings
-from typing import TYPE_CHECKING, overload
+from typing import TYPE_CHECKING
 
 from docutils import utils, parsers, Component
 from docutils.transforms import universal
 
 if TYPE_CHECKING:
-    from typing import Final, Literal
+    from typing import Final
 
     from docutils import nodes
     from docutils.io import Input
     from docutils.parsers import Parser
-    from docutils.readers import doctree, pep, standalone
     from docutils.transforms import Transform
 
 
@@ -123,43 +122,14 @@
         return Component.get_transforms(self)
 
 
-@overload
-def get_reader_class(reader_name: Literal['standalone']
-                     ) -> type[standalone.Reader]:
-    ...
-
-
-@overload
-def get_reader_class(reader_name: Literal['doctree']) -> type[doctree.Reader]:
-    ...
-
-
-@overload
-def get_reader_class(reader_name: Literal['pep']) -> type[pep.Reader]:
-    ...
-
-
-@overload
 def get_reader_class(reader_name: str) -> type[Reader]:
-    ...
-
-
-def get_reader_class(reader_name: str) -> type[Reader]:
     """Return the Reader class from the `reader_name` module."""
     name = reader_name.lower()
-    if name == 'standalone':
-        from docutils.readers import standalone
-        return standalone.Reader
-    if name == 'doctree':
-        from docutils.readers import doctree
-        return doctree.Reader
-    if name == 'pep':
-        from docutils.readers import pep
-        return pep.Reader
-
     try:
-        module = importlib.import_module(name)
-    except ImportError as err:
-        raise ImportError(f'Reader "{reader_name}" not found.') from err
-    else:
-        return module.Reader
+        module = importlib.import_module('docutils.readers.'+name)
+    except ImportError:
+        try:
+            module = importlib.import_module(name)
+        except ImportError as err:
+            raise ImportError(f'Reader "{reader_name}" not found.') from err
+    return module.Reader

Modified: trunk/docutils/docutils/writers/__init__.py
===================================================================
--- trunk/docutils/docutils/writers/__init__.py	2024-10-20 18:57:49 UTC (rev 9955)
+++ trunk/docutils/docutils/writers/__init__.py	2024-10-20 19:45:21 UTC (rev 9956)
@@ -11,7 +11,7 @@
 __docformat__ = 'reStructuredText'
 
 import importlib
-from typing import TYPE_CHECKING, overload
+from typing import TYPE_CHECKING
 
 import docutils
 from docutils import languages, Component
@@ -18,25 +18,12 @@
 from docutils.transforms import universal
 
 if TYPE_CHECKING:
-    from typing import Any, Final, Literal
+    from typing import Any, Final
 
     from docutils import nodes
     from docutils.io import Output
     from docutils.languages import LanguageModule
     from docutils.transforms import Transform
-    from docutils.writers import (
-        docutils_xml,
-        html4css1,
-        html5_polyglot,
-        latex2e,
-        manpage,
-        null,
-        odf_odt,
-        pep_html,
-        pseudoxml,
-        s5_html,
-        xetex,
-    )
 
 
 class Writer(Component):
@@ -150,122 +137,37 @@
         return Component.get_transforms(self)
 
 
-@overload
-def get_writer_class(writer_name: Literal['null']) -> type[null.Writer]:
-    ...
+WRITER_ALIASES = {'html': 'html4css1',  # may change to html5 some day
+                  'html4': 'html4css1',
+                  'xhtml10': 'html4css1',
+                  'html5': 'html5_polyglot',
+                  'xhtml': 'html5_polyglot',
+                  's5': 's5_html',
+                  'latex': 'latex2e',
+                  'xelatex': 'xetex',
+                  'luatex': 'xetex',
+                  'lualatex': 'xetex',
+                  'odf': 'odf_odt',
+                  'odt': 'odf_odt',
+                  'ooffice': 'odf_odt',
+                  'openoffice': 'odf_odt',
+                  'libreoffice': 'odf_odt',
+                  'pprint': 'pseudoxml',
+                  'pformat': 'pseudoxml',
+                  'pdf': 'rlpdf',
+                  'xml': 'docutils_xml',
+                  }
 
 
-@overload
-def get_writer_class(
-    writer_name: Literal['html', 'html4']
-) -> type[html4css1.Writer]:
-    ...
-
-
-@overload
-def get_writer_class(
-    writer_name: Literal['html5']
-) -> type[html5_polyglot.Writer]:
-    ...
-
-
-@overload
-def get_writer_class(
-    writer_name: Literal['pep_html']
-) -> type[pep_html.Writer]:
-    ...
-
-
-@overload
-def get_writer_class(writer_name: Literal['s5']) -> type[s5_html.Writer]:
-    ...
-
-
-@overload
-def get_writer_class(writer_name: Literal['latex']) -> type[latex2e.Writer]:
-    ...
-
-
-@overload
-def get_writer_class(
-    writer_name: Literal['xetex', 'xelatex', 'luatex', 'lualatex']
-) -> type[xetex.Writer]:
-    ...
-
-
-@overload
-def get_writer_class(
-    writer_name: Literal['odf', 'odt', 'openoffice', 'libreoffice']
-) -> type[odf_odt.Writer]:
-    ...
-
-
-@overload
-def get_writer_class(
-    writer_name: Literal['manpage']
-) -> type[manpage.Writer]:
-    ...
-
-
-@overload
-def get_writer_class(
-    writer_name: Literal['pseudoxml', 'pprint', 'pformat']
-) -> type[pseudoxml.Writer]:
-    ...
-
-
-@overload
-def get_writer_class(writer_name: Literal['xml']) -> type[docutils_xml.Writer]:
-    ...
-
-
-@overload
 def get_writer_class(writer_name: str) -> type[Writer]:
-    ...
-
-
-def get_writer_class(writer_name: str) -> type[Writer]:
     """Return the Writer class from the `writer_name` module."""
     name = writer_name.lower()
-    if name == 'null':
-        from docutils.writers import null
-        return null.Writer
-    # The 'html' alias may change to html5 some day
-    if name in {'html', 'html4', 'html4css1', 'xhtml10'}:
-        from docutils.writers import html4css1
-        return html4css1.Writer
-    if name in {'html5', 'html5_polyglot', 'xhtml'}:
-        from docutils.writers import html5_polyglot
-        return html5_polyglot.Writer
-    if name == 'pep_html':
-        from docutils.writers import pep_html
-        return pep_html.Writer
-    if name in {'s5', 's5_html'}:
-        from docutils.writers import s5_html
-        return s5_html.Writer
-    if name in {'latex', 'latex2e'}:
-        from docutils.writers import latex2e
-        return latex2e.Writer
-    if name in {'xetex', 'xelatex', 'luatex', 'lualatex'}:
-        from docutils.writers import xetex
-        return xetex.Writer
-    if name in {'odf', 'odt', 'odf_odt', 'openoffice', 'libreoffice',
-                'ooffice'}:
-        from docutils.writers import odf_odt
-        return odf_odt.Writer
-    if name == 'manpage':
-        from docutils.writers import manpage
-        return manpage.Writer
-    if name in {'pseudoxml', 'pprint', 'pformat'}:
-        from docutils.writers import pseudoxml
-        return pseudoxml.Writer
-    if name in {'xml', 'docutils_xml'}:
-        from docutils.writers import docutils_xml
-        return docutils_xml.Writer
-
+    name = WRITER_ALIASES.get(name, name)
     try:
-        module = importlib.import_module(name)
-    except ImportError as err:
-        raise ImportError(f'Writer "{writer_name}" not found. {err}')
-    else:
-        return module.Writer
+        module = importlib.import_module('docutils.writers.'+name)
+    except ImportError:
+        try:
+            module = importlib.import_module(name)
+        except ImportError as err:
+            raise ImportError(f'Writer "{writer_name}" not found. {err}')
+    return module.Writer

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.