Skip to content

Commit 40f7b7e

Browse files
authored
Merge branch 'master' into pre-commit-ci-update-config
2 parents c9d4468 + ac7ac34 commit 40f7b7e

4 files changed

Lines changed: 16 additions & 28 deletions

File tree

docs/features.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ Protocol checking
6262
+++++++++++++++++
6363

6464
As of version 4.3.0, Typeguard can check instances and classes against Protocols,
65-
regardless of whether they were annotated with :decorator:`typing.runtime_checkable`.
65+
regardless of whether they were annotated with
66+
:func:`@runtime_checkable <typing.runtime_checkable>`.
6667

6768
There are several limitations on the checks performed, however:
6869

src/typeguard/_checkers.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@
3434
from unittest.mock import Mock
3535
from weakref import WeakKeyDictionary
3636

37-
try:
38-
import typing_extensions
39-
except ImportError:
40-
typing_extensions = None # type: ignore[assignment]
37+
import typing_extensions
4138

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

550547

551-
if typing_extensions is None:
552-
553-
def _is_literal_type(typ: object) -> bool:
554-
return typ is typing.Literal
555-
556-
else:
557-
558-
def _is_literal_type(typ: object) -> bool:
559-
return typ is typing.Literal or typ is typing_extensions.Literal
548+
def _is_literal_type(typ: object) -> bool:
549+
return typ is typing.Literal or typ is typing_extensions.Literal
560550

561551

562552
def check_literal(
@@ -905,6 +895,13 @@ def check_type_internal(
905895
type: check_class,
906896
Type: check_class,
907897
Union: check_union,
898+
# On some versions of Python, these may simply be re-exports from "typing",
899+
# but exactly which Python versions is subject to change.
900+
# It's best to err on the safe side and just always specify these.
901+
typing_extensions.Literal: check_literal,
902+
typing_extensions.LiteralString: check_literal_string,
903+
typing_extensions.Self: check_self,
904+
typing_extensions.TypeGuard: check_typeguard,
908905
}
909906
if sys.version_info >= (3, 10):
910907
origin_type_checkers[types.UnionType] = check_uniontype
@@ -913,16 +910,6 @@ def check_type_internal(
913910
origin_type_checkers.update(
914911
{typing.LiteralString: check_literal_string, typing.Self: check_self}
915912
)
916-
if typing_extensions is not None:
917-
# On some Python versions, these may simply be re-exports from typing,
918-
# but exactly which Python versions is subject to change,
919-
# so it's best to err on the safe side
920-
# and update the dictionary on all Python versions
921-
# if typing_extensions is installed
922-
origin_type_checkers[typing_extensions.Literal] = check_literal
923-
origin_type_checkers[typing_extensions.LiteralString] = check_literal_string
924-
origin_type_checkers[typing_extensions.Self] = check_self
925-
origin_type_checkers[typing_extensions.TypeGuard] = check_typeguard
926913

927914

928915
def builtin_checker_lookup(

tests/test_importhook.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,6 @@ def test_debug_instrumentation(monkeypatch, capsys):
6464
monkeypatch.setattr("typeguard.config.debug_instrumentation", True)
6565
import_dummymodule()
6666
out, err = capsys.readouterr()
67-
assert f"Source code of '{dummy_module_path}' after instrumentation:" in err
67+
path_str = str(dummy_module_path)
68+
assert f"Source code of {path_str!r} after instrumentation:" in err
6869
assert "class DummyClass" in err

tests/test_typechecked.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,8 @@ def foo(x: int) -> None:
619619
)
620620
assert process.returncode == expected_return_code
621621
if process.returncode == 1:
622-
assert process.stderr.endswith(
623-
b'typeguard.TypeCheckError: argument "x" (str) is not an instance of '
624-
b"int\n"
622+
assert process.stderr.strip().endswith(
623+
b'typeguard.TypeCheckError: argument "x" (str) is not an instance of int'
625624
)
626625

627626

0 commit comments

Comments
 (0)