Uh oh!
There was an error while loading. Please reload this page.
Fix attrs.evolve with generics - #15016
Conversation
ikonst
commented
Apr 7, 2023
👋 @JelleZijlstra you reviewed #14526 so you might have context |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
vfazio
commented
Apr 7, 2023
I was looking around for MRs touching I was hoping this MR resolved our issue, but it doesn't appear to do so. Our use case is below. I made a minor tweak to make things "work" albeit maybe not in a satisfactory way. I'm willing to open an issue and discuss the proper fix for it if it doesn't fit with this MR re evolve + Generics. importattrfromtypingimportGeneric, TypeVarfromdataclassesimportdataclassC_T=TypeVar("C_T", bound="Config")
@attr.sclassConfig():
name: str=attr.ib()
@dataclassclassConfigFactory(Generic[C_T]):
_configs: list[C_T]
defcreate_configs(self) ->None:
forcurrent_configinself._configs:
x=attr.evolve(current_config, name="abc")
print(x)
@dataclassclassTestCF(ConfigFactory[Config]):
passdeftest() ->None:
factory=TestCF(list())
factory._configs.extend([Config(name="test")])
factory.create_configs()Where I needed to add: diff --git a/mypy/plugins/attrs.py b/mypy/plugins/attrs.py
index 98090cbeb..668293fad 100644
--- a/mypy/plugins/attrs.py+++ b/mypy/plugins/attrs.py@@ -963,7 +963,10 @@ def evolve_function_sig_callback(ctx: mypy.plugin.FunctionSigContext) -> Callabl
# </hack>
inst_type = get_proper_type(inst_type)
+ if isinstance(inst_type, TypeVarType):+ inst_type = inst_type.upper_bound
inst_type_str = format_type_bare(inst_type)
+ |
This comment has been minimized.
This comment has been minimized.
@vfazio tried addressing it in the next commits I basically did what you suggested, but added an
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
8ade002 to
e9f876eCompare
This comment has been minimized.
This comment has been minimized.
vfazio
commented
Apr 8, 2023
Awesome, thanks! |
| if attrs_init_type is None: | ||
| ctx.api.fail( | ||
| f'Argument 1 to "evolve" has incompatible type "{inst_type_str}"; expected an attrs class', | ||
| f'Argument 1 to "evolve" has a variable type "{inst_type_str}" not bound to an attrs class' |
There was a problem hiding this comment.
Should this be more friendly, e.g.
has a variable type "T" that is not necessarily an attrs class; consider adding bounds= to your TypeVar definition
I can't tell where we stand between succinct and instructive.
ikonst
commented
Apr 9, 2023
Split the TypeVar fix into #15022, to be merged before this. |
@hauntsaninja while I have your attention... (There's a flood of comments above but it's basically around split-out #15022 which we already merged.) |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
ikonst
commented
Apr 14, 2023
Closed in favor of #15050 which also addresses unions. |
Fixes
attrs.evolvefor generic attrs classes.