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 4 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 KeyError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do except Exception just to make sure we dont break tests when we fail to get typehints for any funny modules?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KeyError seemed more specific to the modules with different __module__ . Are there any other failures that expose a bug with get_type_hints? I couldn't see the test failures since it's private.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# 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