Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 28, 2025

This PR fixes a crash in pylint when processing dataclasses that use __init_subclass__ to modify the __init__ method. The issue occurred when inheriting from a generic dataclass without providing explicit type parameters.

Problem

When __init_subclass__ modifies the __init__ method by assignment (e.g., cls.__init__ = wrapped_init), astroid's dataclass brain module would crash with an AttributeError because it expected __init__ to always be a FunctionDef node, but after reassignment it becomes an AssignAttr node.

from dataclasses import dataclass
from typing import ParamSpec

@dataclass
class Foo[T]:
    def __init_subclass__(cls) -> None:
        cls.__init__ = wrap(cls.__init__)  # This causes the crash

@dataclass
class Bar(Foo): ...  # pylint crashes here

Solution

Added a monkey patch in pylint/checkers/dataclass_checker.py that replaces astroid's _find_arguments_from_base_classes function with a version that safely handles non-FunctionDef nodes. When __init__ has been modified by __init_subclass__, the patched function simply skips processing that base class rather than crashing.

The fix is minimal and surgical:

  • Only affects the specific case where __init_subclass__ modifies __init__
  • Preserves all existing dataclass functionality
  • Gracefully handles edge cases like multiple inheritance and various assignment patterns

Testing

Added a comprehensive functional test (dataclass_generic_init_subclass.py) that verifies the fix works for both generic and non-generic dataclass inheritance scenarios. All existing dataclass and regression tests continue to pass.

Fixes #10519.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Contributor Author

Copilot AI commented Aug 28, 2025

@Pierre-Sassoulas 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

@Copilot Copilot AI changed the title [WIP] Crash with AstroidBuildingError when inheriting from a generic dataclass that has __init_subclass__ Fix crash with dataclasses that have __init_subclass__ modifying __init__ Aug 28, 2025
@Copilot Copilot AI requested a review from Pierre-Sassoulas August 28, 2025 15:20
Copilot finished work on behalf of Pierre-Sassoulas August 28, 2025 15:20
Copy link
Contributor

🤖 Effect of this PR on checked open source code: 🤖

Effect on home-assistant:
The following messages are no longer emitted:

  1. import-private-name:
    Imported private module (_collections_abc)
    https://github.com/home-assistant/core/blob/1d2599184b49e53450b168c9c45627ac641adaa4/homeassistant/components/smlight/binary_sensor.py#L5

Effect on music21:
The following messages are now emitted:

  1. invalid-name:
    Attribute name "id" doesn't conform to '[a-z_][A-Za-z0-9_]{2,30}$' pattern
    https://github.com/cuthbertLab/music21/blob/501e13d0058804471cbfba4b2d848971c86e7582/music21/prebase.py#L293
  2. redefined-variable-type:
    Redefinition of sc3 type from music21.scale.MajorScale to music21.scale.MinorScale
    https://github.com/cuthbertLab/music21/blob/501e13d0058804471cbfba4b2d848971c86e7582/music21/scale/test_scale_main.py#L169

The following messages are no longer emitted:

  1. invalid-name:
    Attribute name "id" doesn't conform to '[a-z_][A-Za-z0-9_]{2,30}$' pattern
    https://github.com/cuthbertLab/music21/blob/501e13d0058804471cbfba4b2d848971c86e7582/music21/base.py#L612
  2. redefined-variable-type:
    Redefinition of sc3 type from music21.scale.MinorScale to music21.scale.MajorScale
    https://github.com/cuthbertLab/music21/blob/501e13d0058804471cbfba4b2d848971c86e7582/music21/scale/test_scale_main.py#L163

This comment was generated for commit aea77a1

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.

Crash with AstroidBuildingError when inheriting from a generic dataclass that has __init_subclass__
2 participants