Skip to content
Merged
Show file tree
Hide file tree
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
27 changes: 25 additions & 2 deletions typemap/type_eval/_apply_generic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import annotationlib
import dataclasses
import inspect
import sys
import types
import typing

Expand Down Expand Up @@ -68,7 +69,9 @@ def dump(self, *, _level: int = 0):
def substitute(ty, args):
if ty in args:
return args[ty]
elif isinstance(ty, (typing_GenericAlias, types.GenericAlias)):
elif isinstance(
ty, (typing_GenericAlias, types.GenericAlias, types.UnionType)
):
return ty.__origin__[*[substitute(t, args) for t in ty.__args__]]
else:
return ty
Expand Down Expand Up @@ -241,7 +244,27 @@ def get_local_defns(boxed: Boxed) -> tuple[dict[str, Any], dict[str, Any]]:
annos[k] = v
elif af := getattr(boxed.cls, "__annotations__", None):
# TODO: substitute vars in this case
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We may wish to keep this TODO for the non str case?

annos.update(af)
_globals = {}
if mod := sys.modules.get(boxed.cls.__module__):
_globals.update(vars(mod))
_globals.update(boxed.str_args)

_locals = dict(boxed.cls.__dict__)
_locals.update(boxed.str_args)

for k, v in af.items():
if isinstance(v, str):
result = eval(v, _globals, _locals)
# Handle cases where annotation is explicitly a string
# e.g.
# class Foo[T]:
# x: "Bar[T]"
if isinstance(result, str):
result = eval(result, _globals, _locals)
annos[k] = result

else:
annos[k] = v

for name, orig in boxed.cls.__dict__.items():
if name in EXCLUDED_ATTRIBUTES:
Expand Down
3 changes: 2 additions & 1 deletion typemap/type_eval/_eval_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def _get_bound_type_args(
arg_types: tuple[RtType, ...],
kwarg_types: dict[str, RtType],
) -> dict[str, RtType]:
sig = inspect.signature(func)
sig = _eval_operators._resolved_function_signature(func)

bound = sig.bind(*arg_types, **kwarg_types)

return {
Expand Down
63 changes: 61 additions & 2 deletions typemap/type_eval/_eval_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

from typemap import type_eval
from typemap.type_eval import _apply_generic, _typing_inspect
from typemap.type_eval._eval_typing import _eval_types
from typemap.type_eval._eval_typing import (
_eval_types,
_get_class_type_hint_namespaces,
)
from typemap.typing import (
Attrs,
Capitalize,
Expand Down Expand Up @@ -458,7 +461,7 @@ def _callable_type_to_method(name, typ):

def _function_type(func, *, receiver_type):
root = inspect.unwrap(func)
sig = inspect.signature(root)
sig = _resolved_function_signature(root, receiver_type)
# XXX: __type_params__!!!

empty = inspect.Parameter.empty
Expand Down Expand Up @@ -513,6 +516,62 @@ def _ann(x):
return f


def _resolved_function_signature(func, receiver_type=None):
"""Get the signature of a function with type hints resolved.

This is used to deal with string annotations in the signature which are
generated when using __future__ import annotations.
"""

sig = inspect.signature(func)

_globals, _locals = _get_function_hint_namespaces(func, receiver_type)
if hints := typing.get_type_hints(
func, globalns=_globals, localns=_locals, include_extras=True
):
params = []
for name, param in sig.parameters.items():
annotation = hints.get(name, param.annotation)
params.append(param.replace(annotation=annotation))

return_annotation = hints.get("return", sig.return_annotation)
sig = sig.replace(
parameters=params, return_annotation=return_annotation
)

return sig


def _get_function_hint_namespaces(func, receiver_type=None):
globalns = {}
localns = {}

# module globals
module = inspect.getmodule(func)
if module:
globalns |= module.__dict__

# If no receiver was specified, this might still be a method, try to get
# the class from the qualname.
if (
not receiver_type
and (qn := getattr(func, '__qualname__', None))
and '.' in qn
):
class_name = qn.rsplit('.', 1)[0]
receiver_type = getattr(module, class_name, None)

# Get the class's type hint namespaces
if receiver_type:
cls_globalns, cls_localns = _get_class_type_hint_namespaces(
receiver_type
)
globalns.update(cls_globalns)
localns.update(cls_localns)

return globalns, localns


def _hints_to_members(hints, ctx):
"""Convert a hints dictionary to a tuple of Member types."""
return tuple[
Expand Down
53 changes: 52 additions & 1 deletion typemap/type_eval/_eval_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
if typing.TYPE_CHECKING:
from typing import Any

from . import _apply_generic
from . import _apply_generic, _typing_inspect


__all__ = ("eval_typing",)
Expand Down Expand Up @@ -272,8 +272,59 @@ def _eval_func(
return _apply_generic.make_func(func, annos)


def _get_class_type_hint_namespaces(
obj: type,
) -> tuple[dict[str, typing.Any], dict[str, typing.Any]]:
globalns: dict[str, typing.Any] = {}
localns: dict[str, typing.Any] = {}

# Get module globals
if obj.__module__ and (module := sys.modules.get(obj.__module__)):
globalns.update(module.__dict__)

# Annotations may use typevars defined in the class
localns.update(obj.__dict__)

if _typing_inspect.is_generic_alias(obj):
# We need the origin's type vars
localns.update(obj.__origin__.__dict__)

# Extract type parameters from the class
args = typing.get_args(obj)
origin = typing.get_origin(obj)
tps = getattr(obj, '__type_params__', ()) or getattr(
origin, '__parameters__', ()
)
for tp, arg in zip(tps, args, strict=False):
localns[tp.__name__] = arg

# Add the class itself for self-references
localns[obj.__name__] = obj

return globalns, localns


@_eval_types_impl.register
def _eval_type_type(obj: type, ctx: EvalContext):
# Ensure that any string annotations are resolved
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Calling _eval_type_type on a child class that inherits annotations (without defining its own) corrupts the parent class's __annotations__ dict.

Fix on Vercel

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@dnwpark check if the robot is right about this?

if (
hasattr(obj, '__annotations__')
and obj.__annotations__
and any(isinstance(v, str) for v in obj.__annotations__.values())
):
# Ensure we don't recurse infinitely
ctx.seen[obj] = obj

# Replace string annotations with resolved types
globalns, localns = _get_class_type_hint_namespaces(obj)
hints = {
k: _eval_types(v, ctx)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we need to _eval_types here?

And more generally I suppose, is this where we want to do this?
Maybe we'd want to do it in the helpers for Members?
I'm not sure

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We need something like this to resolve a case like this:

class A:
    b: B
    
class B:
    c: C
    
class C:
    pass
    
t = eval_typing(A)
assert t.__annotations__['b'].__annotations__['c'] is C

If we don't recursively eval, the inner annotations will stay as strings.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not sure that bothers me

for k, v in typing.get_type_hints(
obj, globalns=globalns, localns=localns, include_extras=True
).items()
}
obj.__annotations__.update(hints)

return obj


Expand Down