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

Add support for attrs.fields #15021

Merged
merged 10 commits into from May 20, 2023
Merged

Add support for attrs.fields #15021

merged 10 commits into from May 20, 2023

Conversation

Tinche
Copy link
Contributor

@Tinche Tinche commented Apr 9, 2023

Howdy Mypy friends.

This add support for attrs.fields, which is a nicer way of accessing the attrs magic attribute.

@github-actions

This comment has been minimized.

@Tinche
Copy link
Contributor Author

Tinche commented Apr 9, 2023

Great feedback. Looks like I'll need to switch to get_function_signature_hook instead of get_function_hook though. get_function_hook appears to run after type-checking, so the attrs stubs for fields are interfering with the TypeVar handling.

@github-actions

This comment has been minimized.

@Tinche Tinche requested a review from ikonst April 9, 2023 22:00
@ikonst
Copy link
Contributor

ikonst commented Apr 10, 2023

I'll try to take a look tomorrow.

@Tinche
Copy link
Contributor Author

Tinche commented May 10, 2023

@hauntsaninja Hello, I know you mentioned the plugin subsystem isn't really your thing, but could you recommend someone else to help review this? <3

@JelleZijlstra
Copy link
Member

I can review this, please fix the merge conflict first though.

@JelleZijlstra JelleZijlstra self-assigned this May 10, 2023
@JelleZijlstra JelleZijlstra requested review from JelleZijlstra and removed request for ikonst May 10, 2023 22:07
@github-actions

This comment has been minimized.

@Tinche
Copy link
Contributor Author

Tinche commented May 10, 2023

@JelleZijlstra Thank you! Done.

mypy/plugins/attrs.py Outdated Show resolved Hide resolved
mypy/plugins/attrs.py Outdated Show resolved Hide resolved
mypy/plugins/attrs.py Outdated Show resolved Hide resolved
mypy/plugins/attrs.py Outdated Show resolved Hide resolved
mypy/plugins/attrs.py Outdated Show resolved Hide resolved
mypy/plugins/attrs.py Outdated Show resolved Hide resolved
@github-actions

This comment has been minimized.

Tinche and others added 2 commits May 12, 2023 01:25
Co-authored-by: Ilya Priven <ilya.konstantinov@gmail.com>
@github-actions

This comment has been minimized.

@Tinche
Copy link
Contributor Author

Tinche commented May 12, 2023

I don't think this PR will handle unions properly, but I consider this an advanced use case and can tackle it in a future PR.

@github-actions

This comment has been minimized.

@JelleZijlstra JelleZijlstra self-requested a review May 12, 2023 12:41
@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

graphql-core (https://github.com/graphql-python/graphql-core) got 1.05x slower (282.3s -> 297.1s)

@JelleZijlstra JelleZijlstra merged commit 391ed85 into python:master May 20, 2023
20 checks passed
cdce8p added a commit to cdce8p/ha-core that referenced this pull request May 29, 2023
Add support for attrs.fields
python/mypy#15021
@jhance
Copy link
Collaborator

jhance commented Jun 8, 2023

I don't believe that inferring a fixed-length tuple here is actually that useful. In actual uses of attr.fields in our codebase, we tend to iterate over the attr.fields, but when you do that, the value is computed as object rather than attr.Attribute. e.g.

for field in attr.fields(Foo):
    reveal_type(field)

now reveals builtins.object when I want something more like attr.Attribute[Any].

@Tinche
Copy link
Contributor Author

Tinche commented Jun 8, 2023

I'm pretty bullish on this change since it unlocks very interesting stuff, both by itself and down the line, such as python-attrs/attrs#1144.

That said, maybe we can stick a custom __iter__ on the result type to fix this issue in particular?

@Tinche Tinche deleted the tin/fields branch June 8, 2023 14:39
jhance added a commit to jhance/mypy that referenced this pull request Jun 8, 2023
This reverts commit 391ed85.

The use of a Union item type for `attr.Attribute` would likely be
more useful to callsites of `attr.fields` - otherwise this currently
results in inferring `builtins.object` in many cases where we treat
the fields as a variable-length tuple, rather than a fixed-length
one. Applications of the fixed-length tuple seem limited since you
would need to access it by index.

@Tinche as the original author of the commit.
@JukkaL
Copy link
Collaborator

JukkaL commented Jun 8, 2023

That said, maybe we can stick a custom iter on the result type to fix this issue in particular?

Yeah, I think this could work. @jhance What do you think?

@jhance
Copy link
Collaborator

jhance commented Jun 8, 2023

I am more interested in basic cases continuing to work than supporting more interesting advanced cases. I am not sure how you would add an extra __iter__ to the TupleType return value here.

I'm happy to have this feature if it doesn't break basic use cases, but resolving the breakage here is blocking release for us. I am going to include the revert in the release branch for 1.4.0, but wait on reverting in master until we have a plan for what we want to do here.

jhance added a commit that referenced this pull request Jun 8, 2023
This reverts commit 391ed85.

The use of a Union item type for `attr.Attribute` would likely be
more useful to callsites of `attr.fields` - otherwise this currently
results in inferring `builtins.object` in many cases where we treat
the fields as a variable-length tuple, rather than a fixed-length
one. Applications of the fixed-length tuple seem limited since you
would need to access it by index.

@Tinche as the original author of the commit.
svalentin added a commit to svalentin/mypy that referenced this pull request Jul 14, 2023
This reverts commit 391ed85.
Similar to the revert for release-1.4 in commit 3bf9fdc.

Had to do it manually since it won't revert cleanly.
svalentin added a commit that referenced this pull request Jul 14, 2023
This reverts commit 391ed85 for release-1.5.
Similar to the revert for release-1.4 in commit 3bf9fdc.

Had to do it manually since it won't revert cleanly.
koogoro added a commit to koogoro/mypy that referenced this pull request Aug 25, 2023
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

6 participants