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

Add runtime__slots__ attribute to dataclasses#15649

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JelleZijlstra merged 1 commit intomasterfromissue-15647
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletionmypy/plugins/dataclasses.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -469,9 +469,15 @@ def add_slots(
self._cls,
)
return

info.slots = generated_slots

# Now, insert `.__slots__` attribute to class namespace:
slots_type = TupleType(
[self._api.named_type("builtins.str") for _ in generated_slots],

Choose a reason for hiding this comment

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

Is it worth creating Literal types here? So the inferred type in the test cases would betuple[Literal["x"], Literal["y"], Literal["z"]].

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

No, because regular classes produce the same result:

classA:__slots__= ('a','b')reveal_type(A.__slots__)# N: Revealed type is "tuple[builtins.str, builtins.str]"

__match_args__ attribute needsLiteral to handle match kwargs correctly, but__slots__ does not need it at all.

Choose a reason for hiding this comment

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

Thanks, I feel like we might get an issue at some point asking that we use the literal types, but it's fine if we're consistent with how explicit__slots__ works.

self._api.named_type("builtins.tuple"),
)
add_attribute_to_class(self._api, self._cls, "__slots__", slots_type)

def reset_init_only_vars(self, info: TypeInfo, attributes: list[DataclassAttribute]) -> None:
"""Remove init-only vars from the class and reset init var declarations."""
for attr in attributes:
Expand Down
29 changes: 29 additions & 0 deletionstest-data/unit/check-dataclasses.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1547,6 +1547,35 @@ class Other:
[builtins fixtures/dataclasses.pyi]


[case testDataclassWithSlotsRuntimeAttr]
# flags: --python-version 3.10
from dataclasses import dataclass

@dataclass(slots=True)
class Some:
x: int
y: str
z: bool

reveal_type(Some.__slots__) # N: Revealed type is "Tuple[builtins.str, builtins.str, builtins.str]"

@dataclass(slots=True)
class Other:
x: int
y: str

reveal_type(Other.__slots__) # N: Revealed type is "Tuple[builtins.str, builtins.str]"


@dataclass
class NoSlots:
x: int
y: str

NoSlots.__slots__ # E: "Type[NoSlots]" has no attribute "__slots__"
[builtins fixtures/dataclasses.pyi]


[case testSlotsDefinitionWithTwoPasses1]
# flags: --python-version 3.10
# https://github.com/python/mypy/issues/11821
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp