Skip to content

Assigning a function to a ClassVar in a dataclass doesn't exclude it from __init__ #2025

@bluetech

Description

@bluetech

Describe the Bug

The following program raises an unexpected error:

from typing import ClassVar
import dataclasses


@dataclasses.dataclass
class MyDataClass:
    replace: ClassVar = dataclasses.replace


# ERROR Missing argument `replace` in function `MyDataClass.__init__` [missing-argument]
MyDataClass(x=0)

Since the replace attribute is annotated as ClassVar it should not be considered for __init__. I see that ClassVar is handed correctly in some cases (e.g. if I remove the assignment or use an assignment like = 5), but it seems that specifically assigning a function throws it off.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0BhKKjhwAaqkoAddFx586mVA1QBjYaPgzt6AAJKV6kXHiEDajXBlHRdALKkAIstRDjiGXS91KMYsNUYREFLCUo6AF5FFxsTOEJffzUYHRkHZxU3UQAKAEoQABoQAFcGaHiyCgBiOgBVMqgIJjowYvRVMtx0K3QZTBgwFt4aZQB9dGKabBhKbPxg1gZcugBaAD46OAZKD3RvHxgGYso9sCkQADlJ6Z26YHwAX3OZQpAyXzAoUkIGWihqugABVIHy%2BmwwOAIdFUXUgbGOyggXUIMhqAGUYDA6AALBgMYhwRAAeiJ7wGX0IvDYRJg6CJmFwqjgRJh6DhCM6dKG4VQADdUNBUNhYNDYRB4ZREV06LhiJz4jIyAxsV0VryZnAkXsoucAMyEACMACZnugQA8imoyuqAGLQGAUNBYPBESoPIA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions