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

Use get_typing_hints instead of __annotations__ to resolve types in Python 3.10 #297

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions testslide/strict_mock.py
Expand Up @@ -7,7 +7,17 @@
import inspect
import os.path
from types import FrameType
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Type, Union
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
List,
Optional,
Type,
Union,
get_type_hints,
)

import testslide.lib
import testslide.mock_callable
Expand Down Expand Up @@ -623,8 +633,12 @@ def __validate_attribute_type(self, name: str, value: Any) -> None:
):
return

if hasattr(self._template, "__annotations__"):
annotations = self._template.__annotations__
if self._template is not None:
try:
annotations = get_type_hints(self._template)
except Exception:
# Some modules can throw KeyError : https://bugs.python.org/issue41515
annotations = {}
if name in annotations:
testslide.lib._validate_argument_type(annotations[name], name, value)

Expand Down