Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

BUILTINS is not a valid field name for a frozen dataclass #96151

Closed
Assignees
ericvsmith
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@hauntsaninja

Description

@hauntsaninja

This is not a bug that I encountered in the wild, but I happened to notice the possibility for this while reading the source.

from dataclasses import dataclass@dataclass(frozen=True)class X:    BUILTINS: strX(5)

will give you:

Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "<string>", line 3, in __init__AttributeError: 'int' object has no attribute 'object'

There are other field names that you can use to trick dataclass into bad behaviour, but all of the other ones are prefixed by an underscore.

So one "fix" is pretty simple: just renameBUILTINS to_BUILTINS in dataclasses.py to bring it in line with the others.

If a single underscore doesn't feel like sufficient "this is the user's fault", we could use__dataclass_* (like we do for__dataclass_self__ param to__init__)

For what it's worth, the only thing we needBUILTINS for is so we can accessobject inside__init__, so an alternative fix could be to use().__class__.__base__ instead ofBUILTINS.object (and skip the locals dance needed to defineBUILTINS altogether)

If any of that sounds worth doing, I'd be happy to open a PR

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp