Skip to content

Remove some call_obj_hasattr in favor of a centralized hasattr - #197322

Closed
guilhermeleobas wants to merge 12 commits into
gh/guilhermeleobas/563/basefrom
gh/guilhermeleobas/563/head
Closed

guilhermeleobas wants to merge 12 commits into
gh/guilhermeleobas/563/basefrom
gh/guilhermeleobas/563/head

Conversation

@guilhermeleobas

@guilhermeleobas guilhermeleobas commented Sep 16, 2026 •

Copy link
Copy Markdown
Collaborator

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/197322

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 72a2241 with merge base d5e5e58 (image):
💚 Looks good so far! There are no failures yet. 💚

🟡 GREEN LIGHT - PR was reverted - re-landing requires human review:

Green Light will not review this PR again, on this or any later commit; re-landing it needs a human approval.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

[ghstack-poisoned]
guilhermeleobas added a commit that referenced this pull request Nov 2, 2026
@guilhermeleobas
guilhermeleobas marked this pull request as ready for review November 2, 2026 20:42
@pytorchgreenlight pytorchgreenlight Bot added the GL/human GreenLight marked as needing human review label Nov 2, 2026
[ghstack-poisoned]
guilhermeleobas added a commit that referenced this pull request Nov 3, 2026
[ghstack-poisoned]
guilhermeleobas added a commit that referenced this pull request Nov 3, 2026
# pre-existing builtin.py check: a non-constant str name wrongly raises
# TypeError instead of graph breaking.
if not name.is_python_constant():
raise_observed_exception(TypeError, tx)

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.

Why not just graph break here?

@guilhermeleobas guilhermeleobas Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Comment thread test/dynamo/test_guard_serialization.py Resolved
if tx.output.side_effects.has_pending_mutation_of_attr(obj, name):
value = tx.output.side_effects.load_attr(obj, name, deleted_ok=True)
return not isinstance(value, variables.DeletedVariable)

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 think we can install the guard here. Right now we're missing a guard if the attribute exists, i.e this gives the wrong answer

import torch


@torch.compile(backend="eager", fullgraph=True)
def f(x, o):
    if hasattr(o, "a"):
        return x + 1
    return x - 1


class obj:
    pass


o = obj()
o.a = "a"
print(f(torch.zeros(3, 3), o))
del o.a
print(f(torch.zeros(3, 3), o))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed!

[ghstack-poisoned]
guilhermeleobas added a commit that referenced this pull request Nov 5, 2026

@rtimpe rtimpe left a comment

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.

Some more AI findings

Comment on lines +2597 to +2599
if tx.output.side_effects.has_pending_mutation_of_attr(obj, name):
value = tx.output.side_effects.load_attr(obj, name, deleted_ok=True)
return not isinstance(value, variables.DeletedVariable)

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 don't think this shortcut is valid in general - it can skip tracing custom getattr, or fail for class attrs that are shadowed by deleted instance attrs. Can we get rid of it and do it in each VT's tp_getattro_impl
functions instead?

class K:
    a = "class"

@torch.compile(backend="eager", fullgraph=True)
def f(x, o):
    o.a = 1
    del o.a
    return x + 1 if hasattr(o, "a") else x - 1   # eager: x + 1, compiled: x - 1

f(torch.zeros(1), K())

Comment on lines +2630 to +2652
except (NotImplementedError, Unsupported):
pass

# tp_getattro_impl could not resolve the name. generic_getattr would defer
# by wrapping it in a GetAttrVariable, which is indistinguishable from a
# missing attribute, so ask the concrete object (or its type) instead.
probe = NO_SUCH_SUBOBJ
for get_probe in (obj.get_real_python_backed_value, obj.python_type):
try:
probe = get_probe()
except (NotImplementedError, AttributeError):
continue
if probe is not NO_SUCH_SUBOBJ:
break
if probe is not NO_SUCH_SUBOBJ:
found = hasattr(probe, name)
if not found and obj.source:
install_guard(
obj.source.make_guard(
functools.partial(GuardBuilder.HASATTR, attr=name)
)
)
return found

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.

My agent found some regressions here

Following up on my earlier guard comment: the main path is fixed now, but this fallback has a few problems:

  1. A True answer from the probe doesn't install a guard, so the result goes stale.
  2. Catching Unsupported here hides graph breaks. The real hasattr then runs the user's getter once, at compile time, even with fullgraph=True.
  3. When there's no real object, it asks python_type(), which answers for the type, not the instance.
import torch

# 1. True from the fallback installs no guard
class Fn(torch.autograd.Function):
    @staticmethod
    def forward(ctx, x): return x
    @staticmethod
    def backward(ctx, g): return g
Fn.flag = 1

@torch.compile(backend="eager", fullgraph=True)
def f(x):
    return x + 1 if hasattr(Fn, "flag") else x - 1

print(f(torch.zeros(1)))  # tensor([1.])
del Fn.flag
print(f(torch.zeros(1)))  # eager: tensor([-1.]), compiled: tensor([1.])

# 2. Unsupported is swallowed and the getter runs at compile time
log = []
class A:
    @property
    def p(self):
        torch._dynamo.graph_break()
        log.append("p")
        return 1

@torch.compile(backend="eager", fullgraph=True)
def g(x, a):
    return x + 1 if hasattr(a, "p") else x - 1

for _ in range(3):
    log.clear()
    g(torch.zeros(1), A())
    print(log)  # eager: ['p'] every call; compiled: no error under fullgraph, ['p'] then [] []

# 3. With no real object, the answer comes from the type
@torch.compile(backend="eager", fullgraph=True)
def h(x):
    return x + 1 if hasattr(iter(range(3)), "__name__") else x - 1

print(h(torch.zeros(1)))  # eager: tensor([-1.]), compiled: tensor([1.])

All three gave the eager answer before this PR. For (2) that means the Unsupported graph break under fullgraph.

Can we guard both answers on this path, and graph break instead of probing when tp_getattro_impl raises Unsupported or there's no real object?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Moving PR to draft. There are some corner cases that are not easy to fix

@guilhermeleobas
guilhermeleobas marked this pull request as draft November 6, 2026 19:33
@guilhermeleobas
guilhermeleobas removed the request for review from williamwen42 November 6, 2026 19:34
[ghstack-poisoned]
guilhermeleobas added a commit that referenced this pull request Nov 6, 2026
[ghstack-poisoned]
guilhermeleobas added a commit that referenced this pull request Nov 9, 2026
@pytorchmergebot pytorchmergebot added Reverted ci-no-td Do not run TD on this PR labels Nov 11, 2026
[ghstack-poisoned]
guilhermeleobas added a commit that referenced this pull request Nov 12, 2026
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 12, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: e085aa0
Pull-Request: pytorch#203908
[ghstack-poisoned]
guilhermeleobas added a commit that referenced this pull request Nov 12, 2026
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 12, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: 3333b2a
Pull-Request: pytorch#203908
@guilhermeleobas

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 13, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: ac8cd3e
Pull-Request: pytorch#203908
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 15, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: 339cd58
Pull-Request: pytorch#203908
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 16, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: 8676bbf
Pull-Request: pytorch#203908
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 17, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: 10bd401
Pull-Request: pytorch#203908
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 18, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: 688749a
Pull-Request: pytorch#203908
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 18, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: 6343351
Pull-Request: pytorch#203908
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 19, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: 1497fd5
Pull-Request: pytorch#203908
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 19, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: 96ed633
Pull-Request: pytorch#203908
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 21, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: e2f307e
Pull-Request: pytorch#203908
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 21, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: 9715a87
Pull-Request: pytorch#203908
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 22, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: 15653ad
Pull-Request: pytorch#203908
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 25, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: 2b9720f
Pull-Request: pytorch#203908
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 25, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: 5f634dc
Pull-Request: pytorch#203908
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 26, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: 57d66ec
Pull-Request: pytorch#203908
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Nov 27, 2026
Resolve defaulted getattr through the existing descriptor path once, instead of probing and then reading a user getter twice. Guard instance/class absence and the MRO lookup hook, including inherited __getattribute__, so adding a lookup hook cannot leave a previously compiled default in the graph. Keep pending instance/class writes visible without invoking getters again.

Immutable builtin iterables can report missing attributes directly when their actual runtime type has generic lookup. Preserve AttributeError metadata and set-family iterator types. Retain the locally required behavior while optional-attribute work in pytorch#197322 lands separately; mapping construction and update consumers remain in the following PR.

At the native-matched Python3.15 CPU prefix,25 dictionary attribute cases and21 getattr function cases pass. The existing added-lookup-hook regression fails for direct and inherited __getattribute__ when only the MRO-hook protection is removed. The iterator selection runs25 cases with two existing expected failures. Pinned lint and explicit hidden-worktree typing pass. Older-Python and free-threaded execution remains pending.

```bash
python3 test/dynamo/test_functions.py -k getattr
python3 test/dynamo/test_dicts.py -k attribute -k getattr -k absence
python3 test/dynamo/test_iterators.py -k missing -k attribute
```

Prepared with an AI assistant for local human review.

ghstack-source-id: 3504eab
Pull-Request: pytorch#203908
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request GL/superseded GreenLight review is superseded by changes Merged module: dynamo open source release notes: dynamo Reverted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants