Skip to content

Commit

Permalink
Fix typing_extensions import handling for mypy
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sirosen committed Dec 16, 2021
1 parent 69aaa70 commit 1516dde
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions jsonschema/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
# https://www.python.org/dev/peps/pep-0544/

from typing import Any, ClassVar, Iterator, Optional, Union
import sys

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

from jsonschema._format import FormatChecker
Expand Down

0 comments on commit 1516dde

Please sign in to comment.