Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assume that typing_extensions is always installed. #487

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 10 additions & 23 deletions src/typeguard/_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@
from unittest.mock import Mock
from weakref import WeakKeyDictionary

try:
import typing_extensions
except ImportError:
typing_extensions = None # type: ignore[assignment]
import typing_extensions

# Must use this because typing.is_typeddict does not recognize
# TypedDict from typing_extensions, and as of version 4.12.0
Expand Down Expand Up @@ -548,15 +545,8 @@ def check_typevar(
)


if typing_extensions is None:

def _is_literal_type(typ: object) -> bool:
return typ is typing.Literal

else:

def _is_literal_type(typ: object) -> bool:
return typ is typing.Literal or typ is typing_extensions.Literal
def _is_literal_type(typ: object) -> bool:
return typ is typing.Literal or typ is typing_extensions.Literal


def check_literal(
Expand Down Expand Up @@ -905,6 +895,13 @@ def check_type_internal(
type: check_class,
Type: check_class,
Union: check_union,
# On some versions of Python, these may simply be re-exports from "typing",
# but exactly which Python versions is subject to change.
# It's best to err on the safe side and just always specify these.
typing_extensions.Literal: check_literal,
typing_extensions.LiteralString: check_literal_string,
typing_extensions.Self: check_self,
typing_extensions.TypeGuard: check_typeguard,
}
if sys.version_info >= (3, 10):
origin_type_checkers[types.UnionType] = check_uniontype
Expand All @@ -913,16 +910,6 @@ def check_type_internal(
origin_type_checkers.update(
{typing.LiteralString: check_literal_string, typing.Self: check_self}
)
if typing_extensions is not None:
# On some Python versions, these may simply be re-exports from typing,
# but exactly which Python versions is subject to change,
# so it's best to err on the safe side
# and update the dictionary on all Python versions
# if typing_extensions is installed
origin_type_checkers[typing_extensions.Literal] = check_literal
origin_type_checkers[typing_extensions.LiteralString] = check_literal_string
origin_type_checkers[typing_extensions.Self] = check_self
origin_type_checkers[typing_extensions.TypeGuard] = check_typeguard


def builtin_checker_lookup(
Expand Down