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 3 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
16 changes: 13 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,8 @@ 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:
annotations = get_type_hints(self._template)
Copy link
Member

Choose a reason for hiding this comment

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

This does not play nice with pybind11 based modules

in get_type_hints
    base_globals = sys.modules[base.__module__].__dict__
KeyError: 'pybind11_builtins'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, this seems to have caused an issue in sphinx as well where they catch keyerror and have empty annotations. I don't have the right setup but it will be good to know what was the value of annotations using __annotations__ attribute in earlier versions.

sphinx-doc/sphinx#8084

Copy link
Member

Choose a reason for hiding this comment

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

I guess catching key error and returning empty annotations would work well as a workaround for us as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I will make the changes. On further search seems this is an open issue in CPython where there is a PR open to catch the KeyError and discard it for these type of cases : https://bugs.python.org/issue41515

if name in annotations:
testslide.lib._validate_argument_type(annotations[name], name, value)

Expand Down