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

Fix attrs.evolve on bound TypeVar #15022

Merged
merged 2 commits into from
Apr 12, 2023

Conversation

ikonst
Copy link
Contributor

@ikonst ikonst commented Apr 9, 2023

Fixes the error on the last line of this example:

@attrs.define
class A:
  x: int

T = TypeVar('T', bound=A)

def f(t: T) -> None:
   _ = attrs.evolve(t, x=42)  # E: Argument 1 to "evolve" has incompatible type "T"; expected an attrs class

Since T is bounded by A, we know it can be treated as A.

@ikonst ikonst force-pushed the 2023-04-08-attrs-replace-typevar branch from 3a8b535 to dfd155b Compare April 9, 2023 03:24
@ikonst ikonst changed the title 2023 04 08 attrs replace typevar Fix attrs.evolve on bound TypeVar Apr 9, 2023
@github-actions

This comment has been minimized.

@ikonst
Copy link
Contributor Author

ikonst commented Apr 9, 2023

👋 @hauntsaninja @JelleZijlstra

@ikonst ikonst force-pushed the 2023-04-08-attrs-replace-typevar branch from 5088b60 to a3364f1 Compare April 9, 2023 04:05
@github-actions
Copy link
Contributor

github-actions bot commented Apr 9, 2023

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

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Looks great, thank you!

@hauntsaninja hauntsaninja merged commit cc7b062 into python:master Apr 12, 2023
@ikonst ikonst deleted the 2023-04-08-attrs-replace-typevar branch April 12, 2023 11:38
@ikonst
Copy link
Contributor Author

ikonst commented Apr 12, 2023

@hauntsaninja thinking about it, is there an existing facility I can use here, so that I could also support unions? Feels like checking isinstance and accessing .upper_bound is a whack-a-mole that doesn't belong here.

@hauntsaninja
Copy link
Collaborator

Good question, if you can get something from mypy.checkmember to work for you that's probably the way to go

@ikonst
Copy link
Contributor Author

ikonst commented Apr 12, 2023

That was my first thought, but checkmember is complex and probably ill-fitted for anything that's not true member access.

I thought I'd need some kind of TypeVisitor that'll list the types, then iterate over the types and "union" the signatures, e.g.

(a: int, b: int, c: object) | (a: bool, b: str) = (a: bool,)

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

Successfully merging this pull request may close these issues.

None yet

3 participants