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

Fields with single underscore names can mess up dataclasses #98886

Closed
hauntsaninja opened this issue Oct 30, 2022 · 0 comments
Closed

Fields with single underscore names can mess up dataclasses #98886

hauntsaninja opened this issue Oct 30, 2022 · 0 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@hauntsaninja
Copy link
Contributor

hauntsaninja commented Oct 30, 2022

A similar issue to #96151. ericvsmith mentioned this is worth opening an issue for in #98143 (comment)

dataclasses uses variables with single underscore names as part of its implementation. This can cause interesting errors, for example:

from dataclasses import dataclass, field

@dataclass
class X:
    x: int = field(default_factory=lambda: 111)
    _dflt_x: int = field(default_factory=lambda: 222)

X()  # TypeError: '_HAS_DEFAULT_FACTORY_CLASS' object is not callable

The fix is simple: prefix all of these things with __dataclass_, to make name collisions more obviously the user's fault. We already do this for e.g. __dataclass_self__ in the implementation.

Linked PRs

@hauntsaninja hauntsaninja added the type-bug An unexpected behavior, bug, or error label Oct 30, 2022
@hauntsaninja hauntsaninja self-assigned this Oct 30, 2022
@hauntsaninja hauntsaninja added 3.12 bugs and security fixes stdlib Python modules in the Lib dir and removed 3.12 bugs and security fixes labels Oct 30, 2022
hauntsaninja added a commit to hauntsaninja/cpython that referenced this issue Feb 18, 2023
This commit prefixes `__dataclass` to several things in the locals dict:
- Names like _dflt_ (which cause trouble, see first test)
- Names like _type_ (not known to be able to cause trouble)
- _return_type (not known to able to cause trouble)
- _HAS_DEFAULT_FACTORY (which causes trouble, see second test)

In addition, this removes `MISSING` from the locals dict. As far as I
can tell, this wasn't needed even in the initial implementation of
dataclasses.py (and tests on that version passed with it removed).

This is basically a continuation of python#96151, where fixing this was
welcomed in python#98143 (comment)
hauntsaninja added a commit to hauntsaninja/cpython that referenced this issue Feb 20, 2023
hauntsaninja added a commit to hauntsaninja/cpython that referenced this issue Feb 20, 2023
hauntsaninja added a commit that referenced this issue Mar 25, 2023
…mes (#102032)

This commit prefixes `__dataclass` to several things in the locals dict:
- Names like `_dflt_` (which cause trouble, see first test)
- Names like `_type_` (not known to be able to cause trouble)
- `_return_type` (not known to able to cause trouble)
- `_HAS_DEFAULT_FACTORY` (which causes trouble, see second test)

In addition, this removes `MISSING` from the locals dict. As far as I can tell, this wasn't needed even in the initial implementation of dataclasses.py (and tests on that version passed with it removed). This makes me wary :-)

This is basically a continuation of #96151, where fixing this was welcomed in #98143 (comment)
Fidget-Spinner pushed a commit to Fidget-Spinner/cpython that referenced this issue Mar 27, 2023
…ore names (python#102032)

This commit prefixes `__dataclass` to several things in the locals dict:
- Names like `_dflt_` (which cause trouble, see first test)
- Names like `_type_` (not known to be able to cause trouble)
- `_return_type` (not known to able to cause trouble)
- `_HAS_DEFAULT_FACTORY` (which causes trouble, see second test)

In addition, this removes `MISSING` from the locals dict. As far as I can tell, this wasn't needed even in the initial implementation of dataclasses.py (and tests on that version passed with it removed). This makes me wary :-)

This is basically a continuation of python#96151, where fixing this was welcomed in python#98143 (comment)
warsaw pushed a commit to warsaw/cpython that referenced this issue Apr 11, 2023
…ore names (python#102032)

This commit prefixes `__dataclass` to several things in the locals dict:
- Names like `_dflt_` (which cause trouble, see first test)
- Names like `_type_` (not known to be able to cause trouble)
- `_return_type` (not known to able to cause trouble)
- `_HAS_DEFAULT_FACTORY` (which causes trouble, see second test)

In addition, this removes `MISSING` from the locals dict. As far as I can tell, this wasn't needed even in the initial implementation of dataclasses.py (and tests on that version passed with it removed). This makes me wary :-)

This is basically a continuation of python#96151, where fixing this was welcomed in python#98143 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant