Skip to content

Commit 4e12cce

Browse files
committed
fix-completions-for-unsafe-attr
1 parent 8716194 commit 4e12cce

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

IPython/core/guarded_eval.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from IPython.utils.decorators import undoc
3232

3333

34-
from typing import Self, LiteralString
34+
from typing import Self, LiteralString, get_type_hints
3535

3636
if sys.version_info < (3, 12):
3737
from typing_extensions import TypeAliasType
@@ -1036,6 +1036,22 @@ def dummy_function(*args, **kwargs):
10361036
value = eval_node(node.value, context)
10371037
if policy.can_get_attr(value, node.attr):
10381038
return getattr(value, node.attr)
1039+
try:
1040+
cls = (
1041+
value if isinstance(value, type) else getattr(value, "__class__", None)
1042+
)
1043+
if cls is not None:
1044+
resolved_hints = get_type_hints(
1045+
cls,
1046+
globalns=(context.globals or {}),
1047+
localns=(context.locals or {}),
1048+
)
1049+
if node.attr in resolved_hints:
1050+
annotated = resolved_hints[node.attr]
1051+
return _resolve_annotation(annotated, context)
1052+
except Exception:
1053+
# Fall through to the guard rejection
1054+
pass
10391055
raise GuardRejection(
10401056
"Attribute access (`__getattr__`) for",
10411057
type(value), # not joined to avoid calling `repr`

0 commit comments

Comments
 (0)