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

Add dataclass InitVar test case #12798

Merged
merged 1 commit into from
May 16, 2022
Merged

Add dataclass InitVar test case #12798

merged 1 commit into from
May 16, 2022

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented May 16, 2022

This test case covers a recent change in functionality:
if an attribute is defined as an InitVar, it can't be
assigned to. Previously this was supported (probably
by accident).

This test case cover a recent change in functionality:
if an attribute is defined as an InitVar, it can't be
assigned to. Previously this was supported (probably
by accident).
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

This makes sense to me. (If you use slots=True on 3.10, does it generate a slot for InitVars, out of interest, do you know?)

@AlexWaygood
Copy link
Member

If you use slots=True on 3.10, does it generate a slot for InitVars, out of interest, do you know?

I've confirmed locally that slots for InitVars are not autogenerated at runtime with slots=True, so that supports this behaviour even more:

>>> from dataclasses import dataclass, InitVar
>>> @dataclass(slots=True)
... class Foo:
...     bar: InitVar[str]
...     baz: InitVar[int] = 0
...
>>> f = Foo('hi', 5)
>>> f.__slots__
()
>>> f.bar = 'bye'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute 'bar'
>>> f.baz = 6
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object attribute 'baz' is read-only

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants