Skip to content

Conversation

XuehaiPan
Copy link
Collaborator

@XuehaiPan XuehaiPan commented Apr 18, 2024

Stack from ghstack (oldest at bottom):

Closes #114966

Frozen field assignment in __init__ in Python 3.8-3.9:

https://github.com/python/cpython/blob/f5bd65ed37bbccfa113438cf9e34feae6816ecb1/Lib/dataclasses.py#L402-L411

import builtins

BUILTINS = builtins

def _field_assign(frozen, name, value, self_name):
    # If we're a frozen class, then assign to our fields in __init__
    # via object.__setattr__.  Otherwise, just use a simple
    # assignment.
    #
    # self_name is what "self" is called in this function: don't
    # hard-code "self", since that might be a field name.
    if frozen:
        return f'BUILTINS.object.__setattr__({self_name},{name!r},{value})'
    return f'{self_name}.{name}={value}'

Frozen field assignment in __init__ in Python 3.10+:

https://github.com/python/cpython/blob/812245ecce2d8344c3748228047bab456816180a/Lib/dataclasses.py#L436-L445

__dataclass_builtins_object__ = object

def _field_assign(frozen, name, value, self_name):
    # If we're a frozen class, then assign to our fields in __init__
    # via object.__setattr__.  Otherwise, just use a simple
    # assignment.
    #
    # self_name is what "self" is called in this function: don't
    # hard-code "self", since that might be a field name.
    if frozen:
        return f'__dataclass_builtins_object__.__setattr__({self_name},{name!r},{value})'
    return f'{self_name}.{name}={value}'

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng @chauhang

[ghstack-poisoned]
Copy link

pytorch-bot bot commented Apr 18, 2024

🔗 Helpful Links

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

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

✅ You can merge normally! (3 Unrelated Failures)

As of commit 4e7ddb8 with merge base 5677128 (image):

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

@XuehaiPan XuehaiPan linked an issue Apr 18, 2024 that may be closed by this pull request
@XuehaiPan XuehaiPan added ciflow/trunk Trigger trunk jobs on your pull request release notes: dynamo labels Apr 18, 2024
@XuehaiPan XuehaiPan changed the title [dynamo, 3.8] support dataclass with frozen=True in Python 3.8 [dynamo, 3.8-3.9] support dataclass with frozen=True in Python 3.8/3.9 Apr 18, 2024
@XuehaiPan XuehaiPan requested a review from jansel April 18, 2024 13:30
[ghstack-poisoned]
[ghstack-poisoned]
@XuehaiPan
Copy link
Collaborator Author

@pytorchbot merge

@pytorchmergebot
Copy link
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

sanketpurandare pushed a commit to sanketpurandare/pytorch that referenced this pull request Apr 22, 2024
…3.9 (pytorch#124393)

Closes pytorch#114966

Frozen field assignment in `__init__` in Python 3.8-3.9:

https://github.com/python/cpython/blob/f5bd65ed37bbccfa113438cf9e34feae6816ecb1/Lib/dataclasses.py#L402-L411

```python
import builtins

BUILTINS = builtins

def _field_assign(frozen, name, value, self_name):
    # If we're a frozen class, then assign to our fields in __init__
    # via object.__setattr__.  Otherwise, just use a simple
    # assignment.
    #
    # self_name is what "self" is called in this function: don't
    # hard-code "self", since that might be a field name.
    if frozen:
        return f'BUILTINS.object.__setattr__({self_name},{name!r},{value})'
    return f'{self_name}.{name}={value}'
```

Frozen field assignment in `__init__` in Python 3.10+:

https://github.com/python/cpython/blob/812245ecce2d8344c3748228047bab456816180a/Lib/dataclasses.py#L436-L445

```python
__dataclass_builtins_object__ = object

def _field_assign(frozen, name, value, self_name):
    # If we're a frozen class, then assign to our fields in __init__
    # via object.__setattr__.  Otherwise, just use a simple
    # assignment.
    #
    # self_name is what "self" is called in this function: don't
    # hard-code "self", since that might be a field name.
    if frozen:
        return f'__dataclass_builtins_object__.__setattr__({self_name},{name!r},{value})'
    return f'{self_name}.{name}={value}'
```

Pull Request resolved: pytorch#124393
Approved by: https://github.com/jansel
petrex pushed a commit to petrex/pytorch that referenced this pull request May 3, 2024
…3.9 (pytorch#124393)

Closes pytorch#114966

Frozen field assignment in `__init__` in Python 3.8-3.9:

https://github.com/python/cpython/blob/f5bd65ed37bbccfa113438cf9e34feae6816ecb1/Lib/dataclasses.py#L402-L411

```python
import builtins

BUILTINS = builtins

def _field_assign(frozen, name, value, self_name):
    # If we're a frozen class, then assign to our fields in __init__
    # via object.__setattr__.  Otherwise, just use a simple
    # assignment.
    #
    # self_name is what "self" is called in this function: don't
    # hard-code "self", since that might be a field name.
    if frozen:
        return f'BUILTINS.object.__setattr__({self_name},{name!r},{value})'
    return f'{self_name}.{name}={value}'
```

Frozen field assignment in `__init__` in Python 3.10+:

https://github.com/python/cpython/blob/812245ecce2d8344c3748228047bab456816180a/Lib/dataclasses.py#L436-L445

```python
__dataclass_builtins_object__ = object

def _field_assign(frozen, name, value, self_name):
    # If we're a frozen class, then assign to our fields in __init__
    # via object.__setattr__.  Otherwise, just use a simple
    # assignment.
    #
    # self_name is what "self" is called in this function: don't
    # hard-code "self", since that might be a field name.
    if frozen:
        return f'__dataclass_builtins_object__.__setattr__({self_name},{name!r},{value})'
    return f'{self_name}.{name}={value}'
```

Pull Request resolved: pytorch#124393
Approved by: https://github.com/jansel
fathnd pushed a commit to fathnd/homomorphic that referenced this pull request May 5, 2024
@github-actions github-actions bot deleted the gh/XuehaiPan/28/head branch June 1, 2024 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[dynamo] dynamo does not support dataclasses with frozen=True

4 participants