Skip to content

Commit a8c131b

Browse files
committed
Fix typing_extensions import handling for mypy
mypy can handle `if sys.version_info` checking better than `try ... except ImportError`. This is somewhat disappointing, but the rewrite of these import lines isn't that bad and aligns with the recommendations of the mypy docs.
1 parent 5a2f8ee commit a8c131b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

jsonschema/protocols.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66
# https://www.python.org/dev/peps/pep-0544/
77

88
from typing import Any, ClassVar, Iterator, Optional, Union
9+
import sys
910

10-
try:
11+
# doing these imports with `try ... except ImportError` doesn't pass mypy
12+
# checking because mypy sees `typing._SpecialForm` and
13+
# `typing_extensions._SpecialForm` as incompatible
14+
#
15+
# see:
16+
# https://mypy.readthedocs.io/en/stable/runtime_troubles.html#using-new-additions-to-the-typing-module
17+
# https://github.com/python/mypy/issues/4427
18+
if sys.version_info >= (3, 8):
1119
from typing import Protocol, runtime_checkable
12-
except ImportError:
20+
else:
1321
from typing_extensions import Protocol, runtime_checkable
1422

1523
from jsonschema._format import FormatChecker

0 commit comments

Comments
 (0)