brain/dataclasses: add inference tip for dataclasses.replace()#3061
Open
ego-ipse wants to merge 5 commits into
Open
brain/dataclasses: add inference tip for dataclasses.replace()#3061ego-ipse wants to merge 5 commits into
ego-ipse wants to merge 5 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
dataclasses.replace(obj, **changes)was inferred by following the stdlibbody of
replace()/_replace(). That body resolvesobj.__class__viathe metaclass chain, which trips when
objis an instance of a class whosebases include a subscripted generic (e.g.
IrCollection(Base[T])) — theSubscriptnode causeshelpers.object_typeto returnUninferable,propagating up to the entire
replace()call.The same bug manifests differently for old-style
Generic[T]vs PEP 695class Foo[T]syntax: old-style returnsUninferable(masked error), PEP 695returns the wrong class (visible false positives in pylint E1101).
Fix
Add a dedicated inference tip for
dataclasses.replaceinbrain_dataclasses.pythat short-circuits the stdlib body entirely: it infersthe first argument, determines its type, and yields a fresh instance of that
class. This is the same pattern used by other brain plugins (
isinstance,type(), etc.) and correctly propagates the concrete caller type throughreplace()— including acrossSelf-annotated method chains.The tip handles both call forms:
dataclasses.replace(obj, ...)(attribute access)from dataclasses import replace; replace(obj, ...)(bare name)Tests
Nine new tests in
tests/brain/test_dataclasses.py:test_replace_returns_instance_of_caller_typetest_replace_returns_subclass_instanceGeneric[T]base — concrete subclass propagatedtest_replace_pep695_generic_baseclass Base[T]— same (skipped on <3.12)test_replace_bare_name_formfrom dataclasses import replaceformtest_replace_frozen_dataclassfrozen=Trueis orthogonal, still workstest_replace_uninferable_first_argUninferabletest_replace_classdef_first_argtest_replace_non_instance_non_class_falls_backtest_replace_no_args_does_not_crash