Repository: executablebooks/MyST-Parser
Pull request #642 (2022-10-28 11:13:30)

🔧: Depend on typing_extensions only on Python <3.8


Patch:
diff --git a/myst_parser/_compat.py b/myst_parser/_compat.py
index d29cf4d..da6a8d7 100644
--- a/myst_parser/_compat.py
+++ b/myst_parser/_compat.py
@@ -1,8 +1,14 @@
 """Helpers for cross compatibility across dependency versions."""
+import sys
 from typing import Callable, Iterable
 
 from docutils.nodes import Element
 
+if sys.version_info >= (3, 8):
+    from typing import Literal, Protocol, get_args, get_origin  # noqa: F401
+else:
+    from typing_extensions import Literal, Protocol, get_args, get_origin  # noqa: F401
+
 
 def findall(node: Element) -> Callable[..., Iterable[Element]]:
     """Iterate through"""
diff --git a/myst_parser/_docs.py b/myst_parser/_docs.py
index a7c46a3..6644bb3 100644
--- a/myst_parser/_docs.py
+++ b/myst_parser/_docs.py
@@ -10,8 +10,8 @@ from docutils.parsers.rst import directives
 from sphinx.directives import other
 from sphinx.util import logging
 from sphinx.util.docutils import SphinxDirective
-from typing_extensions import get_args, get_origin
 
+from ._compat import get_args, get_origin
 from .config.main import MdParserConfig
 from .parsers.docutils_ import Parser as DocutilsParser
 
diff --git a/myst_parser/config/dc_validators.py b/myst_parser/config/dc_validators.py
index 765cfb9..09c9430 100644
--- a/myst_parser/config/dc_validators.py
+++ b/myst_parser/config/dc_validators.py
@@ -4,7 +4,7 @@ from __future__ import annotations
 import dataclasses as dc
 from typing import Any, Sequence
 
-from typing_extensions import Protocol
+from myst_parser._compat import Protocol
 
 
 def validate_field(inst: Any, field: dc.Field, value: Any) -> None:
diff --git a/myst_parser/parsers/docutils_.py b/myst_parser/parsers/docutils_.py
index aaef5e2..d0f99bb 100644
--- a/myst_parser/parsers/docutils_.py
+++ b/myst_parser/parsers/docutils_.py
@@ -5,8 +5,8 @@ from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tupl
 from docutils import frontend, nodes
 from docutils.core import default_description, publish_cmdline
 from docutils.parsers.rst import Parser as RstParser
-from typing_extensions import Literal, get_args, get_origin
 
+from myst_parser._compat import Literal, get_args, get_origin
 from myst_parser.config.main import (
     MdParserConfig,
     TopmatterReadError,
diff --git a/pyproject.toml b/pyproject.toml
index 880a4fd..5b4484f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -40,7 +40,7 @@ dependencies = [
     "mdit-py-plugins~=0.3.1",
     "pyyaml",
     "sphinx>=4,<6",
-    "typing-extensions",
+    'typing-extensions; python_version < "3.8"',
 ]
 
 [project.urls]


Test patch:
diff --git a/tests/test_docutils.py b/tests/test_docutils.py
index f6f5d36..0d0fa6d 100644
--- a/tests/test_docutils.py
+++ b/tests/test_docutils.py
@@ -4,8 +4,8 @@ from textwrap import dedent
 
 import pytest
 from docutils import VersionInfo, __version_info__
-from typing_extensions import Literal
 
+from myst_parser._compat import Literal
 from myst_parser.mdit_to_docutils.base import make_document
 from myst_parser.parsers.docutils_ import (
     Parser,